55use Http \Client \Common \PluginClient ;
66use Http \Client \HttpAsyncClient ;
77use Http \Client \HttpClient ;
8+ use Http \Discovery \HttpAsyncClientDiscovery ;
9+ use Http \Discovery \HttpClientDiscovery ;
10+ use Http \Discovery \Strategy \CommonClassesStrategy ;
811use Http \HttplugBundle \Collector \StackPlugin ;
12+ use Http \HttplugBundle \Discovery \ConfiguredClientsStrategy ;
913use Nyholm \NSA ;
1014use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
15+ use Symfony \Component \EventDispatcher \Event ;
1116
1217class 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