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

DiscoveredClientsTest::testDiscovery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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());
130
    }
131
}
132