Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 835280b

Browse files
[FrameworkBundle] fix BC-breaking property in WebTestAssertionsTrait
1 parent a6b306d commit 835280b

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\Constraint\LogicalNot;
1616
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1717
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
18+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1819
use Symfony\Component\DomCrawler\Crawler;
1920
use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
2021
use Symfony\Component\HttpFoundation\Request;
@@ -28,6 +29,32 @@
2829
*/
2930
trait WebTestAssertionsTrait
3031
{
32+
/**
33+
* Creates a KernelBrowser.
34+
*
35+
* @param array $options An array of options to pass to the createKernel method
36+
* @param array $server An array of server parameters
37+
*
38+
* @return KernelBrowser A KernelBrowser instance
39+
*/
40+
protected static function createClient(array $options = [], array $server = [])
41+
{
42+
$kernel = static::bootKernel($options);
43+
44+
try {
45+
$client = $kernel->getContainer()->get('test.client');
46+
} catch (ServiceNotFoundException $e) {
47+
if (class_exists(KernelBrowser::class)) {
48+
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
49+
}
50+
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"');
51+
}
52+
53+
$client->setServerParameters($server);
54+
55+
return self::getClient($client);
56+
}
57+
3158
public static function assertResponseIsSuccessful(string $message = ''): void
3259
{
3360
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
@@ -186,13 +213,19 @@ public static function assertRouteSame($expectedRoute, array $parameters = [], s
186213
self::assertThat(static::getRequest(), $constraint, $message);
187214
}
188215

189-
private static function getClient(): KernelBrowser
216+
private static function getClient(KernelBrowser $newClient = null): ?KernelBrowser
190217
{
191-
if (!static::$client instanceof KernelBrowser) {
218+
static $client;
219+
220+
if (0 < \func_num_args()) {
221+
return $client = $newClient;
222+
}
223+
224+
if (!$client instanceof KernelBrowser) {
192225
static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient"?', __CLASS__));
193226
}
194227

195-
return static::$client;
228+
return $client;
196229
}
197230

198231
private static function getCrawler(): Crawler

src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

14-
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
15-
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
16-
1714
/**
1815
* WebTestCase is the base class for functional tests.
1916
*
@@ -23,39 +20,9 @@ abstract class WebTestCase extends KernelTestCase
2320
{
2421
use WebTestAssertionsTrait;
2522

26-
/** @var KernelBrowser|null */
27-
protected static $client;
28-
2923
protected function doTearDown(): void
3024
{
3125
parent::doTearDown();
32-
33-
static::$client = null;
34-
}
35-
36-
/**
37-
* Creates a KernelBrowser.
38-
*
39-
* @param array $options An array of options to pass to the createKernel method
40-
* @param array $server An array of server parameters
41-
*
42-
* @return KernelBrowser A KernelBrowser instance
43-
*/
44-
protected static function createClient(array $options = [], array $server = [])
45-
{
46-
$kernel = static::bootKernel($options);
47-
48-
try {
49-
$client = $kernel->getContainer()->get('test.client');
50-
} catch (ServiceNotFoundException $e) {
51-
if (class_exists(KernelBrowser::class)) {
52-
throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.');
53-
}
54-
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"');
55-
}
56-
57-
$client->setServerParameters($server);
58-
59-
return static::$client = $client;
26+
self::getClient(null);
6027
}
6128
}

src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,9 @@ private function getTester(KernelBrowser $client): WebTestCase
277277
return new class($client) extends WebTestCase {
278278
use WebTestAssertionsTrait;
279279

280-
protected static $client;
281-
282280
public function __construct(KernelBrowser $client)
283281
{
284-
static::$client = $client;
282+
self::getClient($client);
285283
}
286284
};
287285
}

0 commit comments

Comments
 (0)