Skip to content

Commit 233857c

Browse files
authored
Add more tests for discovery (#192)
* Added failing tests for #188 * Use the actual discovery function * Fixed tests * Applied changes from StyleCI
1 parent 227eee2 commit 233857c

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Discovery/ConfiguredClientsStrategy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyn
3535
{
3636
self::$client = $httpClient;
3737
self::$asyncClient = $asyncClient;
38+
HttpClientDiscovery::clearCache();
3839
}
3940

4041
/**

Tests/Functional/DiscoveredClientsTest.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use Http\Client\Common\PluginClient;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
8+
use Http\Discovery\HttpAsyncClientDiscovery;
9+
use Http\Discovery\HttpClientDiscovery;
10+
use Http\Discovery\Strategy\CommonClassesStrategy;
811
use Http\HttplugBundle\Collector\StackPlugin;
12+
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
913
use Nyholm\NSA;
1014
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
15+
use Symfony\Component\EventDispatcher\Event;
1116

1217
class DiscoveredClientsTest extends WebTestCase
1318
{
@@ -71,6 +76,31 @@ public function testDiscoveredAsyncClientWithProfilingEnabled()
7176
$this->assertEquals('auto_discovered_async', NSA::getProperty($plugins[0], 'client'));
7277
}
7378

79+
/**
80+
* Test with httplug.discovery.client: "auto".
81+
*/
82+
public function testDiscovery()
83+
{
84+
$container = $this->getContainer(true);
85+
86+
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_client'));
87+
$this->assertTrue($container->has('httplug.auto_discovery.auto_discovered_async'));
88+
$this->assertTrue($container->has('httplug.strategy'));
89+
90+
$container->get('httplug.strategy');
91+
92+
$httpClient = $container->get('httplug.auto_discovery.auto_discovered_client');
93+
$httpAsyncClient = $container->get('httplug.auto_discovery.auto_discovered_async');
94+
95+
$this->assertInstanceOf(PluginClient::class, $httpClient);
96+
$this->assertSame(HttpClientDiscovery::find(), $httpClient);
97+
$this->assertInstanceOf(PluginClient::class, $httpAsyncClient);
98+
$this->assertSame(HttpAsyncClientDiscovery::find(), $httpAsyncClient);
99+
}
100+
101+
/**
102+
* Test with httplug.discovery.client: null.
103+
*/
74104
public function testDisabledDiscovery()
75105
{
76106
$container = $this->getContainer(true, 'discovery_disabled');
@@ -80,6 +110,9 @@ public function testDisabledDiscovery()
80110
$this->assertFalse($container->has('httplug.strategy'));
81111
}
82112

113+
/**
114+
* Test with httplug.discovery.client: "httplug.client.acme".
115+
*/
83116
public function testForcedDiscovery()
84117
{
85118
$container = $this->getContainer(true, 'discovery_forced');
@@ -88,10 +121,10 @@ public function testForcedDiscovery()
88121
$this->assertFalse($container->has('httplug.auto_discovery.auto_discovered_async'));
89122
$this->assertTrue($container->has('httplug.strategy'));
90123

91-
$strategy = $container->get('httplug.strategy');
124+
$container->get('httplug.strategy');
92125

93-
$this->assertEquals($container->get('httplug.client.acme'), NSA::getProperty($strategy, 'client'));
94-
$this->assertEquals($container->get('httplug.client.acme'), NSA::getProperty($strategy, 'asyncClient'));
126+
$this->assertEquals($container->get('httplug.client.acme'), HttpClientDiscovery::find());
127+
$this->assertEquals($container->get('httplug.client.acme'), HttpAsyncClientDiscovery::find());
95128
}
96129

97130
private function getContainer($debug, $environment = 'test')
@@ -100,4 +133,14 @@ private function getContainer($debug, $environment = 'test')
100133

101134
return static::$kernel->getContainer();
102135
}
136+
137+
protected function setUp()
138+
{
139+
parent::setUp();
140+
141+
// Reset values
142+
$strategy = new ConfiguredClientsStrategy(null, null, null);
143+
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]);
144+
$strategy->onEvent(new Event());
145+
}
103146
}

0 commit comments

Comments
 (0)