Skip to content

Commit 6f187b4

Browse files
authored
Automatically boot the SF kernel (symfony#99)
* Automatically boot the SF kernel * Fix --prefer-lowest
1 parent 1d47189 commit 6f187b4

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
bootstrap="vendor/autoload.php"
88
colors="true"
99
>
10+
11+
<php>
12+
<env name="KERNEL_CLASS" value="Symfony\Component\Panther\Tests\DummyKernel" />
13+
</php>
14+
1015
<testsuites>
1116
<testsuite name="Project Test Suite">
1217
<directory>tests</directory>

src/PantherTestCaseTrait.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Goutte\Client as GoutteClient;
1717
use GuzzleHttp\Client as GuzzleClient;
18+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1819
use Symfony\Component\Panther\Client as PantherClient;
1920
use Symfony\Component\Panther\ProcessManager\WebServerManager;
2021

@@ -86,17 +87,27 @@ protected static function startWebServer(?string $webServerDir = null, string $h
8687
self::$baseUri = "http://$hostname:$port";
8788
}
8889

89-
protected static function createPantherClient(string $hostname = '127.0.0.1', int $port = 9000): PantherClient
90+
/**
91+
* @param array $kernelOptions An array of options to pass to the createKernel method
92+
*/
93+
protected static function createPantherClient(string $hostname = '127.0.0.1', int $port = 9000, array $kernelOptions = []): PantherClient
9094
{
9195
self::startWebServer(null, $hostname, $port);
9296
if (null === self::$pantherClient) {
9397
self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
9498
}
9599

100+
if (\is_a(self::class, KernelTestCase::class, true)) {
101+
static::bootKernel($kernelOptions);
102+
}
103+
96104
return self::$pantherClient;
97105
}
98106

99-
protected static function createGoutteClient(string $hostname = '127.0.0.1', int $port = 9000): GoutteClient
107+
/**
108+
* @param array $kernelOptions An array of options to pass to the createKernel method
109+
*/
110+
protected static function createGoutteClient(string $hostname = '127.0.0.1', int $port = 9000, array $kernelOptions = []): GoutteClient
100111
{
101112
if (!\class_exists(GoutteClient::class)) {
102113
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
@@ -110,6 +121,10 @@ protected static function createGoutteClient(string $hostname = '127.0.0.1', int
110121
self::$goutteClient = $goutteClient;
111122
}
112123

124+
if (\is_a(self::class, KernelTestCase::class, true)) {
125+
static::bootKernel($kernelOptions);
126+
}
127+
113128
return self::$goutteClient;
114129
}
115130
}

tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\BrowserKit\Cookie;
2020
use Symfony\Component\BrowserKit\CookieJar as BrowserKitCookieJar;
2121
use Symfony\Component\DomCrawler\Crawler as DomCrawlerCrawler;
22+
use Symfony\Component\HttpKernel\KernelInterface;
2223
use Symfony\Component\Panther\Client;
2324
use Symfony\Component\Panther\Cookie\CookieJar;
2425
use Symfony\Component\Panther\DomCrawler\Crawler;
@@ -33,6 +34,7 @@ public function testCreateClient()
3334
$client = self::createPantherClient();
3435
$this->assertInstanceOf(BrowserKitClient::class, $client);
3536
$this->assertInstanceOf(WebDriver::class, $client);
37+
$this->assertInstanceOf(KernelInterface::class, self::$kernel);
3638
}
3739

3840
public function testWaitFor()

tests/DummyKernel.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panther project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Panther\Tests;
15+
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpKernel\KernelInterface;
19+
20+
/**
21+
* @author Kévin Dunglas <[email protected]>
22+
*/
23+
class DummyKernel implements KernelInterface
24+
{
25+
public function serialize()
26+
{
27+
}
28+
29+
public function unserialize($serialized)
30+
{
31+
}
32+
33+
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
34+
{
35+
}
36+
37+
public function registerBundles()
38+
{
39+
}
40+
41+
public function registerContainerConfiguration(LoaderInterface $loader)
42+
{
43+
}
44+
45+
public function boot()
46+
{
47+
}
48+
49+
public function shutdown()
50+
{
51+
}
52+
53+
public function getBundles()
54+
{
55+
}
56+
57+
public function getBundle($name, $first = true)
58+
{
59+
}
60+
61+
public function locateResource($name, $dir = null, $first = true)
62+
{
63+
}
64+
65+
public function getName()
66+
{
67+
}
68+
69+
public function getEnvironment()
70+
{
71+
}
72+
73+
public function isDebug()
74+
{
75+
}
76+
77+
public function getRootDir()
78+
{
79+
}
80+
81+
public function getContainer()
82+
{
83+
return new class() implements \Psr\Container\ContainerInterface {
84+
public function get($id)
85+
{
86+
}
87+
88+
public function has($id)
89+
{
90+
return false;
91+
}
92+
};
93+
}
94+
95+
public function getStartTime()
96+
{
97+
}
98+
99+
public function getCacheDir()
100+
{
101+
}
102+
103+
public function getLogDir()
104+
{
105+
}
106+
107+
public function getCharset()
108+
{
109+
}
110+
}

0 commit comments

Comments
 (0)