1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BVCR\Behat\ServiceContainer; |
4
|
|
|
|
5
|
|
|
use Behat\Testwork\ServiceContainer\Extension; |
6
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
7
|
|
|
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension; |
8
|
|
|
use Behat\Behat\Context\ServiceContainer\ContextExtension; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
|
14
|
|
|
class VCRExtension implements Extension |
15
|
|
|
{ |
16
|
|
|
const VCR_RECORDER_ID = 'vcr.recorder'; |
17
|
|
|
const VCR_CONFIG_ID = 'vcr.config'; |
18
|
|
|
const VCR_HTTP_CLIENT_ID = 'vcr.http_client'; |
19
|
|
|
const VCR_FACTORY_ID = 'vcr.factory'; |
20
|
|
|
const VCR_SUBSCRIBER_ID = 'vcr.subscriber'; |
21
|
|
|
const VCR_CONTEXT_INITIALIZE_ID = 'vcr.context_initialize'; |
22
|
|
|
const VCR_CONFIGURATION_RESOLVER_ID = 'vcr.configuration_resolver'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
1 |
|
public function getConfigKey() |
28
|
|
|
{ |
29
|
1 |
|
return 'vcr'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function initialize(ExtensionManager $extensionManager) |
36
|
|
|
{ |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function configure(ArrayNodeDefinition $builder) |
43
|
|
|
{ |
44
|
|
|
$normalizeTags = function ($tags) { |
45
|
|
|
return array_map(function ($tag) { |
46
|
1 |
|
return ltrim($tag, '@'); |
47
|
1 |
|
}, $tags); |
48
|
1 |
|
}; |
49
|
|
|
|
50
|
1 |
|
$defineCassetteFilenamingStrategy = function ($item) { |
|
|
|
|
51
|
1 |
|
if (isset($item['use_scenario_name']) && true === $item['use_scenario_name']) { |
52
|
1 |
|
$item['cassette_filenaming_strategy'] = 'by_scenario_name'; |
53
|
1 |
|
} |
54
|
1 |
|
return $item; |
55
|
1 |
|
}; |
56
|
|
|
|
57
|
|
|
$builder |
58
|
1 |
|
->children() |
59
|
1 |
|
->arrayNode('behat_tags') |
60
|
1 |
|
->prototype('array') |
61
|
1 |
|
->beforeNormalization() |
62
|
1 |
|
->always() |
63
|
1 |
|
->then($defineCassetteFilenamingStrategy) |
64
|
1 |
|
->end() |
65
|
1 |
|
->children() |
66
|
1 |
|
->arrayNode('tags') |
67
|
1 |
|
->info('Behat tags to tell VCR to use a cassette for the tagged scenario') |
68
|
1 |
|
->prototype('scalar') |
69
|
1 |
|
->end() |
70
|
1 |
|
->isRequired() |
71
|
1 |
|
->cannotBeEmpty() |
72
|
1 |
|
->beforeNormalization() |
73
|
1 |
|
->always() |
74
|
1 |
|
->then($normalizeTags) |
75
|
1 |
|
->end() |
76
|
1 |
|
->end() |
77
|
1 |
|
->scalarNode('cassette_path') |
78
|
1 |
|
->info('Path where cassette files should be stored') |
79
|
1 |
|
->isRequired() |
80
|
1 |
|
->cannotBeEmpty() |
81
|
1 |
|
->end() |
82
|
1 |
|
->scalarNode('cassette_storage') |
83
|
1 |
|
->info('Format in which is stored the cassette') |
84
|
1 |
|
->isRequired() |
85
|
1 |
|
->cannotBeEmpty() |
86
|
1 |
|
->end() |
87
|
1 |
|
->variableNode('library_hooks') |
88
|
1 |
|
->info('Enable only some library hooks') |
89
|
1 |
|
->end() |
90
|
1 |
|
->variableNode('match_requests_on') |
91
|
1 |
|
->info('Customize how VCR matches requests') |
92
|
1 |
|
->end() |
93
|
1 |
|
->scalarNode('mode') |
94
|
1 |
|
->info('Record mode determines how requests are handled') |
95
|
1 |
|
->end() |
96
|
1 |
|
->booleanNode('use_scenario_name') |
97
|
1 |
|
->info('VCR name the cassettes automatically according to the scenario name') |
98
|
1 |
|
->defaultFalse() |
99
|
1 |
|
->end() |
100
|
1 |
|
->enumNode('cassette_filenaming_strategy') |
101
|
1 |
|
->info('VCR cassette filenaming strategy') |
102
|
1 |
|
->defaultValue('by_tags') |
103
|
1 |
|
->values(array('by_scenario_name', 'by_tags')) |
104
|
1 |
|
->end() |
105
|
1 |
|
->end() |
106
|
1 |
|
->end() |
107
|
1 |
|
->end() |
108
|
1 |
|
->end() |
109
|
1 |
|
->end(); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
1 |
|
public function load(ContainerBuilder $container, array $config) |
116
|
|
|
{ |
117
|
1 |
|
$this->loadEventSubscriber($container); |
118
|
1 |
|
$this->loadVcrConfig($container); |
119
|
1 |
|
$this->loadVcrClient($container); |
120
|
1 |
|
$this->loadVcrFactory($container); |
121
|
1 |
|
$this->loadVcrRecorder($container); |
122
|
1 |
|
$this->loadContextInitializer($container); |
123
|
1 |
|
$this->loadConfigurationResolver($container, $config); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
View Code Duplication |
private function loadEventSubscriber(ContainerBuilder $container) |
|
|
|
|
127
|
|
|
{ |
128
|
1 |
|
$definition = new Definition('BVCR\Behat\EventDispatcher\VCRSubscriber', array( |
129
|
1 |
|
new Reference(self::VCR_RECORDER_ID), |
130
|
1 |
|
new Reference(self::VCR_CONFIGURATION_RESOLVER_ID) |
131
|
1 |
|
)); |
132
|
1 |
|
$definition->addTag(EventDispatcherExtension::SUBSCRIBER_TAG); |
133
|
1 |
|
$container->setDefinition(self::VCR_SUBSCRIBER_ID, $definition); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
private function loadVcrConfig(ContainerBuilder $container) |
137
|
|
|
{ |
138
|
1 |
|
$definition = new Definition('VCR\Configuration'); |
139
|
1 |
|
$container->setDefinition(self::VCR_CONFIG_ID, $definition); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
private function loadVcrClient(ContainerBuilder $container) |
143
|
|
|
{ |
144
|
1 |
|
$definition = new Definition('VCR\Util\HttpClient'); |
145
|
1 |
|
$container->setDefinition(self::VCR_HTTP_CLIENT_ID, $definition); |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
private function loadVcrFactory(ContainerBuilder $container) |
149
|
|
|
{ |
150
|
1 |
|
$definition = new Definition('VCR\VCRFactory', array(new Reference(self::VCR_CONFIG_ID))); |
151
|
1 |
|
if (method_exists($definition, 'setFactory')) { |
152
|
1 |
|
$definition->setFactory(array('VCR\VCRFactory', 'getInstance')); |
153
|
1 |
|
} else { |
154
|
|
|
// to be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
155
|
|
|
$definition->setFactoryClass('VCR\VCRFactory'); |
|
|
|
|
156
|
|
|
$definition->setFactoryMethod('getInstance'); |
|
|
|
|
157
|
|
|
} |
158
|
1 |
|
$container->setDefinition(self::VCR_FACTORY_ID, $definition); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
1 |
|
private function loadVcrRecorder(ContainerBuilder $container) |
162
|
|
|
{ |
163
|
1 |
|
$definition = new Definition( |
164
|
1 |
|
'VCR\Videorecorder', |
165
|
|
|
array( |
166
|
1 |
|
new Reference(self::VCR_CONFIG_ID), |
167
|
1 |
|
new Reference(self::VCR_HTTP_CLIENT_ID), |
168
|
1 |
|
new Reference(self::VCR_FACTORY_ID), |
169
|
|
|
) |
170
|
1 |
|
); |
171
|
1 |
|
$definition->addMethodCall('setEventDispatcher', array(new Reference('event_dispatcher'))); |
172
|
1 |
|
$container->setDefinition(self::VCR_RECORDER_ID, $definition); |
173
|
1 |
|
} |
174
|
|
|
|
175
|
1 |
View Code Duplication |
private function loadContextInitializer(ContainerBuilder $container) |
|
|
|
|
176
|
|
|
{ |
177
|
1 |
|
$definition = new Definition('BVCR\Behat\Context\Initializer\VCRAwareInitializer', array( |
178
|
1 |
|
new Reference(self::VCR_RECORDER_ID) |
179
|
1 |
|
)); |
180
|
1 |
|
$definition->addTag(ContextExtension::INITIALIZER_TAG); |
181
|
1 |
|
$container->setDefinition(self::VCR_CONTEXT_INITIALIZE_ID, $definition); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
1 |
|
private function loadConfigurationResolver(ContainerBuilder $container, $config) |
185
|
|
|
{ |
186
|
1 |
|
$definition = new Definition('BVCR\Behat\Resolver\ConfigurationThroughTagsResolver', array($config)); |
187
|
1 |
|
$container->setDefinition(self::VCR_CONFIGURATION_RESOLVER_ID, $definition); |
188
|
1 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function process(ContainerBuilder $container) |
194
|
|
|
{ |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.