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\Configuration\LoggerConfiguration; |
14
|
|
|
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory; |
15
|
|
|
|
16
|
|
|
class Behat3SymfonyExtension implements Extension |
17
|
|
|
{ |
18
|
|
|
const TEST_CLIENT_SERVICE_ID = 'behat3_symfony_extension.test.client'; |
19
|
|
|
const KERNEL_SERVICE_ID = 'behat3_symfony_extension.kernel'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
1 |
|
public function getConfigKey() |
25
|
|
|
{ |
26
|
1 |
|
return 'behat3_symfony'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// @codeCoverageIgnoreStart |
30
|
|
|
/** |
31
|
|
|
* (Not possible to cover this because ExtensionManager is a final class) |
32
|
|
|
* |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function initialize(ExtensionManager $extensionManager) |
36
|
|
|
{ |
37
|
|
|
$minExtension = $extensionManager->getExtension('mink'); |
38
|
|
|
if ($minExtension instanceof MinkExtension) { |
39
|
|
|
$minExtension->registerDriverFactory(new Behat3SymfonyFactory()); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* (Will be covered by Functional tests) |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function configure(ArrayNodeDefinition $builder) |
48
|
|
|
{ |
49
|
|
|
$castToBool = function ($value) { |
50
|
|
|
$filtered = filter_var( |
51
|
|
|
$value, |
52
|
|
|
FILTER_VALIDATE_BOOLEAN, |
53
|
|
|
FILTER_NULL_ON_FAILURE |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
return (null === $filtered) ? (bool) $value : $filtered; |
57
|
|
|
}; |
58
|
|
|
$builder->children() |
59
|
|
|
->booleanNode('debug_mode') |
60
|
|
|
->beforeNormalization() |
61
|
|
|
->always() |
62
|
|
|
->then($castToBool) |
63
|
|
|
->end() |
64
|
|
|
->defaultFalse() |
65
|
|
|
->end() |
66
|
|
|
->end(); |
67
|
|
|
$builder->append((new KernelConfiguration())->getConfigTreeBuilder()); |
68
|
|
|
$builder->append((new LoggerConfiguration())->getConfigTreeBuilder()); |
69
|
|
|
} |
70
|
|
|
// @codeCoverageIgnoreEnd |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
4 |
|
public function load(ContainerBuilder $container, array $config) |
76
|
|
|
{ |
77
|
4 |
|
$config = $this->normalizeConfig($config); |
78
|
4 |
|
$this->bindConfigToContainer($container, $config); |
79
|
|
|
|
80
|
4 |
|
$loader = new XmlFileLoader( |
81
|
4 |
|
$container, |
82
|
4 |
|
new FileLocator(__DIR__.'/../Resources/config') |
83
|
4 |
|
); |
84
|
|
|
|
85
|
4 |
|
$loader->load('client.xml'); |
86
|
4 |
|
$loader->load('kernel.xml'); |
87
|
4 |
|
$loader->load('initializer.xml'); |
88
|
4 |
|
$loader->load('logger.xml'); |
89
|
4 |
|
if (true === $config['kernel']['reboot']) { |
90
|
2 |
|
$loader->load('kernel_auto_reboot.xml'); |
91
|
2 |
|
} |
92
|
4 |
|
if (true === $config['kernel']['debug']) { |
93
|
2 |
|
$loader->load('kernel_debug_mode.xml'); |
94
|
2 |
|
} |
95
|
4 |
|
} |
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
|
4 |
|
protected function bindConfigToContainer( |
140
|
|
|
ContainerBuilder $container, |
141
|
|
|
array $config, |
142
|
|
|
$baseId = 'behat3_symfony_extension' |
143
|
|
|
) { |
144
|
4 |
|
foreach ($config as $configKey => $configValue) { |
145
|
4 |
|
if (is_array($configValue)) { |
146
|
4 |
|
$this->bindConfigToContainer( |
147
|
4 |
|
$container, |
148
|
4 |
|
$configValue, |
149
|
4 |
|
sprintf('%s.%s', $baseId, $configKey) |
150
|
4 |
|
); |
151
|
4 |
|
} else { |
152
|
4 |
|
$container->setParameter(sprintf('%s.%s', $baseId, $configKey), $configValue); |
153
|
|
|
} |
154
|
4 |
|
} |
155
|
4 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $config |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
4 |
|
protected function normalizeConfig(array $config) |
162
|
|
|
{ |
163
|
4 |
|
if (true === $config['debug_mode']) { |
164
|
2 |
|
$config['kernel']['debug'] = true; |
165
|
2 |
|
$config['logger']['level'] = Logger::DEBUG; |
166
|
2 |
|
} |
167
|
|
|
|
168
|
4 |
|
return $config; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|