|
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\Dispatcher\BehatKernelEventDispatcher; |
|
16
|
|
|
use Yoanm\Behat3SymfonyExtension\Factory\KernelFactory; |
|
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
|
|
|
// @codeCoverageIgnoreEnd |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function load(ContainerBuilder $container, array $config) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$kernelConfig = $config[$this->getConfigKey()]; |
|
101
|
|
|
|
|
102
|
2 |
|
$this->loadContainerParameter($container, $kernelConfig); |
|
103
|
2 |
|
$this->loadInitializer($container); |
|
104
|
2 |
|
$this->loadSubscriber($container, $kernelConfig); |
|
105
|
2 |
|
$this->createService( |
|
106
|
2 |
|
$container, |
|
107
|
2 |
|
'test.client', |
|
108
|
2 |
|
Client::class, |
|
109
|
|
|
[ |
|
110
|
2 |
|
new Reference(self::KERNEL_SERVICE_ID), |
|
111
|
2 |
|
[], |
|
112
|
2 |
|
new Reference($this->buildContainerId('test.client.history')), |
|
113
|
2 |
|
new Reference($this->buildContainerId('test.client.cookiejar')) |
|
114
|
2 |
|
] |
|
115
|
2 |
|
); |
|
116
|
2 |
|
$this->createService( |
|
117
|
2 |
|
$container, |
|
118
|
2 |
|
'test.client.history', |
|
119
|
|
|
History::class |
|
120
|
2 |
|
); |
|
121
|
2 |
|
$this->createService( |
|
122
|
2 |
|
$container, |
|
123
|
2 |
|
'test.client.cookiejar', |
|
124
|
|
|
CookieJar::class |
|
125
|
2 |
|
); |
|
126
|
2 |
|
$this->createService( |
|
127
|
2 |
|
$container, |
|
128
|
2 |
|
'dispatcher.kernel_event', |
|
129
|
2 |
|
BehatKernelEventDispatcher::class, |
|
130
|
2 |
|
[new Reference('event_dispatcher')] |
|
131
|
2 |
|
); |
|
132
|
|
|
// Load Kernel thanks to the factory |
|
133
|
2 |
|
$this->createService( |
|
134
|
2 |
|
$container, |
|
135
|
2 |
|
'kernel', |
|
136
|
2 |
|
$kernelConfig['class'], |
|
137
|
2 |
|
[], |
|
138
|
2 |
|
[], |
|
139
|
2 |
|
[], |
|
140
|
2 |
|
[new Reference($this->buildContainerId('factory.kernel')), 'load'] |
|
141
|
2 |
|
); |
|
142
|
|
|
|
|
143
|
2 |
|
$this->createService( |
|
144
|
2 |
|
$container, |
|
145
|
2 |
|
'factory.kernel', |
|
146
|
2 |
|
KernelFactory::class, |
|
147
|
|
|
[ |
|
148
|
2 |
|
new Reference($this->buildContainerId('dispatcher.kernel_event')), |
|
149
|
2 |
|
'%'.$this->buildContainerId('kernel.path').'%', |
|
150
|
2 |
|
'%'.$this->buildContainerId('kernel.class').'%', |
|
151
|
2 |
|
'%'.$this->buildContainerId('kernel.env').'%', |
|
152
|
2 |
|
'%'.$this->buildContainerId('kernel.debug').'%' |
|
153
|
2 |
|
] |
|
154
|
2 |
|
); |
|
155
|
2 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function process(ContainerBuilder $container) |
|
161
|
|
|
{ |
|
162
|
3 |
|
$basePath = $container->getParameter('paths.base'); |
|
163
|
3 |
|
$bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap')); |
|
164
|
3 |
|
if ($bootstrapPath) { |
|
165
|
2 |
|
$bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath); |
|
166
|
2 |
|
if (file_exists($bootstrapPathUnderBasePath)) { |
|
167
|
1 |
|
$bootstrapPath = $bootstrapPathUnderBasePath; |
|
168
|
1 |
|
} |
|
169
|
2 |
|
if (file_exists($bootstrapPath)) { |
|
170
|
1 |
|
require_once($bootstrapPath); |
|
171
|
1 |
|
} else { |
|
172
|
1 |
|
throw new ProcessingException('Could not find bootstrap file !'); |
|
173
|
|
|
} |
|
174
|
1 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
// load kernel |
|
177
|
2 |
|
$kernelPath = $container->getParameter($this->buildContainerId('kernel.path')); |
|
178
|
2 |
|
$kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath); |
|
179
|
2 |
|
if (file_exists($kernelPathUnderBasePath)) { |
|
180
|
|
|
$kernelPath = $kernelPathUnderBasePath; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
$container->getDefinition(self::KERNEL_SERVICE_ID) |
|
184
|
2 |
|
->setFile($kernelPath); |
|
185
|
2 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param ContainerBuilder $container |
|
189
|
|
|
*/ |
|
190
|
2 |
|
protected function loadInitializer(ContainerBuilder $container) |
|
191
|
|
|
{ |
|
192
|
2 |
|
$this->createService( |
|
193
|
2 |
|
$container, |
|
194
|
2 |
|
'initializer.kernel_aware', |
|
195
|
2 |
|
KernelAwareInitializer::class, |
|
196
|
2 |
|
[new Reference(self::KERNEL_SERVICE_ID)], |
|
197
|
2 |
|
['context.initializer'] |
|
198
|
2 |
|
); |
|
199
|
2 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param ContainerBuilder $container |
|
203
|
|
|
* @param $kernelConfig |
|
204
|
|
|
*/ |
|
205
|
2 |
|
protected function loadSubscriber(ContainerBuilder $container, $kernelConfig) |
|
206
|
|
|
{ |
|
207
|
2 |
|
if (true === $kernelConfig['reboot']) { |
|
208
|
1 |
|
$this->createService( |
|
209
|
1 |
|
$container, |
|
210
|
1 |
|
'subscriber.reboot_kernel', |
|
211
|
1 |
|
RebootKernelSubscriber::class, |
|
212
|
1 |
|
[new Reference(self::KERNEL_SERVICE_ID)], |
|
213
|
1 |
|
[EventDispatcherExtension::SUBSCRIBER_TAG] |
|
214
|
1 |
|
); |
|
215
|
1 |
|
} |
|
216
|
2 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param ContainerBuilder $container |
|
220
|
|
|
* @param $kernelConfig |
|
221
|
|
|
*/ |
|
222
|
2 |
|
protected function loadContainerParameter(ContainerBuilder $container, $kernelConfig) |
|
223
|
|
|
{ |
|
224
|
2 |
|
foreach ($kernelConfig as $key => $value) { |
|
225
|
2 |
|
$container->setParameter($this->buildContainerId(sprintf('kernel.%s', $key)), $value); |
|
226
|
2 |
|
} |
|
227
|
2 |
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|