Completed
Push — master ( dd63ce...bad327 )
by David
14:41
created

DiscoveredClientsTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 36.21 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 42
loc 116
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testDiscoveredClient() 10 10 1
A testDiscoveredAsyncClient() 10 10 1
A testDiscoveredClientWithProfilingEnabled() 11 11 1
A testDiscoveredAsyncClientWithProfilingEnabled() 11 11 1
A testDiscovery() 0 18 1
A testDisabledDiscovery() 0 8 1
A testForcedDiscovery() 0 13 1
A getContainer() 0 6 1
A setUp() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Http\HttplugBundle\Tests\Functional;
4
5
use Http\Client\HttpAsyncClient;
6
use Http\Client\HttpClient;
7
use Http\Discovery\HttpAsyncClientDiscovery;
8
use Http\Discovery\HttpClientDiscovery;
9
use Http\Discovery\Strategy\CommonClassesStrategy;
10
use Http\HttplugBundle\Collector\ProfileClient;
11
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
12
use Nyholm\NSA;
13
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
14
use Symfony\Component\EventDispatcher\Event;
15
16
class DiscoveredClientsTest extends WebTestCase
17
{
18 View Code Duplication
    public function testDiscoveredClient()
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...
19
    {
20
        $container = $this->getContainer(false);
21
22
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
23
24
        $service = $container->get('httplug.auto_discovery.auto_discovered_client');
25
26
        $this->assertInstanceOf(HttpClient::class, $service);
27
    }
28
29 View Code Duplication
    public function testDiscoveredAsyncClient()
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...
30
    {
31
        $container = $this->getContainer(false);
32
33
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
34
35
        $service = $container->get('httplug.auto_discovery.auto_discovered_async');
36
37
        $this->assertInstanceOf(HttpAsyncClient::class, $service);
38
    }
39
40 View Code Duplication
    public function testDiscoveredClientWithProfilingEnabled()
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...
41
    {
42
        $container = $this->getContainer(true);
43
44
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
45
46
        $service = $container->get('httplug.auto_discovery.auto_discovered_client');
47
48
        $this->assertInstanceOf(ProfileClient::class, $service);
49
        $this->assertInstanceOf(HttpClient::class, NSA::getProperty($service, 'client'));
50
    }
51
52 View Code Duplication
    public function testDiscoveredAsyncClientWithProfilingEnabled()
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...
53
    {
54
        $container = $this->getContainer(true);
55
56
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
57
58
        $service = $container->get('httplug.auto_discovery.auto_discovered_async');
59
60
        $this->assertInstanceOf(ProfileClient::class, $service);
61
        $this->assertInstanceOf(HttpAsyncClient::class, NSA::getProperty($service, 'client'));
62
    }
63
64
    /**
65
     * Test with httplug.discovery.client: "auto".
66
     */
67
    public function testDiscovery()
68
    {
69
        $container = $this->getContainer(true);
70
71
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
72
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
73
        $this->assertTrue($container->has('httplug.strategy'));
74
75
        $container->get('httplug.strategy');
76
77
        $httpClient = $container->get('httplug.auto_discovery.auto_discovered_client');
78
        $httpAsyncClient = $container->get('httplug.auto_discovery.auto_discovered_async');
79
80
        $this->assertInstanceOf(ProfileClient::class, $httpClient);
81
        $this->assertSame(HttpClientDiscovery::find(), $httpClient);
82
        $this->assertInstanceOf(ProfileClient::class, $httpAsyncClient);
83
        $this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
84
    }
85
86
    /**
87
     * Test with httplug.discovery.client: null.
88
     */
89
    public function testDisabledDiscovery()
90
    {
91
        $container = $this->getContainer(true, 'discovery_disabled');
92
93
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
94
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
95
        $this->assertFalse($container->has('httplug.strategy'));
96
    }
97
98
    /**
99
     * Test with httplug.discovery.client: "httplug.client.acme".
100
     */
101
    public function testForcedDiscovery()
102
    {
103
        $container = $this->getContainer(true, 'discovery_forced');
104
105
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
106
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
107
        $this->assertTrue($container->has('httplug.strategy'));
108
109
        $container->get('httplug.strategy');
110
111
        $this->assertEquals($container->get('httplug.client.acme'), HttpClientDiscovery::find());
112
        $this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
113
    }
114
115
    private function getContainer($debug, $environment = 'test')
116
    {
117
        static::bootKernel(['debug' => $debug, 'environment' => $environment]);
118
119
        return static::$kernel->getContainer();
120
    }
121
122
    protected function setUp()
123
    {
124
        parent::setUp();
125
126
        // Reset values
127
        $strategy = new ConfiguredClientsStrategy(null, null, null);
0 ignored issues
show
Unused Code introduced by
The call to ConfiguredClientsStrategy::__construct() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
128
        HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]);
129
        $strategy->onEvent(new Event());
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\EventDispatcher\Event has been deprecated with message: since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
130
    }
131
}
132