1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Http\HttplugBundle\DependencyInjection\Configuration; |
6
|
|
|
use Http\HttplugBundle\DependencyInjection\HttplugExtension; |
7
|
|
|
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author David Buchmann <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ConfigurationTest extends AbstractExtensionConfigurationTestCase |
13
|
|
|
{ |
14
|
|
|
private $emptyConfig = [ |
15
|
|
|
'main_alias' => [ |
16
|
|
|
'client' => 'httplug.client.default', |
17
|
|
|
'message_factory' => 'httplug.message_factory.default', |
18
|
|
|
'uri_factory' => 'httplug.uri_factory.default', |
19
|
|
|
'stream_factory' => 'httplug.stream_factory.default', |
20
|
|
|
], |
21
|
|
|
'classes' => [ |
22
|
|
|
'client' => null, |
23
|
|
|
'message_factory' => null, |
24
|
|
|
'uri_factory' => null, |
25
|
|
|
'stream_factory' => null, |
26
|
|
|
], |
27
|
|
|
'clients' => [], |
28
|
|
|
'profiling' => [ |
29
|
|
|
'enabled' => true, |
30
|
|
|
'formatter' => null, |
31
|
|
|
'captured_body_length' => 0, |
32
|
|
|
], |
33
|
|
|
'plugins' => [ |
34
|
|
|
'authentication' => [], |
35
|
|
|
'cache' => [ |
36
|
|
|
'enabled' => false, |
37
|
|
|
'stream_factory' => 'httplug.stream_factory', |
38
|
|
|
'config' => [ |
39
|
|
|
'methods' => ['GET', 'HEAD'], |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
'cookie' => [ |
43
|
|
|
'enabled' => false, |
44
|
|
|
], |
45
|
|
|
'decoder' => [ |
46
|
|
|
'enabled' => true, |
47
|
|
|
'use_content_encoding' => true, |
48
|
|
|
], |
49
|
|
|
'history' => [ |
50
|
|
|
'enabled' => false, |
51
|
|
|
], |
52
|
|
|
'logger' => [ |
53
|
|
|
'enabled' => true, |
54
|
|
|
'logger' => 'logger', |
55
|
|
|
'formatter' => null, |
56
|
|
|
], |
57
|
|
|
'redirect' => [ |
58
|
|
|
'enabled' => true, |
59
|
|
|
'preserve_header' => true, |
60
|
|
|
'use_default_for_multiple' => true, |
61
|
|
|
], |
62
|
|
|
'retry' => [ |
63
|
|
|
'enabled' => true, |
64
|
|
|
'retry' => 1, |
65
|
|
|
], |
66
|
|
|
'stopwatch' => [ |
67
|
|
|
'enabled' => true, |
68
|
|
|
'stopwatch' => 'debug.stopwatch', |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
'discovery' => [ |
72
|
|
|
'client' => 'auto', |
73
|
|
|
'async_client' => null, |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
protected function getContainerExtension() |
78
|
|
|
{ |
79
|
|
|
return new HttplugExtension(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getConfiguration() |
83
|
|
|
{ |
84
|
|
|
return new Configuration(true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function testEmptyConfiguration() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$formats = array_map(function ($path) { |
90
|
|
|
return __DIR__.'/../../Resources/Fixtures/'.$path; |
91
|
|
|
}, [ |
92
|
|
|
'config/empty.yml', |
93
|
|
|
'config/empty.xml', |
94
|
|
|
'config/empty.php', |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
foreach ($formats as $format) { |
98
|
|
|
$this->assertProcessedConfigurationEquals($this->emptyConfig, [$format]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testSupportsAllConfigFormats() |
103
|
|
|
{ |
104
|
|
|
$expectedConfiguration = [ |
105
|
|
|
'main_alias' => [ |
106
|
|
|
'client' => 'my_client', |
107
|
|
|
'message_factory' => 'my_message_factory', |
108
|
|
|
'uri_factory' => 'my_uri_factory', |
109
|
|
|
'stream_factory' => 'my_stream_factory', |
110
|
|
|
], |
111
|
|
|
'classes' => [ |
112
|
|
|
'client' => 'Http\Adapter\Guzzle6\Client', |
113
|
|
|
'message_factory' => 'Http\Message\MessageFactory\GuzzleMessageFactory', |
114
|
|
|
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory', |
115
|
|
|
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory', |
116
|
|
|
], |
117
|
|
|
'clients' => [ |
118
|
|
|
'test' => [ |
119
|
|
|
'factory' => 'httplug.factory.guzzle6', |
120
|
|
|
'http_methods_client' => true, |
121
|
|
|
'service' => null, |
122
|
|
|
'public' => null, |
123
|
|
|
'flexible_client' => false, |
124
|
|
|
'batch_client' => false, |
125
|
|
|
'plugins' => [ |
126
|
|
|
[ |
127
|
|
|
'reference' => [ |
128
|
|
|
'enabled' => true, |
129
|
|
|
'id' => 'httplug.plugin.redirect', |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
[ |
133
|
|
|
'add_host' => [ |
134
|
|
|
'enabled' => true, |
135
|
|
|
'host' => 'http://localhost', |
136
|
|
|
'replace' => false, |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
'add_path' => [ |
141
|
|
|
'enabled' => true, |
142
|
|
|
'path' => '/api/v1', |
143
|
|
|
], |
144
|
|
|
], |
145
|
|
|
[ |
146
|
|
|
'base_uri' => [ |
147
|
|
|
'enabled' => true, |
148
|
|
|
'uri' => 'http://localhost', |
149
|
|
|
'replace' => false, |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
[ |
153
|
|
|
'header_set' => [ |
154
|
|
|
'enabled' => true, |
155
|
|
|
'headers' => [ |
156
|
|
|
'X-FOO' => 'bar', |
157
|
|
|
], |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
[ |
161
|
|
|
'header_remove' => [ |
162
|
|
|
'enabled' => true, |
163
|
|
|
'headers' => [ |
164
|
|
|
'X-FOO', |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
[ |
169
|
|
|
'authentication' => [ |
170
|
|
|
'my_basic' => [ |
171
|
|
|
'type' => 'basic', |
172
|
|
|
'username' => 'foo', |
173
|
|
|
'password' => 'bar', |
174
|
|
|
'params' => [], |
175
|
|
|
], |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
'config' => [], |
180
|
|
|
], |
181
|
|
|
], |
182
|
|
|
'profiling' => [ |
183
|
|
|
'enabled' => true, |
184
|
|
|
'formatter' => 'my_toolbar_formatter', |
185
|
|
|
'captured_body_length' => 0, |
186
|
|
|
], |
187
|
|
|
'plugins' => [ |
188
|
|
|
'authentication' => [ |
189
|
|
|
'my_basic' => [ |
190
|
|
|
'type' => 'basic', |
191
|
|
|
'username' => 'foo', |
192
|
|
|
'password' => 'bar', |
193
|
|
|
'params' => [], |
194
|
|
|
], |
195
|
|
|
'my_wsse' => [ |
196
|
|
|
'type' => 'wsse', |
197
|
|
|
'username' => 'foo', |
198
|
|
|
'password' => 'bar', |
199
|
|
|
'params' => [], |
200
|
|
|
], |
201
|
|
|
'my_bearer' => [ |
202
|
|
|
'type' => 'bearer', |
203
|
|
|
'token' => 'foo', |
204
|
|
|
'params' => [], |
205
|
|
|
], |
206
|
|
|
'my_service' => [ |
207
|
|
|
'type' => 'service', |
208
|
|
|
'service' => 'my_auth_service', |
209
|
|
|
'params' => [], |
210
|
|
|
], |
211
|
|
|
], |
212
|
|
|
'cache' => [ |
213
|
|
|
'enabled' => true, |
214
|
|
|
'cache_pool' => 'my_cache_pool', |
215
|
|
|
'stream_factory' => 'my_other_stream_factory', |
216
|
|
|
'config' => [ |
217
|
|
|
'cache_lifetime' => 2592000, |
218
|
|
|
'default_ttl' => 42, |
219
|
|
|
'hash_algo' => 'sha1', |
220
|
|
|
'methods' => ['GET'], |
221
|
|
|
'cache_key_generator' => null, |
222
|
|
|
'respect_response_cache_directives' => ['X-Foo'], |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
'cookie' => [ |
226
|
|
|
'enabled' => true, |
227
|
|
|
'cookie_jar' => 'my_cookie_jar', |
228
|
|
|
], |
229
|
|
|
'decoder' => [ |
230
|
|
|
'enabled' => false, |
231
|
|
|
'use_content_encoding' => true, |
232
|
|
|
], |
233
|
|
|
'history' => [ |
234
|
|
|
'enabled' => true, |
235
|
|
|
'journal' => 'my_journal', |
236
|
|
|
], |
237
|
|
|
'logger' => [ |
238
|
|
|
'enabled' => false, |
239
|
|
|
'logger' => 'logger', |
240
|
|
|
'formatter' => null, |
241
|
|
|
], |
242
|
|
|
'redirect' => [ |
243
|
|
|
'enabled' => false, |
244
|
|
|
'preserve_header' => true, |
245
|
|
|
'use_default_for_multiple' => true, |
246
|
|
|
], |
247
|
|
|
'retry' => [ |
248
|
|
|
'enabled' => false, |
249
|
|
|
'retry' => 1, |
250
|
|
|
], |
251
|
|
|
'stopwatch' => [ |
252
|
|
|
'enabled' => false, |
253
|
|
|
'stopwatch' => 'debug.stopwatch', |
254
|
|
|
], |
255
|
|
|
], |
256
|
|
|
'discovery' => [ |
257
|
|
|
'client' => 'auto', |
258
|
|
|
'async_client' => null, |
259
|
|
|
], |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
$formats = array_map(function ($path) { |
263
|
|
|
return __DIR__.'/../../Resources/Fixtures/'.$path; |
264
|
|
|
}, [ |
265
|
|
|
'config/full.yml', |
266
|
|
|
'config/full.xml', |
267
|
|
|
'config/full.php', |
268
|
|
|
]); |
269
|
|
|
|
270
|
|
|
foreach ($formats as $format) { |
271
|
|
|
$this->assertProcessedConfigurationEquals($expectedConfiguration, [$format]); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
277
|
|
|
* @expectedExceptionMessage Nonexisting\Class |
278
|
|
|
*/ |
279
|
|
|
public function testMissingClass() |
280
|
|
|
{ |
281
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_class.yml'; |
282
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
287
|
|
|
* @expectedExceptionMessage Unrecognized option "foobar" under "httplug.clients.acme.plugins.0" |
288
|
|
|
*/ |
289
|
|
|
public function testInvalidPlugin() |
290
|
|
|
{ |
291
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_plugin.yml'; |
292
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
297
|
|
|
* @expectedExceptionMessage password, service, username |
298
|
|
|
*/ |
299
|
|
|
public function testInvalidAuthentication() |
300
|
|
|
{ |
301
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_auth.yml'; |
302
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @group legacy |
307
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
308
|
|
|
* @expectedExceptionMessage Invalid configuration for path "httplug.plugins.cache.config": You can't provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead. |
309
|
|
|
*/ |
310
|
|
|
public function testInvalidCacheConfig() |
311
|
|
|
{ |
312
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_config.yml'; |
313
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @group legacy |
318
|
|
|
*/ |
319
|
|
View Code Duplication |
public function testBackwardCompatibility() |
|
|
|
|
320
|
|
|
{ |
321
|
|
|
$formats = array_map(function ($path) { |
322
|
|
|
return __DIR__.'/../../Resources/Fixtures/'.$path; |
323
|
|
|
}, [ |
324
|
|
|
'config/bc/toolbar.yml', |
325
|
|
|
'config/bc/toolbar_auto.yml', |
326
|
|
|
]); |
327
|
|
|
|
328
|
|
|
foreach ($formats as $format) { |
329
|
|
|
$this->assertProcessedConfigurationEquals($this->emptyConfig, [$format]); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @group legacy |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function testCacheConfigDeprecationCompatibility() |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/bc/cache_config.yml'; |
339
|
|
|
$config = $this->emptyConfig; |
340
|
|
|
$config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ |
341
|
|
|
'enabled' => true, |
342
|
|
|
'cache_pool' => 'my_cache_pool', |
343
|
|
|
'config' => [ |
344
|
|
|
'methods' => ['GET', 'HEAD'], |
345
|
|
|
'respect_cache_headers' => true, |
346
|
|
|
], |
347
|
|
|
]); |
348
|
|
|
$this->assertProcessedConfigurationEquals($config, [$file]); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @group legacy |
353
|
|
|
*/ |
354
|
|
View Code Duplication |
public function testCacheConfigDeprecationCompatibilityIssue166() |
|
|
|
|
355
|
|
|
{ |
356
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/bc/issue-166.yml'; |
357
|
|
|
$config = $this->emptyConfig; |
358
|
|
|
$config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ |
359
|
|
|
'enabled' => true, |
360
|
|
|
'cache_pool' => 'my_cache_pool', |
361
|
|
|
'config' => [ |
362
|
|
|
'methods' => ['GET', 'HEAD'], |
363
|
|
|
'respect_cache_headers' => false, |
364
|
|
|
], |
365
|
|
|
]); |
366
|
|
|
$this->assertProcessedConfigurationEquals($config, [$file]); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
371
|
|
|
* @expectedExceptionMessage Can't configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling". |
372
|
|
|
*/ |
373
|
|
|
public function testProfilingToolbarCollision() |
374
|
|
|
{ |
375
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/bc/profiling_toolbar.yml'; |
376
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
381
|
|
|
* @expectedExceptionMessage The child node "cache_pool" at path "httplug.clients.test.plugins.0.cache" must be configured. |
382
|
|
|
*/ |
383
|
|
|
public function testClientCacheConfigMustHavePool() |
384
|
|
|
{ |
385
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/client_cache_config_with_no_pool.yml'; |
386
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
391
|
|
|
* @expectedExceptionMessage The child node "cache_pool" at path "httplug.plugins.cache" must be configured. |
392
|
|
|
*/ |
393
|
|
|
public function testCacheConfigMustHavePool() |
394
|
|
|
{ |
395
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/cache_config_with_no_pool.yml'; |
396
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
public function testLimitlessCapturedBodyLength() |
400
|
|
|
{ |
401
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/limitless_captured_body_length.yml'; |
402
|
|
|
$config = $this->emptyConfig; |
403
|
|
|
$config['profiling']['captured_body_length'] = null; |
404
|
|
|
$this->assertProcessedConfigurationEquals($config, [$file]); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
409
|
|
|
* @expectedExceptionMessage The child node "captured_body_length" at path "httplug.profiling" must be an integer or null. |
410
|
|
|
*/ |
411
|
|
|
public function testInvalidCapturedBodyLengthString() |
412
|
|
|
{ |
413
|
|
|
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_captured_body_length.yml'; |
414
|
|
|
$this->assertProcessedConfigurationEquals([], [$file]); |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.