1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer; |
3
|
|
|
|
4
|
|
|
use Behat\MinkExtension\ServiceContainer\MinkExtension; |
5
|
|
|
use Behat\Testwork\ServiceContainer\Extension; |
6
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
9
|
|
|
use Symfony\Component\Config\FileLocator; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
12
|
|
|
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\KernelConfiguration; |
13
|
|
|
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory; |
14
|
|
|
|
15
|
|
|
class Behat3SymfonyExtension implements Extension |
16
|
|
|
{ |
17
|
|
|
const TEST_CLIENT_SERVICE_ID = 'behat3_symfony_extension.test.client'; |
18
|
|
|
const KERNEL_SERVICE_ID = 'behat3_symfony_extension.kernel'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
1 |
|
public function getConfigKey() |
24
|
|
|
{ |
25
|
1 |
|
return 'behat3_symfony'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// @codeCoverageIgnoreStart |
29
|
|
|
/** |
30
|
|
|
* (Not possible to cover this because ExtensionManager is a final class) |
31
|
|
|
* |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function initialize(ExtensionManager $extensionManager) |
35
|
|
|
{ |
36
|
|
|
$minExtension = $extensionManager->getExtension('mink'); |
37
|
|
|
if ($minExtension instanceof MinkExtension) { |
38
|
|
|
$minExtension->registerDriverFactory(new Behat3SymfonyFactory()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
// @codeCoverageIgnoreEnd |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function configure(ArrayNodeDefinition $builder) |
47
|
|
|
{ |
48
|
|
|
$castToBool = function ($value) { |
49
|
|
|
$filtered = filter_var( |
50
|
|
|
$value, |
51
|
|
|
FILTER_VALIDATE_BOOLEAN, |
52
|
|
|
FILTER_NULL_ON_FAILURE |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
return (null === $filtered) ? (bool) $value : $filtered; |
56
|
|
|
}; |
57
|
|
|
$builder->children() |
58
|
|
|
->booleanNode('debug_mode') |
59
|
|
|
->beforeNormalization() |
60
|
|
|
->always() |
61
|
|
|
->then($castToBool) |
62
|
|
|
->end() |
63
|
|
|
->defaultFalse() |
64
|
|
|
->end() |
65
|
|
|
->end(); |
66
|
|
|
$builder->append((new KernelConfiguration())->getConfigTreeBuilder()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function load(ContainerBuilder $container, array $config) |
73
|
|
|
{ |
74
|
|
|
$config = $this->normalizeConfig($config); |
75
|
|
|
$this->bindConfigToContainer($container, $config); |
76
|
|
|
|
77
|
|
|
$loader = new XmlFileLoader( |
78
|
|
|
$container, |
79
|
|
|
new FileLocator(__DIR__.'/../Resources/config') |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$loader->load('client.xml'); |
83
|
|
|
$loader->load('kernel.xml'); |
84
|
|
|
$loader->load('initializer.xml'); |
85
|
|
|
if (true === $config['kernel']['reboot']) { |
86
|
|
|
$loader->load('kernel_auto_reboot.xml'); |
87
|
|
|
} |
88
|
|
|
if (true === $config['kernel']['debug']) { |
89
|
|
|
$loader->load('kernel_debug_mode.xml'); |
90
|
|
|
|
91
|
|
|
// Override log level parameter |
92
|
|
|
$this->checkUtilsExtensionAlreadyLoaded($container); |
93
|
|
|
$container->setParameter('behat_utils_extension.logger.level', Logger::DEBUG); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
3 |
|
public function process(ContainerBuilder $container) |
101
|
|
|
{ |
102
|
3 |
|
$bootstrapPath = $container->getParameter('behat3_symfony_extension.kernel.bootstrap'); |
103
|
3 |
|
if ($bootstrapPath) { |
104
|
2 |
|
require_once($this->normalizePath($container, $bootstrapPath)); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
// load kernel |
108
|
3 |
|
$container->getDefinition(self::KERNEL_SERVICE_ID) |
109
|
3 |
|
->setFile( |
110
|
3 |
|
$this->normalizePath( |
111
|
3 |
|
$container, |
112
|
3 |
|
$container->getParameter('behat3_symfony_extension.kernel.path') |
113
|
3 |
|
) |
114
|
3 |
|
); |
115
|
3 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ContainerBuilder $container |
119
|
|
|
* @param string $path |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
3 |
|
protected function normalizePath(ContainerBuilder $container, $path) |
124
|
|
|
{ |
125
|
3 |
|
$basePath = $container->getParameter('paths.base'); |
126
|
3 |
|
$pathUnderBasePath = sprintf('%s/%s', $basePath, $path); |
127
|
3 |
|
if (file_exists($pathUnderBasePath)) { |
128
|
2 |
|
$path = $pathUnderBasePath; |
129
|
2 |
|
} |
130
|
|
|
|
131
|
3 |
|
return $path; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param ContainerBuilder $container |
136
|
|
|
* @param array $config |
137
|
|
|
* @param string $baseId |
138
|
|
|
*/ |
139
|
|
|
protected function bindConfigToContainer( |
140
|
|
|
ContainerBuilder $container, |
141
|
|
|
array $config, |
142
|
|
|
$baseId = 'behat3_symfony_extension' |
143
|
|
|
) { |
144
|
|
|
foreach ($config as $configKey => $configValue) { |
145
|
|
|
if (is_array($configValue)) { |
146
|
|
|
$this->bindConfigToContainer( |
147
|
|
|
$container, |
148
|
|
|
$configValue, |
149
|
|
|
sprintf('%s.%s', $baseId, $configKey) |
150
|
|
|
); |
151
|
|
|
} else { |
152
|
|
|
$container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $config |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function normalizeConfig(array $config) |
162
|
|
|
{ |
163
|
|
|
if (true === $config['debug_mode']) { |
164
|
|
|
$config['kernel']['debug'] = true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $config; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param ContainerBuilder $container |
172
|
|
|
* @throws \Exception |
173
|
|
|
*/ |
174
|
|
|
protected function checkUtilsExtensionAlreadyLoaded(ContainerBuilder $container) |
175
|
|
|
{ |
176
|
|
|
if (!$container->hasParameter('behat_utils_extension.logger.path')) { |
177
|
|
|
throw new \Exception('BehatUtilsExtension must be loaded before this one !'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|