Completed
Push — master ( be11c5...35d281 )
by David
06:05 queued 11s
created

testContentTypePluginAllowedOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;
4
5
use Http\Client\HttpClient;
6
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
7
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
8
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpKernel\Kernel;
11
12
/**
13
 * @author David Buchmann <[email protected]>
14
 * @author Tobias Nyholm <[email protected]>
15
 */
16
class HttplugExtensionTest extends AbstractExtensionTestCase
17
{
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->setParameter('kernel.debug', true);
23
    }
24
25
    protected function getContainerExtensions()
26
    {
27
        return [
28
            new HttplugExtension(),
29
        ];
30
    }
31
32
    public function testConfigLoadDefault()
33
    {
34
        $this->load();
35
36 View Code Duplication
        foreach (['client', 'message_factory', 'uri_factory', 'stream_factory'] as $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
37
            $this->assertContainerBuilderHasAlias("httplug.$type", "httplug.$type.default");
38
        }
39
    }
40
41
    public function testConfigLoadClass()
42
    {
43
        $this->load([
44
            'classes' => [
45
                'client' => 'Http\Adapter\Guzzle6\Client',
46
            ],
47
        ]);
48
49
        $this->assertContainerBuilderHasService('httplug.client.default', 'Http\Adapter\Guzzle6\Client');
50
    }
51
52
    public function testConfigLoadService()
53
    {
54
        $this->load([
55
            'main_alias' => [
56
                'client' => 'my_client_service',
57
                'message_factory' => 'my_message_factory_service',
58
                'uri_factory' => 'my_uri_factory_service',
59
                'stream_factory' => 'my_stream_factory_service',
60
            ],
61
        ]);
62
63 View Code Duplication
        foreach (['client', 'message_factory', 'uri_factory', 'stream_factory'] as $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
64
            $this->assertContainerBuilderHasAlias("httplug.$type", "my_{$type}_service");
65
        }
66
    }
67
68
    public function testClientPlugins()
69
    {
70
        $this->load([
71
            'clients' => [
72
                'acme' => [
73
                    'factory' => 'httplug.factory.curl',
74
                    'plugins' => [
75
                        [
76
                            'decoder' => [
77
                                'use_content_encoding' => false,
78
                            ],
79
                        ],
80
                        'httplug.plugin.redirect',
81
                        [
82
                            'add_host' => [
83
                                'host' => 'http://localhost:8000',
84
                            ],
85
                        ],
86
                        [
87
                            'content_type' => [
88
                                'skip_detection' => true,
89
                            ],
90
                        ],
91
                        [
92
                            'header_append' => [
93
                                'headers' => ['X-FOO' => 'bar'],
94
                            ],
95
                        ],
96
                        [
97
                            'header_defaults' => [
98
                                'headers' => ['X-FOO' => 'bar'],
99
                            ],
100
                        ],
101
                        [
102
                            'header_set' => [
103
                                'headers' => ['X-FOO' => 'bar'],
104
                            ],
105
                        ],
106
                        [
107
                            'header_remove' => [
108
                                'headers' => ['X-FOO'],
109
                            ],
110
                        ],
111
                        [
112
                            'query_defaults' => [
113
                                'parameters' => ['locale' => 'en'],
114
                            ],
115
                        ],
116
                        [
117
                            'authentication' => [
118
                                'my_basic' => [
119
                                    'type' => 'basic',
120
                                    'username' => 'foo',
121
                                    'password' => 'bar',
122
                                ],
123
                            ],
124
                        ],
125
                        [
126
                            'cache' => [
127
                                'cache_pool' => 'my_cache_pool',
128
                            ],
129
                        ],
130
                    ],
131
                ],
132
            ],
133
        ]);
134
135
        $plugins = [
136
            'httplug.client.acme.plugin.decoder',
137
            'httplug.plugin.redirect',
138
            'httplug.client.acme.plugin.add_host',
139
            'httplug.client.acme.plugin.content_type',
140
            'httplug.client.acme.plugin.header_append',
141
            'httplug.client.acme.plugin.header_defaults',
142
            'httplug.client.acme.plugin.header_set',
143
            'httplug.client.acme.plugin.header_remove',
144
            'httplug.client.acme.plugin.query_defaults',
145
            'httplug.client.acme.authentication.my_basic',
146
            'httplug.client.acme.plugin.cache',
147
        ];
148
        $pluginReferences = array_map(function ($id) {
149
            return new Reference($id);
150
        }, $plugins);
151
152
        $this->assertContainerBuilderHasService('httplug.client.acme');
153
        foreach ($plugins as $id) {
154
            $this->assertContainerBuilderHasService($id);
155
        }
156
        $this->assertContainerBuilderHasServiceDefinitionWithArgument('httplug.client.acme', 1, $pluginReferences);
157
        $this->assertContainerBuilderHasService('httplug.client.mock');
158
    }
159
160
    /**
161
     * @group legacy
162
     */
163 View Code Duplication
    public function testNoProfilingWhenToolbarIsDisabled()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
164
    {
165
        $this->load(
166
            [
167
                'toolbar' => [
168
                    'enabled' => false,
169
                ],
170
                'clients' => [
171
                    'acme' => [
172
                        'factory' => 'httplug.factory.curl',
173
                        'plugins' => ['foo'],
174
                    ],
175
                ],
176
            ]
177
        );
178
179
        $this->verifyProfilingDisabled();
180
    }
181
182 View Code Duplication
    public function testNoProfilingWhenNotInDebugMode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
183
    {
184
        $this->setParameter('kernel.debug', false);
185
        $this->load(
186
            [
187
                'clients' => [
188
                    'acme' => [
189
                        'factory' => 'httplug.factory.curl',
190
                        'plugins' => ['foo'],
191
                    ],
192
                ],
193
            ]
194
        );
195
196
        $this->verifyProfilingDisabled();
197
    }
198
199
    /**
200
     * @group legacy
201
     */
202 View Code Duplication
    public function testProfilingWhenToolbarIsSpecificallyOn()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
203
    {
204
        $this->setParameter('kernel.debug', false);
205
        $this->load(
206
            [
207
                'toolbar' => [
208
                    'enabled' => true,
209
                ],
210
                'clients' => [
211
                    'acme' => [
212
                        'factory' => 'httplug.factory.curl',
213
                        'plugins' => ['foo'],
214
                    ],
215
                ],
216
            ]
217
        );
218
219
        $this->assertContainerBuilderHasService(PluginClientFactoryListener::class);
220
    }
221
222
    public function testOverrideProfillingFormatter()
223
    {
224
        $this->load(
225
            [
226
                'profiling' => [
227
                    'formatter' => 'acme.formatter',
228
                ],
229
            ]
230
        );
231
232
        $def = $this->container->findDefinition('httplug.collector.formatter');
233
        $this->assertEquals('acme.formatter', (string) $def->getArgument(0));
234
    }
235
236
    public function testCachePluginConfigCacheKeyGeneratorReference()
237
    {
238
        $this->load([
239
            'plugins' => [
240
                'cache' => [
241
                    'cache_pool' => 'my_cache_pool',
242
                    'config' => [
243
                        'cache_key_generator' => 'header_cache_key_generator',
244
                    ],
245
                ],
246
            ],
247
        ]);
248
249
        $cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
250
251
        $config = $cachePlugin->getArgument(2);
252
        $this->assertArrayHasKey('cache_key_generator', $config);
253
        $this->assertInstanceOf(Reference::class, $config['cache_key_generator']);
254
        $this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
255
    }
256
257
    public function testContentTypePluginAllowedOptions()
258
    {
259
        $this->load([
260
            'clients' => [
261
                'acme' => [
262
                    'plugins' => [
263
                        [
264
                            'content_type' => [
265
                                'skip_detection' => true,
266
                                'size_limit' => 200000,
267
                            ],
268
                        ],
269
                    ],
270
                ],
271
            ],
272
        ]);
273
274
        $cachePlugin = $this->container->findDefinition('httplug.client.acme.plugin.content_type');
275
276
        $config = $cachePlugin->getArgument(0);
277
        $this->assertEquals([
278
            'skip_detection' => true,
279
            'size_limit' => 200000,
280
        ], $config);
281
    }
282
283
    public function testUsingServiceKeyForClients()
284
    {
285
        $this->load([
286
            'clients' => [
287
                'acme' => [
288
                    'service' => 'my_custom_client',
289
                ],
290
            ],
291
        ]);
292
293
        $client = $this->container->getAlias('httplug.client.acme.client');
294
        $this->assertEquals('my_custom_client', (string) $client);
295
        $this->assertFalse($client->isPublic());
296
    }
297
298
    private function verifyProfilingDisabled()
299
    {
300
        $def = $this->container->findDefinition('httplug.client');
301
        $this->assertTrue(is_subclass_of($def->getClass(), HttpClient::class));
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Http\Client\HttpClient::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
302
        $arguments = $def->getArguments();
303
304
        if (isset($arguments[3])) {
305
            $this->assertEmpty(
306
                $arguments[3],
307
                'Parameter 3 to the PluginClient must not contain any debug_plugin information when profiling is disabled'
308
            );
309
        }
310
    }
311
312
    public function testClientShouldHaveDefaultVisibility()
313
    {
314
        $this->load([
315
            'clients' => [
316
                'acme' => [],
317
            ],
318
        ]);
319
320
        $this->assertContainerBuilderHasService('httplug.client.acme');
321
322
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
323
            // Symfony made services private by default starting from 3.4
324
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
325
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPrivate());
326
        } else {
327
            // Legacy Symfony
328
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
329
        }
330
    }
331
332 View Code Duplication
    public function testFlexibleClientShouldBePrivateByDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
333
    {
334
        $this->load([
335
            'clients' => [
336
                'acme' => [
337
                    'flexible_client' => true,
338
                ],
339
            ],
340
        ]);
341
342
        $this->assertContainerBuilderHasService('httplug.client.acme');
343
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
344
    }
345
346 View Code Duplication
    public function testHttpMethodsClientShouldBePrivateByDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
347
    {
348
        $this->load([
349
            'clients' => [
350
                'acme' => [
351
                    'http_methods_client' => true,
352
                ],
353
            ],
354
        ]);
355
356
        $this->assertContainerBuilderHasService('httplug.client.acme');
357
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
358
    }
359
360 View Code Duplication
    public function testBatchClientShouldBePrivateByDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
361
    {
362
        $this->load([
363
            'clients' => [
364
                'acme' => [
365
                    'batch_client' => true,
366
                ],
367
            ],
368
        ]);
369
370
        $this->assertContainerBuilderHasService('httplug.client.acme');
371
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
372
    }
373
374 View Code Duplication
    public function testClientCanBePublic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
375
    {
376
        $this->load([
377
            'clients' => [
378
                'acme' => [
379
                    'public' => true,
380
                ],
381
            ],
382
        ]);
383
384
        $this->assertContainerBuilderHasService('httplug.client.acme');
385
        $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
386
387
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
388
            // Symfony made services private by default starting from 3.4
389
            $this->assertFalse($this->container->getDefinition('httplug.client.acme')->isPrivate());
390
        }
391
    }
392
393 View Code Duplication
    public function testFlexibleClientCanBePublic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
394
    {
395
        $this->load([
396
            'clients' => [
397
                'acme' => [
398
                    'public' => true,
399
                    'flexible_client' => true,
400
                ],
401
            ],
402
        ]);
403
404
        $this->assertContainerBuilderHasService('httplug.client.acme');
405
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
406
407
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
408
            // Symfony made services private by default starting from 3.4
409
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPrivate());
410
        }
411
    }
412
413 View Code Duplication
    public function testHttpMethodsClientCanBePublic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
414
    {
415
        $this->load([
416
            'clients' => [
417
                'acme' => [
418
                    'public' => true,
419
                    'http_methods_client' => true,
420
                ],
421
            ],
422
        ]);
423
424
        $this->assertContainerBuilderHasService('httplug.client.acme');
425
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
426
427
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
428
            // Symfony made services private by default starting from 3.4
429
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPrivate());
430
        }
431
    }
432
433 View Code Duplication
    public function testBatchClientCanBePublic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
434
    {
435
        $this->load([
436
            'clients' => [
437
                'acme' => [
438
                    'public' => true,
439
                    'batch_client' => true,
440
                ],
441
            ],
442
        ]);
443
444
        $this->assertContainerBuilderHasService('httplug.client.acme');
445
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
446
447
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
448
            // Symfony made services private by default starting from 3.4
449
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPrivate());
450
        }
451
    }
452
}
453