|
13 | 13 |
|
14 | 14 | namespace Panthere; |
15 | 15 |
|
16 | | -use Goutte\Client as GoutteClient; |
17 | | -use GuzzleHttp\Client as GuzzleClient; |
18 | | -use Panthere\Client as PanthereClient; |
19 | | -use Panthere\ProcessManager\WebServerManager; |
20 | 16 | use PHPUnit\Framework\TestCase; |
21 | 17 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
22 | 18 |
|
23 | 19 | if (\class_exists(WebTestCase::class)) { |
24 | | - /** |
25 | | - * @internal |
26 | | - */ |
27 | | - abstract class InternalTestCase extends WebTestCase |
| 20 | + abstract class PanthereTestCase extends WebTestCase |
28 | 21 | { |
| 22 | + use PanthereTestCaseTrait; |
29 | 23 | } |
30 | 24 | } else { |
31 | | - /** |
32 | | - * @internal |
33 | | - */ |
34 | | - abstract class InternalTestCase extends TestCase |
| 25 | + abstract class PanthereTestCase extends TestCase |
35 | 26 | { |
36 | | - } |
37 | | -} |
38 | | - |
39 | | -/** |
40 | | - * @author Kévin Dunglas <[email protected]> |
41 | | - */ |
42 | | -abstract class PanthereTestCase extends InternalTestCase |
43 | | -{ |
44 | | - /** |
45 | | - * @var string|null |
46 | | - */ |
47 | | - protected static $webServerDir; |
48 | | - |
49 | | - /** |
50 | | - * @var WebServerManager|null |
51 | | - */ |
52 | | - protected static $webServerManager; |
53 | | - |
54 | | - /** |
55 | | - * @var string|null |
56 | | - */ |
57 | | - protected static $baseUri; |
58 | | - |
59 | | - /** |
60 | | - * @var GoutteClient|null |
61 | | - */ |
62 | | - protected static $goutteClient; |
63 | | - |
64 | | - /** |
65 | | - * @var PanthereClient|null |
66 | | - */ |
67 | | - protected static $panthereClient; |
68 | | - |
69 | | - public static function tearDownAfterClass() |
70 | | - { |
71 | | - if (null !== self::$webServerManager) { |
72 | | - self::$webServerManager->quit(); |
73 | | - self::$webServerManager = null; |
74 | | - } |
75 | | - |
76 | | - if (null !== self::$panthereClient) { |
77 | | - self::$panthereClient->quit(); |
78 | | - self::$panthereClient = null; |
79 | | - } |
80 | | - |
81 | | - if (null !== self::$goutteClient) { |
82 | | - self::$goutteClient = null; |
83 | | - } |
84 | | - |
85 | | - self::$baseUri = null; |
86 | | - } |
87 | | - |
88 | | - protected static function startWebServer(?string $webServerDir = null, string $hostname = '127.0.0.1', int $port = 9000): void |
89 | | - { |
90 | | - if (null !== static::$webServerManager) { |
91 | | - return; |
92 | | - } |
93 | | - |
94 | | - if (null === $webServerDir) { |
95 | | - // Try the local $webServerDir property, or the PANTHERE_WEB_SERVER_DIR env var or default to the Flex directory structure |
96 | | - $webServerDir = static::$webServerDir ?? $_ENV['PANTHERE_WEB_SERVER_DIR'] ?? __DIR__.'/../../../../public'; |
97 | | - } |
98 | | - |
99 | | - self::$webServerManager = new WebServerManager($webServerDir, $hostname, $port); |
100 | | - self::$webServerManager->start(); |
101 | | - |
102 | | - self::$baseUri = "http://$hostname:$port"; |
103 | | - } |
104 | | - |
105 | | - protected static function createPanthereClient(string $hostname = '127.0.0.1', int $port = 9000): PanthereClient |
106 | | - { |
107 | | - self::startWebServer(null, $hostname, $port); |
108 | | - if (null === self::$panthereClient) { |
109 | | - self::$panthereClient = Client::createChromeClient(null, null, [], self::$baseUri); |
110 | | - } |
111 | | - |
112 | | - return self::$panthereClient; |
113 | | - } |
114 | | - |
115 | | - protected static function createGoutteClient(): GoutteClient |
116 | | - { |
117 | | - if (!\class_exists(GoutteClient::class)) { |
118 | | - throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".'); |
119 | | - } |
120 | | - |
121 | | - self::startWebServer(); |
122 | | - if (null === self::$goutteClient) { |
123 | | - $goutteClient = new GoutteClient(); |
124 | | - $goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri])); |
125 | | - |
126 | | - self::$goutteClient = $goutteClient; |
127 | | - } |
128 | | - |
129 | | - return self::$goutteClient; |
| 27 | + use PanthereTestCaseTrait; |
130 | 28 | } |
131 | 29 | } |
0 commit comments