-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathKernelTestCase.php
143 lines (120 loc) · 4.47 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Service\ResetInterface;
/**
* KernelTestCase is the base class for tests needing a Kernel.
*
* @author Fabien Potencier <[email protected]>
*/
abstract class KernelTestCase extends TestCase
{
use MailerAssertionsTrait;
use NotificationAssertionsTrait;
protected static $class;
/**
* @var KernelInterface
*/
protected static $kernel;
protected static $booted = false;
protected function tearDown(): void
{
static::ensureKernelShutdown();
static::$class = null;
static::$kernel = null;
static::$booted = false;
}
/**
* @throws \RuntimeException
* @throws \LogicException
*/
protected static function getKernelClass(): string
{
if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) {
throw new \LogicException(sprintf('You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the "%1$s::createKernel()" or "%1$s::getKernelClass()" method.', static::class));
}
if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_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;
}
/**
* Boots the Kernel for this test.
*/
protected static function bootKernel(array $options = []): KernelInterface
{
static::ensureKernelShutdown();
$kernel = static::createKernel($options);
$kernel->boot();
static::$kernel = $kernel;
static::$booted = true;
return static::$kernel;
}
/**
* Provides a dedicated test container with access to both public and private
* services. The container will not include private services that have been
* inlined or removed. Private services will be removed when they are not
* used by other services.
*
* Using this method is the best way to get a container from your test code.
*
* @return Container
*/
protected static function getContainer(): ContainerInterface
{
if (!static::$booted) {
static::bootKernel();
}
try {
return self::$kernel->getContainer()->get('test.service_container');
} catch (ServiceNotFoundException $e) {
throw new \LogicException('Could not find service "test.service_container". Try updating the "framework.test" config to "true".', 0, $e);
}
}
/**
* Creates a Kernel.
*
* Available options:
*
* * environment
* * debug
*/
protected static function createKernel(array $options = []): KernelInterface
{
static::$class ??= static::getKernelClass();
$env = $options['environment'] ?? $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'test';
$debug = $options['debug'] ?? $_ENV['APP_DEBUG'] ?? $_SERVER['APP_DEBUG'] ?? true;
return new static::$class($env, $debug);
}
/**
* Shuts the kernel down if it was used in the test - called by the tearDown method by default.
*/
protected static function ensureKernelShutdown()
{
if (null !== static::$kernel) {
static::$kernel->boot();
$container = static::$kernel->getContainer();
if ($container->has('services_resetter')) {
// Instantiate the service because Container::reset() only resets services that have been used
$container->get('services_resetter');
}
static::$kernel->shutdown();
static::$booted = false;
if ($container instanceof ResetInterface) {
$container->reset();
}
}
}
}