|
14 | 14 | namespace Symfony\Component\Panther; |
15 | 15 |
|
16 | 16 | use PHPUnit\Framework\TestCase; |
17 | | -use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
18 | 17 | use Symfony\Bundle\FrameworkBundle\Test\ForwardCompatTestTrait; |
19 | | -use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait; |
20 | 18 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
21 | | -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
22 | | -use Symfony\Component\Panther\Client as PantherClient; |
23 | 19 |
|
24 | 20 | if (\class_exists(WebTestCase::class)) { |
25 | | - if (trait_exists('Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait') && trait_exists('Symfony\Bundle\FrameworkBundle\Test\ForwardCompatTestTrait')) { |
26 | | - abstract class PantherTestCase extends WebTestCase |
27 | | - { |
28 | | - use ForwardCompatTestTrait; |
29 | | - use PantherTestCaseTrait; |
30 | | - use WebTestAssertionsTrait { |
31 | | - assertPageTitleSame as private baseAssertPageTitleSame; |
32 | | - assertPageTitleContains as private baseAssertPageTitleContains; |
33 | | - } |
34 | | - |
35 | | - public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void |
| 21 | + if (trait_exists('Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait')) { |
| 22 | + if (trait_exists('Symfony\Bundle\FrameworkBundle\Test\ForwardCompatTestTrait')) { |
| 23 | + // Symfony 4.3 |
| 24 | + abstract class PantherTestCase extends WebTestCase |
36 | 25 | { |
37 | | - $client = self::getClient(); |
38 | | - if ($client instanceof PantherClient) { |
39 | | - self::assertSame($expectedTitle, $client->getTitle()); |
| 26 | + use ForwardCompatTestTrait; |
| 27 | + use WebTestAssertionsTrait; |
40 | 28 |
|
41 | | - return; |
| 29 | + private function doTearDown() |
| 30 | + { |
| 31 | + parent::tearDown(); |
| 32 | + self::getClient(null); |
42 | 33 | } |
43 | | - |
44 | | - self::baseAssertPageTitleSame($expectedTitle, $message); |
45 | 34 | } |
46 | | - |
47 | | - public static function assertPageTitleContains(string $expectedTitle, string $message = ''): void |
| 35 | + } else { |
| 36 | + // Symfony 5 |
| 37 | + abstract class PantherTestCase extends WebTestCase |
48 | 38 | { |
49 | | - $client = self::getClient(); |
50 | | - if ($client instanceof PantherClient) { |
51 | | - if (method_exists(self::class, 'assertStringContainsString')) { |
52 | | - self::assertStringContainsString($expectedTitle, $client->getTitle()); |
53 | | - |
54 | | - return; |
55 | | - } |
56 | | - |
57 | | - self::assertContains($expectedTitle, $client->getTitle()); |
| 39 | + use WebTestAssertionsTrait; |
58 | 40 |
|
59 | | - return; |
| 41 | + protected function tearDown(): void |
| 42 | + { |
| 43 | + parent::tearDown(); |
| 44 | + self::getClient(null); |
60 | 45 | } |
61 | | - |
62 | | - self::baseAssertPageTitleContains($expectedTitle, $message); |
63 | | - } |
64 | | - |
65 | | - private function doTearDown() |
66 | | - { |
67 | | - parent::tearDown(); |
68 | | - self::getClient(null); |
69 | | - } |
70 | | - |
71 | | - // Copied from WebTestCase to allow assertions to work with createClient |
72 | | - |
73 | | - /** |
74 | | - * Creates a KernelBrowser. |
75 | | - * |
76 | | - * @param array $options An array of options to pass to the createKernel method |
77 | | - * @param array $server An array of server parameters |
78 | | - * |
79 | | - * @return KernelBrowser A KernelBrowser instance |
80 | | - */ |
81 | | - protected static function createClient(array $options = [], array $server = []) |
82 | | - { |
83 | | - $kernel = static::bootKernel($options); |
84 | | - |
85 | | - try { |
86 | | - /** |
87 | | - * @var KernelBrowser |
88 | | - */ |
89 | | - $client = $kernel->getContainer()->get('test.client'); |
90 | | - } catch (ServiceNotFoundException $e) { |
91 | | - if (class_exists(KernelBrowser::class)) { |
92 | | - throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.'); |
93 | | - } |
94 | | - 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"'); |
95 | | - } |
96 | | - |
97 | | - $client->setServerParameters($server); |
98 | | - |
99 | | - return self::getClient($client); |
100 | 46 | } |
101 | 47 | } |
102 | 48 | } else { |
| 49 | + // Symfony 4.3 and inferior |
103 | 50 | abstract class PantherTestCase extends WebTestCase |
104 | 51 | { |
105 | 52 | use PantherTestCaseTrait; |
106 | 53 | } |
107 | 54 | } |
108 | 55 | } else { |
| 56 | + // Without Symfony |
109 | 57 | abstract class PantherTestCase extends TestCase |
110 | 58 | { |
111 | 59 | use PantherTestCaseTrait; |
|
0 commit comments