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