DiscoveredClientsTest::testForcedDiscovery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Functional;
6
7
use Http\Adapter\Guzzle7\Client;
8
use Http\Client\HttpAsyncClient;
9
use Http\Client\HttpClient;
10
use Http\Discovery\HttpAsyncClientDiscovery;
11
use Http\Discovery\HttpClientDiscovery;
12
use Http\Discovery\Strategy\CommonClassesStrategy;
13
use Http\HttplugBundle\Collector\ProfileClient;
14
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener;
15
use Nyholm\NSA;
16
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17
18
class DiscoveredClientsTest extends WebTestCase
19
{
20 View Code Duplication
    public function testDiscoveredClient(): void
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...
21
    {
22
        $container = $this->getContainer(false);
23
24
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
25
26
        $service = $container->get('httplug.auto_discovery.auto_discovered_client');
27
28
        $this->assertInstanceOf(HttpClient::class, $service);
29
    }
30
31 View Code Duplication
    public function testDiscoveredAsyncClient(): void
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...
32
    {
33
        $container = $this->getContainer(false);
34
35
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
36
37
        $service = $container->get('httplug.auto_discovery.auto_discovered_async');
38
39
        $this->assertInstanceOf(HttpAsyncClient::class, $service);
40
    }
41
42 View Code Duplication
    public function testDiscoveredClientWithProfilingEnabled(): void
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...
43
    {
44
        $container = $this->getContainer(true);
45
46
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
47
48
        $service = $container->get('httplug.auto_discovery.auto_discovered_client');
49
50
        $this->assertInstanceOf(ProfileClient::class, $service);
51
        $this->assertInstanceOf(HttpClient::class, NSA::getProperty($service, 'client'));
0 ignored issues
show
Bug introduced by
It seems like $service defined by $container->get('httplug...uto_discovered_client') on line 48 can also be of type null; however, Nyholm\NSA::getProperty() does only seem to accept object|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
    }
53
54 View Code Duplication
    public function testDiscoveredAsyncClientWithProfilingEnabled(): void
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...
55
    {
56
        $container = $this->getContainer(true);
57
58
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
59
60
        $service = $container->get('httplug.auto_discovery.auto_discovered_async');
61
62
        $this->assertInstanceOf(ProfileClient::class, $service);
63
        $this->assertInstanceOf(HttpAsyncClient::class, NSA::getProperty($service, 'client'));
0 ignored issues
show
Bug introduced by
It seems like $service defined by $container->get('httplug...auto_discovered_async') on line 60 can also be of type null; however, Nyholm\NSA::getProperty() does only seem to accept object|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
    }
65
66
    /**
67
     * Test with httplug.discovery.client: "auto".
68
     */
69
    public function testDiscovery(): void
70
    {
71
        $container = $this->getContainer(true);
72
73
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
74
        $this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
75
        $this->assertTrue($container->has('httplug.strategy'));
76
77
        $container->get('httplug.strategy');
78
79
        $httpClient = $container->get('httplug.auto_discovery.auto_discovered_client');
80
        $httpAsyncClient = $container->get('httplug.auto_discovery.auto_discovered_async');
81
82
        $this->assertInstanceOf(ProfileClient::class, $httpClient);
83
        $this->assertSame(HttpClientDiscovery::find(), $httpClient);
84
        $this->assertInstanceOf(ProfileClient::class, $httpAsyncClient);
85
        $this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
86
    }
87
88
    /**
89
     * Test with httplug.discovery.client: null.
90
     */
91
    public function testDisabledDiscovery(): void
92
    {
93
        $container = $this->getContainer(true, 'discovery_disabled');
94
95
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
96
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
97
        $this->assertFalse($container->has('httplug.strategy'));
98
    }
99
100
    /**
101
     * Test with httplug.discovery.client: "httplug.client.acme".
102
     */
103
    public function testForcedDiscovery(): void
104
    {
105
        if (!class_exists(Client::class)) {
106
            $this->markTestSkipped('Guzzle7 adapter is not installed');
107
        }
108
109
        $container = $this->getContainer(true, 'discovery_forced');
110
111
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
112
        $this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
113
        $this->assertTrue($container->has('httplug.strategy'));
114
115
        $container->get('httplug.strategy');
116
117
        $this->assertEquals($container->get('httplug.client.acme'), HttpClientDiscovery::find());
118
        $this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
119
    }
120
121
    private function getContainer($debug, $environment = 'test')
122
    {
123
        static::bootKernel(['debug' => $debug, 'environment' => $environment]);
124
125
        return static::$kernel->getContainer();
126
    }
127
128
    protected function setUp(): void
129
    {
130
        parent::setUp();
131
132
        // Reset values
133
        $strategy = new ConfiguredClientsStrategyListener(null, null);
0 ignored issues
show
Unused Code introduced by
The call to ConfiguredClientsStrategyListener::__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...
134
        HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]);
135
        $strategy->onEvent();
136
    }
137
}
138