Skip to content

Commit 27dcd8b

Browse files
committed
Fix createClient() support
1 parent 177a10b commit 27dcd8b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/PantherTestCase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
namespace Symfony\Component\Panther;
1515

1616
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
18+
use Symfony\Bundle\FrameworkBundle\Test\ForwardCompatTestTrait;
1719
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
1820
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1921
use Symfony\Component\BrowserKit\AbstractBrowser;
22+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2023
use Symfony\Component\Panther\Client as PantherClient;
2124

2225
if (\class_exists(WebTestCase::class)) {
@@ -31,6 +34,7 @@
3134
if ($canUseAssertions) {
3235
abstract class PantherTestCase extends WebTestCase
3336
{
37+
use ForwardCompatTestTrait;
3438
use PantherTestCaseTrait;
3539
use WebTestAssertionsTrait {
3640
assertPageTitleSame as private baseAssertPageTitleSame;
@@ -60,6 +64,43 @@ public static function assertPageTitleContains(string $expectedTitle, string $me
6064

6165
self::baseAssertPageTitleContains($expectedTitle, $message);
6266
}
67+
68+
private function doTearDown()
69+
{
70+
parent::tearDown();
71+
self::getClient(null);
72+
}
73+
74+
// Copied from WebTestCase to allow assertions to work with createClient
75+
76+
/**
77+
* Creates a KernelBrowser.
78+
*
79+
* @param array $options An array of options to pass to the createKernel method
80+
* @param array $server An array of server parameters
81+
*
82+
* @return KernelBrowser A KernelBrowser instance
83+
*/
84+
protected static function createClient(array $options = [], array $server = [])
85+
{
86+
$kernel = static::bootKernel($options);
87+
88+
try {
89+
/**
90+
* @var KernelBrowser
91+
*/
92+
$client = $kernel->getContainer()->get('test.client');
93+
} catch (ServiceNotFoundException $e) {
94+
if (class_exists(KernelBrowser::class)) {
95+
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
96+
}
97+
throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit"');
98+
}
99+
100+
$client->setServerParameters($server);
101+
102+
return self::getClient($client);
103+
}
63104
}
64105
} else {
65106
abstract class PantherTestCase extends WebTestCase

0 commit comments

Comments
 (0)