-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDiscoveredClientsTest.php
More file actions
139 lines (104 loc) · 5.04 KB
/
DiscoveredClientsTest.php
File metadata and controls
139 lines (104 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
namespace Http\HttplugBundle\Tests\Functional;
use Http\Adapter\Guzzle7\Client;
use Http\Client\HttpAsyncClient;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\Strategy\CommonClassesStrategy;
use Http\Discovery\Strategy\CommonPsr17ClassesStrategy;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener;
use Nyholm\NSA;
use Psr\Http\Client\ClientInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class DiscoveredClientsTest extends WebTestCase
{
public function testDiscoveredClient(): void
{
$container = $this->getCustomContainer(false);
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
$service = $container->get('httplug.auto_discovery.auto_discovered_client');
$this->assertInstanceOf(ClientInterface::class, $service);
}
public function testDiscoveredAsyncClient(): void
{
$container = $this->getCustomContainer(false);
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
$service = $container->get('httplug.auto_discovery.auto_discovered_async');
$this->assertInstanceOf(HttpAsyncClient::class, $service);
}
public function testDiscoveredClientWithProfilingEnabled(): void
{
$container = $this->getCustomContainer(true);
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
$service = $container->get('httplug.auto_discovery.auto_discovered_client');
$this->assertInstanceOf(ProfileClient::class, $service);
$this->assertInstanceOf(ClientInterface::class, NSA::getProperty($service, 'client'));
}
public function testDiscoveredAsyncClientWithProfilingEnabled(): void
{
$container = $this->getCustomContainer(true);
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
$service = $container->get('httplug.auto_discovery.auto_discovered_async');
$this->assertInstanceOf(ProfileClient::class, $service);
$this->assertInstanceOf(HttpAsyncClient::class, NSA::getProperty($service, 'client'));
}
/**
* Test with httplug.discovery.client: "auto".
*/
public function testDiscovery(): void
{
$container = $this->getCustomContainer(true);
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
$this->assertTrue($container->has('httplug.strategy'));
$container->get('httplug.strategy');
$httpClient = $container->get('httplug.auto_discovery.auto_discovered_client');
$httpAsyncClient = $container->get('httplug.auto_discovery.auto_discovered_async');
$this->assertInstanceOf(ProfileClient::class, $httpClient);
$this->assertSame(Psr18ClientDiscovery::find(), $httpClient);
$this->assertInstanceOf(ProfileClient::class, $httpAsyncClient);
$this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
}
/**
* Test with httplug.discovery.client: null.
*/
public function testDisabledDiscovery(): void
{
$container = $this->getCustomContainer(true, 'discovery_disabled');
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
$this->assertFalse($container->has('httplug.strategy'));
}
/**
* Test with httplug.discovery.client: "httplug.client.acme".
*/
public function testForcedDiscovery(): void
{
if (!class_exists(Client::class)) {
$this->markTestSkipped('Guzzle7 adapter is not installed');
}
$container = $this->getCustomContainer(true, 'discovery_forced');
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_client'));
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
$this->assertTrue($container->has('httplug.strategy'));
$container->get('httplug.strategy');
$this->assertEquals($container->get('httplug.client.acme'), Psr18ClientDiscovery::find());
$this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
}
private function getCustomContainer($debug, $environment = 'test'): ContainerInterface
{
static::bootKernel(['debug' => $debug, 'environment' => $environment]);
return static::$kernel->getContainer();
}
protected function setUp(): void
{
parent::setUp();
// Reset values
$strategy = new ConfiguredClientsStrategyListener(null, null);
Psr18ClientDiscovery::setStrategies([CommonClassesStrategy::class, CommonPsr17ClassesStrategy::class]);
$strategy->onEvent();
}
}