1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension; |
3
|
|
|
|
4
|
|
|
use Behat\MinkExtension\ServiceContainer\MinkExtension; |
5
|
|
|
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension; |
6
|
|
|
use Behat\Testwork\ServiceContainer\Exception\ProcessingException; |
7
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
8
|
|
|
use Symfony\Component\BrowserKit\CookieJar; |
9
|
|
|
use Symfony\Component\BrowserKit\History; |
10
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use Yoanm\Behat3SymfonyExtension\Client\Client; |
14
|
|
|
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelAwareInitializer; |
15
|
|
|
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelHandlerAwareInitializer; |
16
|
|
|
use Yoanm\Behat3SymfonyExtension\Handler\KernelHandler; |
17
|
|
|
use Yoanm\Behat3SymfonyExtension\ServiceContainer\AbstractExtension; |
18
|
|
|
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory; |
19
|
|
|
use Yoanm\Behat3SymfonyExtension\Subscriber\RebootKernelSubscriber; |
20
|
|
|
|
21
|
|
|
class KernelSubExtension extends AbstractExtension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
3 |
|
public function getConfigKey() |
27
|
|
|
{ |
28
|
3 |
|
return 'kernel'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// @codeCoverageIgnoreStart |
32
|
|
|
// Not possible to cover this because ExtensionManager is a final class |
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
|
|
public function initialize(ExtensionManager $extensionManager) |
37
|
|
|
{ |
38
|
|
|
$minExtension = $extensionManager->getExtension('mink'); |
39
|
|
|
if ($minExtension instanceof MinkExtension) { |
40
|
|
|
$minExtension->registerDriverFactory(new Behat3SymfonyFactory()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
// @codeCoverageIgnoreEnd |
44
|
|
|
|
45
|
|
|
// @codeCoverageIgnoreStart |
46
|
|
|
// Will be covered by FT |
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function configure(ArrayNodeDefinition $builder) |
51
|
|
|
{ |
52
|
|
|
$castToBool = function ($value) { |
53
|
|
|
$filtered = filter_var( |
54
|
|
|
$value, |
55
|
|
|
FILTER_VALIDATE_BOOLEAN, |
56
|
|
|
FILTER_NULL_ON_FAILURE |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return (null === $filtered) ? (bool) $value : $filtered; |
60
|
|
|
}; |
61
|
|
|
$builder |
62
|
|
|
->addDefaultsIfNotSet() |
63
|
|
|
->children() |
64
|
|
|
->scalarNode('bootstrap') |
65
|
|
|
->defaultValue('app/autoload.php') |
66
|
|
|
->end() |
67
|
|
|
->scalarNode('path') |
68
|
|
|
->defaultValue('app/AppKernel.php') |
69
|
|
|
->end() |
70
|
|
|
->scalarNode('class') |
71
|
|
|
->defaultValue('AppKernel') |
72
|
|
|
->end() |
73
|
|
|
->scalarNode('env') |
74
|
|
|
->defaultValue('test') |
75
|
|
|
->end() |
76
|
|
|
->booleanNode('debug') |
77
|
|
|
->beforeNormalization() |
78
|
|
|
->always() |
79
|
|
|
->then($castToBool) |
80
|
|
|
->end() |
81
|
|
|
->defaultTrue() |
82
|
|
|
->end() |
83
|
|
|
->booleanNode('reboot') |
84
|
|
|
->info('If true symfony kernel will be rebooted after each scenario/example') |
85
|
|
|
->beforeNormalization() |
86
|
|
|
->always() |
87
|
|
|
->then($castToBool) |
88
|
|
|
->end() |
89
|
|
|
->defaultTrue() |
90
|
|
|
->end() |
91
|
|
|
->end(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function load(ContainerBuilder $container, array $config) |
98
|
|
|
{ |
99
|
|
|
$kernelConfig = $config[$this->getConfigKey()]; |
100
|
|
|
|
101
|
|
|
$this->loadContainerParameter($container, $kernelConfig); |
102
|
|
|
$this->loadInitializer($container); |
103
|
|
|
$this->loadSubscriber($container, $kernelConfig); |
104
|
|
|
$this |
105
|
|
|
->createService( |
106
|
|
|
$container, |
107
|
|
|
'test.client', |
108
|
|
|
Client::class, |
109
|
|
|
[ |
110
|
|
|
new Reference($this->buildContainerId('handler.kernel')), |
111
|
|
|
new Reference(self::KERNEL_SERVICE_ID), |
112
|
|
|
[], |
113
|
|
|
new Reference($this->buildContainerId('test.client.history')), |
114
|
|
|
new Reference($this->buildContainerId('test.client.cookiejar')) |
115
|
|
|
] |
116
|
|
|
) |
117
|
|
|
->setShared(false); |
118
|
|
|
$this |
119
|
|
|
->createService( |
120
|
|
|
$container, |
121
|
|
|
'test.client.history', |
122
|
|
|
History::class |
123
|
|
|
) |
124
|
|
|
->setShared(false); |
125
|
|
|
$this |
126
|
|
|
->createService( |
127
|
|
|
$container, |
128
|
|
|
'test.client.cookiejar', |
129
|
|
|
CookieJar::class |
130
|
|
|
) |
131
|
|
|
->setShared(false); |
132
|
|
|
$this->createService( |
133
|
|
|
$container, |
134
|
|
|
'kernel', |
135
|
|
|
$kernelConfig['class'], |
136
|
|
|
[$kernelConfig['env'], $kernelConfig['debug']], |
137
|
|
|
[], |
138
|
|
|
[['boot']] |
139
|
|
|
); |
140
|
|
|
$this->createService( |
141
|
|
|
$container, |
142
|
|
|
'handler.kernel', |
143
|
|
|
KernelHandler::class, |
144
|
|
|
[ |
145
|
|
|
new Reference('event_dispatcher'), |
146
|
|
|
new Reference(self::KERNEL_SERVICE_ID), |
147
|
|
|
] |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function process(ContainerBuilder $container) |
155
|
|
|
{ |
156
|
|
|
$basePath = $container->getParameter('paths.base'); |
157
|
|
|
$bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap')); |
158
|
|
|
if ($bootstrapPath) { |
159
|
|
|
$bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath); |
160
|
|
|
if (file_exists($bootstrapPathUnderBasePath)) { |
161
|
|
|
$bootstrapPath = $bootstrapPathUnderBasePath; |
162
|
|
|
} |
163
|
|
|
if (file_exists($bootstrapPath)) { |
164
|
|
|
require_once($bootstrapPath); |
165
|
|
|
} else { |
166
|
|
|
throw new ProcessingException('Could not find bootstrap file !'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// load kernel |
171
|
|
|
$kernelPath = $container->getParameter($this->buildContainerId('kernel.path')); |
172
|
|
|
$kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath); |
173
|
|
|
if (file_exists($kernelPathUnderBasePath)) { |
174
|
|
|
$kernelPath = $kernelPathUnderBasePath; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$container->getDefinition(self::KERNEL_SERVICE_ID) |
178
|
|
|
->setFile($kernelPath); |
179
|
|
|
|
180
|
|
|
} |
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param ContainerBuilder $container |
184
|
|
|
*/ |
185
|
|
|
protected function loadInitializer(ContainerBuilder $container) |
186
|
|
|
{ |
187
|
|
|
$this->createService( |
188
|
|
|
$container, |
189
|
|
|
'initializer.kernel_aware', |
190
|
|
|
KernelAwareInitializer::class, |
191
|
|
|
[new Reference(self::KERNEL_SERVICE_ID)], |
192
|
|
|
['context.initializer'] |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$this->createService( |
196
|
|
|
$container, |
197
|
|
|
'initializer.kernel_handler_aware', |
198
|
|
|
KernelHandlerAwareInitializer::class, |
199
|
|
|
[new Reference($this->buildContainerId('handler.kernel'))], |
200
|
|
|
['context.initializer'] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param ContainerBuilder $container |
206
|
|
|
* @param $kernelConfig |
207
|
|
|
*/ |
208
|
|
|
protected function loadSubscriber(ContainerBuilder $container, $kernelConfig) |
209
|
|
|
{ |
210
|
|
|
if (true === $kernelConfig['reboot']) { |
211
|
|
|
$this->createService( |
212
|
|
|
$container, |
213
|
|
|
'subscriber.reboot_kernel', |
214
|
|
|
RebootKernelSubscriber::class, |
215
|
|
|
[new Reference($this->buildContainerId('handler.kernel'))], |
216
|
|
|
[EventDispatcherExtension::SUBSCRIBER_TAG] |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param ContainerBuilder $container |
223
|
|
|
* @param $kernelConfig |
224
|
|
|
*/ |
225
|
|
|
protected function loadContainerParameter(ContainerBuilder $container, $kernelConfig) |
226
|
|
|
{ |
227
|
|
|
foreach ($kernelConfig as $key => $value) { |
228
|
|
|
$container->setParameter($this->buildContainerId(sprintf('kernel.%s', $key)), $value); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|