|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Test;
|
13 | 13 |
|
14 |
| -use PHPUnit\Framework\TestCase; |
15 |
| -use Symfony\Component\DependencyInjection\ResettableContainerInterface; |
16 |
| -use Symfony\Component\Finder\Finder; |
17 |
| -use Symfony\Component\HttpKernel\KernelInterface; |
| 14 | +use PHPUnit\Runner\Version; |
18 | 15 |
|
19 |
| -/** |
20 |
| - * KernelTestCase is the base class for tests needing a Kernel. |
21 |
| - * |
22 |
| - * @author Fabien Potencier <[email protected]> |
23 |
| - */ |
24 |
| -abstract class KernelTestCase extends TestCase |
25 |
| -{ |
26 |
| - protected static $class; |
27 |
| - |
28 |
| - /** |
29 |
| - * @var KernelInterface |
30 |
| - */ |
31 |
| - protected static $kernel; |
32 |
| - |
33 |
| - /** |
34 |
| - * Finds the directory where the phpunit.xml(.dist) is stored. |
35 |
| - * |
36 |
| - * If you run tests with the PHPUnit CLI tool, everything will work as expected. |
37 |
| - * If not, override this method in your test classes. |
38 |
| - * |
39 |
| - * @return string The directory where phpunit.xml(.dist) is stored |
40 |
| - * |
41 |
| - * @throws \RuntimeException |
42 |
| - * |
43 |
| - * @deprecated since 3.4 and will be removed in 4.0. |
44 |
| - */ |
45 |
| - protected static function getPhpUnitXmlDir() |
46 |
| - { |
47 |
| - @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); |
48 |
| - |
49 |
| - if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) { |
50 |
| - throw new \RuntimeException('You must override the KernelTestCase::createKernel() method.'); |
51 |
| - } |
52 |
| - |
53 |
| - $dir = static::getPhpUnitCliConfigArgument(); |
54 |
| - if (null === $dir && |
55 |
| - (is_file(getcwd().\DIRECTORY_SEPARATOR.'phpunit.xml') || |
56 |
| - is_file(getcwd().\DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) { |
57 |
| - $dir = getcwd(); |
58 |
| - } |
59 |
| - |
60 |
| - // Can't continue |
61 |
| - if (null === $dir) { |
62 |
| - throw new \RuntimeException('Unable to guess the Kernel directory.'); |
63 |
| - } |
64 |
| - |
65 |
| - if (!is_dir($dir)) { |
66 |
| - $dir = \dirname($dir); |
67 |
| - } |
68 |
| - |
69 |
| - return $dir; |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * Finds the value of the CLI configuration option. |
74 |
| - * |
75 |
| - * PHPUnit will use the last configuration argument on the command line, so this only returns |
76 |
| - * the last configuration argument. |
77 |
| - * |
78 |
| - * @return string The value of the PHPUnit CLI configuration option |
79 |
| - * |
80 |
| - * @deprecated since 3.4 and will be removed in 4.0. |
81 |
| - */ |
82 |
| - private static function getPhpUnitCliConfigArgument() |
83 |
| - { |
84 |
| - @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); |
85 |
| - |
86 |
| - $dir = null; |
87 |
| - $reversedArgs = array_reverse($_SERVER['argv']); |
88 |
| - foreach ($reversedArgs as $argIndex => $testArg) { |
89 |
| - if (preg_match('/^-[^ \-]*c$/', $testArg) || '--configuration' === $testArg) { |
90 |
| - $dir = realpath($reversedArgs[$argIndex - 1]); |
91 |
| - break; |
92 |
| - } elseif (0 === strpos($testArg, '--configuration=')) { |
93 |
| - $argPath = substr($testArg, \strlen('--configuration=')); |
94 |
| - $dir = realpath($argPath); |
95 |
| - break; |
96 |
| - } elseif (0 === strpos($testArg, '-c')) { |
97 |
| - $argPath = substr($testArg, \strlen('-c')); |
98 |
| - $dir = realpath($argPath); |
99 |
| - break; |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - return $dir; |
104 |
| - } |
105 |
| - |
106 |
| - /** |
107 |
| - * Attempts to guess the kernel location. |
108 |
| - * |
109 |
| - * When the Kernel is located, the file is required. |
110 |
| - * |
111 |
| - * @return string The Kernel class name |
112 |
| - * |
113 |
| - * @throws \RuntimeException |
114 |
| - */ |
115 |
| - protected static function getKernelClass() |
116 |
| - { |
117 |
| - if (isset($_SERVER['KERNEL_CLASS']) || isset($_ENV['KERNEL_CLASS'])) { |
118 |
| - $class = isset($_ENV['KERNEL_CLASS']) ? $_ENV['KERNEL_CLASS'] : $_SERVER['KERNEL_CLASS']; |
119 |
| - if (!class_exists($class)) { |
120 |
| - throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.', $class, static::class)); |
121 |
| - } |
122 |
| - |
123 |
| - return $class; |
124 |
| - } else { |
125 |
| - @trigger_error(sprintf('Using the KERNEL_DIR environment variable or the automatic guessing based on the phpunit.xml / phpunit.xml.dist file location is deprecated since Symfony 3.4. Set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel instead. Not setting the KERNEL_CLASS environment variable will throw an exception on 4.0 unless you override the %1$::createKernel() or %1$::getKernelClass() method.', static::class), E_USER_DEPRECATED); |
126 |
| - } |
127 |
| - |
128 |
| - if (isset($_SERVER['KERNEL_DIR']) || isset($_ENV['KERNEL_DIR'])) { |
129 |
| - $dir = isset($_ENV['KERNEL_DIR']) ? $_ENV['KERNEL_DIR'] : $_SERVER['KERNEL_DIR']; |
130 |
| - |
131 |
| - if (!is_dir($dir)) { |
132 |
| - $phpUnitDir = static::getPhpUnitXmlDir(); |
133 |
| - if (is_dir("$phpUnitDir/$dir")) { |
134 |
| - $dir = "$phpUnitDir/$dir"; |
135 |
| - } |
136 |
| - } |
137 |
| - } else { |
138 |
| - $dir = static::getPhpUnitXmlDir(); |
139 |
| - } |
140 |
| - |
141 |
| - $finder = new Finder(); |
142 |
| - $finder->name('*Kernel.php')->depth(0)->in($dir); |
143 |
| - $results = iterator_to_array($finder); |
144 |
| - if (!\count($results)) { |
145 |
| - throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to https://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.'); |
146 |
| - } |
147 |
| - |
148 |
| - $file = current($results); |
149 |
| - $class = $file->getBasename('.php'); |
150 |
| - |
151 |
| - require_once $file; |
152 |
| - |
153 |
| - return $class; |
154 |
| - } |
155 |
| - |
156 |
| - /** |
157 |
| - * Boots the Kernel for this test. |
158 |
| - * |
159 |
| - * @return KernelInterface A KernelInterface instance |
160 |
| - */ |
161 |
| - protected static function bootKernel(array $options = []) |
162 |
| - { |
163 |
| - static::ensureKernelShutdown(); |
164 |
| - |
165 |
| - static::$kernel = static::createKernel($options); |
166 |
| - static::$kernel->boot(); |
167 |
| - |
168 |
| - return static::$kernel; |
169 |
| - } |
170 |
| - |
171 |
| - /** |
172 |
| - * Creates a Kernel. |
173 |
| - * |
174 |
| - * Available options: |
175 |
| - * |
176 |
| - * * environment |
177 |
| - * * debug |
178 |
| - * |
179 |
| - * @return KernelInterface A KernelInterface instance |
180 |
| - */ |
181 |
| - protected static function createKernel(array $options = []) |
182 |
| - { |
183 |
| - if (null === static::$class) { |
184 |
| - static::$class = static::getKernelClass(); |
185 |
| - } |
186 |
| - |
187 |
| - if (isset($options['environment'])) { |
188 |
| - $env = $options['environment']; |
189 |
| - } elseif (isset($_ENV['APP_ENV'])) { |
190 |
| - $env = $_ENV['APP_ENV']; |
191 |
| - } elseif (isset($_SERVER['APP_ENV'])) { |
192 |
| - $env = $_SERVER['APP_ENV']; |
193 |
| - } else { |
194 |
| - $env = 'test'; |
195 |
| - } |
196 |
| - |
197 |
| - if (isset($options['debug'])) { |
198 |
| - $debug = $options['debug']; |
199 |
| - } elseif (isset($_ENV['APP_DEBUG'])) { |
200 |
| - $debug = $_ENV['APP_DEBUG']; |
201 |
| - } elseif (isset($_SERVER['APP_DEBUG'])) { |
202 |
| - $debug = $_SERVER['APP_DEBUG']; |
203 |
| - } else { |
204 |
| - $debug = true; |
205 |
| - } |
206 |
| - |
207 |
| - return new static::$class($env, $debug); |
208 |
| - } |
209 |
| - |
210 |
| - /** |
211 |
| - * Shuts the kernel down if it was used in the test. |
212 |
| - */ |
213 |
| - protected static function ensureKernelShutdown() |
214 |
| - { |
215 |
| - if (null !== static::$kernel) { |
216 |
| - $container = static::$kernel->getContainer(); |
217 |
| - static::$kernel->shutdown(); |
218 |
| - if ($container instanceof ResettableContainerInterface) { |
219 |
| - $container->reset(); |
220 |
| - } |
221 |
| - } |
222 |
| - } |
223 |
| - |
224 |
| - /** |
225 |
| - * Clean up Kernel usage in this test. |
226 |
| - */ |
227 |
| - protected function tearDown() |
228 |
| - { |
229 |
| - static::ensureKernelShutdown(); |
230 |
| - } |
| 16 | +if (class_exists(Version::class) && version_compare(Version::id(), '8.0.0') >= 0) { |
| 17 | + class_alias(KernelTestCaseNew::class, KernelTestCase::class, true); |
| 18 | +} else { |
| 19 | + class_alias(KernelTestCaseOld::class, KernelTestCase::class, true); |
231 | 20 | }
|
0 commit comments