Completed
Push — master ( b587fb...d05392 )
by David
20:34
created

testHttpMethodsClientCanBePublic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
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 testUsingServiceKeyForClients()
258
    {
259
        $this->load([
260
            'clients' => [
261
                'acme' => [
262
                    'service' => 'my_custom_client',
263
                ],
264
            ],
265
        ]);
266
267
        $client = $this->container->getAlias('httplug.client.acme.client');
268
        $this->assertEquals('my_custom_client', (string) $client);
269
        $this->assertFalse($client->isPublic());
270
    }
271
272
    private function verifyProfilingDisabled()
273
    {
274
        $def = $this->container->findDefinition('httplug.client');
275
        $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...
276
        $arguments = $def->getArguments();
277
278
        if (isset($arguments[3])) {
279
            $this->assertEmpty(
280
                $arguments[3],
281
                'Parameter 3 to the PluginClient must not contain any debug_plugin information when profiling is disabled'
282
            );
283
        }
284
    }
285
286
    public function testClientShouldHaveDefaultVisibility()
287
    {
288
        $this->load([
289
            'clients' => [
290
                'acme' => [],
291
            ],
292
        ]);
293
294
        $this->assertContainerBuilderHasService('httplug.client.acme');
295
296
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
297
            // Symfony made services private by default starting from 3.4
298
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
299
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPrivate());
300
        } else {
301
            // Legacy Symfony
302
            $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
303
        }
304
    }
305
306 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...
307
    {
308
        $this->load([
309
            'clients' => [
310
                'acme' => [
311
                    'flexible_client' => true,
312
                ],
313
            ],
314
        ]);
315
316
        $this->assertContainerBuilderHasService('httplug.client.acme');
317
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
318
    }
319
320 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...
321
    {
322
        $this->load([
323
            'clients' => [
324
                'acme' => [
325
                    'http_methods_client' => true,
326
                ],
327
            ],
328
        ]);
329
330
        $this->assertContainerBuilderHasService('httplug.client.acme');
331
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
332
    }
333
334 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...
335
    {
336
        $this->load([
337
            'clients' => [
338
                'acme' => [
339
                    'batch_client' => true,
340
                ],
341
            ],
342
        ]);
343
344
        $this->assertContainerBuilderHasService('httplug.client.acme');
345
        $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
346
    }
347
348 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...
349
    {
350
        $this->load([
351
            'clients' => [
352
                'acme' => [
353
                    'public' => true,
354
                ],
355
            ],
356
        ]);
357
358
        $this->assertContainerBuilderHasService('httplug.client.acme');
359
        $this->assertTrue($this->container->getDefinition('httplug.client.acme')->isPublic());
360
361
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
362
            // Symfony made services private by default starting from 3.4
363
            $this->assertFalse($this->container->getDefinition('httplug.client.acme')->isPrivate());
364
        }
365
    }
366
367 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...
368
    {
369
        $this->load([
370
            'clients' => [
371
                'acme' => [
372
                    'public' => true,
373
                    'flexible_client' => true,
374
                ],
375
            ],
376
        ]);
377
378
        $this->assertContainerBuilderHasService('httplug.client.acme');
379
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.flexible')->isPublic());
380
381
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
382
            // Symfony made services private by default starting from 3.4
383
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.flexible')->isPrivate());
384
        }
385
    }
386
387 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...
388
    {
389
        $this->load([
390
            'clients' => [
391
                'acme' => [
392
                    'public' => true,
393
                    'http_methods_client' => true,
394
                ],
395
            ],
396
        ]);
397
398
        $this->assertContainerBuilderHasService('httplug.client.acme');
399
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.http_methods')->isPublic());
400
401
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
402
            // Symfony made services private by default starting from 3.4
403
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.http_methods')->isPrivate());
404
        }
405
    }
406
407 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...
408
    {
409
        $this->load([
410
            'clients' => [
411
                'acme' => [
412
                    'public' => true,
413
                    'batch_client' => true,
414
                ],
415
            ],
416
        ]);
417
418
        $this->assertContainerBuilderHasService('httplug.client.acme');
419
        $this->assertTrue($this->container->getDefinition('httplug.client.acme.batch_client')->isPublic());
420
421
        if (version_compare(Kernel::VERSION, '3.4', '>=')) {
422
            // Symfony made services private by default starting from 3.4
423
            $this->assertFalse($this->container->getDefinition('httplug.client.acme.batch_client')->isPrivate());
424
        }
425
    }
426
}
427