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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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')); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testDiscoveredAsyncClientWithProfilingEnabled(): void |
|
|
|
|
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')); |
|
|
|
|
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); |
|
|
|
|
134
|
|
|
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]); |
135
|
|
|
$strategy->onEvent(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.