-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTestKernel.php
225 lines (197 loc) · 6.33 KB
/
TestKernel.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 CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Testing\HttpKernel;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Bundle\WebServerBundle\WebServerBundle;
use Symfony\Cmf\Component\Testing\DependencyInjection\Compiler\TestContainerPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
/**
* TestKernel base class for Symfony CMF Bundle
* integration tests.
*
* @author Daniel Leech <[email protected]>
*/
abstract class TestKernel extends Kernel
{
protected $bundleSets = [];
protected $requiredBundles = [];
/**
* Register commonly needed bundle sets and then
* after initializing the parent kernel, let the
* concrete kernel configure itself using the abstracvt
* configure() command.
*/
public function __construct($env, $debug)
{
$defaultBundles = [
FrameworkBundle::class,
SecurityBundle::class,
TwigBundle::class,
MonologBundle::class,
];
if (class_exists(WebServerBundle::class)) {
$defaultBundles[] = WebServerBundle::class;
}
$this->registerBundleSet('default', $defaultBundles);
$this->registerBundleSet('phpcr_odm', [DoctrineBundle::class, DoctrinePHPCRBundle::class]);
$this->registerBundleSet('doctrine_orm', [DoctrineBundle::class]);
parent::__construct($env, $debug);
$this->configure();
}
/**
* Use this method to declare which bundles are required
* by the Kernel, e.g.
*
* $this->requireBundleSets('default', 'phpcr_odm');
* $this->addBundle(new MyBundle);
* $this->addBundles(array(new Bundle1, new Bundle2));
*/
abstract protected function configure();
/**
* Register a set of bundles with the given name.
*
* This method does not add the bundles to the kernel,
* it just makes a set available.
*/
public function registerBundleSet($name, $bundles)
{
$this->bundleSets[$name] = $bundles;
}
/**
* The bundles in the named sets will be added to the Kernel.
*/
public function requireBundleSets(array $names)
{
foreach ($names as $name) {
$this->requireBundleSet($name);
}
}
/**
* Require the bundles in the named bundle set.
*
* Note that we register the FQN's and not the concrete classes.
* This enables us to declare pre-defined bundle sets without
* worrying if the bundle is actually present or not.
*/
public function requireBundleSet($name)
{
if (!isset($this->bundleSets[$name])) {
throw new \InvalidArgumentException(sprintf(
'Bundle set %s has not been registered, available bundle sets: %s',
$name,
implode(',', array_keys($this->bundleSets))
));
}
foreach ($this->bundleSets[$name] as $bundle) {
if (!class_exists($bundle)) {
throw new \InvalidArgumentException(sprintf(
'Bundle class "%s" does not exist.',
$bundle
));
}
$this->requiredBundles[$bundle] = new $bundle();
}
}
/**
* Add concrete bundles to the kernel.
*/
public function addBundles(array $bundles)
{
foreach ($bundles as $bundle) {
$this->addBundle($bundle);
}
}
/**
* Add a concrete bundle to the kernel.
*/
public function addBundle(BundleInterface $bundle)
{
$this->requiredBundles[] = $bundle;
}
/**
* {@inheritDoc}.
*
* Here we return our list of bundles
*/
public function registerBundles(): iterable
{
return $this->requiredBundles;
}
/**
* Returns the KernelDir of the CHILD class,
* i.e. the concrete implementation in the bundles
* src/ directory (or wherever).
*/
public function getKernelDir()
{
return $this->getProjectDir();
}
public function getProjectDir(): string
{
$refl = new \ReflectionClass($this);
$fname = $refl->getFileName();
$kernelDir = \dirname($fname);
return $kernelDir;
}
public function getCacheDir(): string
{
return implode('/', [
$this->getKernelDir(),
'var',
'cache',
]);
}
public function getLogDir(): string
{
return implode('/', [
$this->getKernelDir(),
'var',
'logs',
]);
}
/**
* Registers the bundles defined in config/bundles.php.
*/
protected function registerConfiguredBundles()
{
$bundleFilePath = $this->getKernelDir().'/config/bundles.php';
if (!file_exists($bundleFilePath)) {
return;
}
$bundles = require $bundleFilePath;
foreach ($bundles as $class => $environments) {
if (isset($environments['all']) || isset($environments[$this->environment])) {
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf(
'Bundle class "%s" does not exist.',
$class
));
}
$this->requiredBundles[$class] = new $class();
}
}
}
protected function build(ContainerBuilder $container): void
{
parent::build($container);
if (\in_array($this->getEnvironment(), ['test', 'phpcr']) && file_exists($this->getKernelDir().'/config/public_services.php')) {
$services = require $this->getKernelDir().'/config/public_services.php';
$container->addCompilerPass(new TestContainerPass($services), PassConfig::TYPE_OPTIMIZE);
}
}
}