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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
128
|
|
|
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]); |
129
|
|
|
$strategy->onEvent(new Event()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.