Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d26f2bf

Browse filesBrowse files
Fix PhpUnit 8 compatibility
1 parent 5e9373b commit d26f2bf
Copy full SHA for d26f2bf

File tree

4 files changed

+290
-216
lines changed
Filter options

4 files changed

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.