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