55use  Http \Client \HttpAsyncClient ;
66use  Http \Client \HttpClient ;
77use  Http \HttplugBundle \Discovery \ConfiguredClientsStrategy ;
8+ use  Symfony \Component \DependencyInjection \ContainerInterface ;
9+ use  Symfony \Component \DependencyInjection \ServiceLocator ;
810
911class  ConfiguredClientsStrategyTest extends  \PHPUnit_Framework_TestCase
1012{
1113    public  function  testGetCandidates ()
1214    {
1315        $ httpClient  = $ this  ->getMockBuilder (HttpClient::class)->getMock ();
1416        $ httpAsyncClient  = $ this  ->getMockBuilder (HttpAsyncClient::class)->getMock ();
15-         $ strategy  = new  ConfiguredClientsStrategy ($ httpClient , $ httpAsyncClient );
17+         $ locator  = $ this  ->getLocatorMock ();
18+         $ locator
19+             ->expects ($ this  ->exactly (2 ))
20+             ->method ('has ' )
21+             ->willReturn (true )
22+         ;
23+         $ locator
24+             ->expects ($ this  ->exactly (2 ))
25+             ->method ('get ' )
26+             ->will ($ this  ->onConsecutiveCalls ($ httpClient , $ httpAsyncClient ))
27+         ;
28+ 
29+         $ strategy  = new  ConfiguredClientsStrategy ($ locator , 'httplug.auto_discovery.auto_discovered_client ' , 'httplug.auto_discovery.auto_discovered_async ' );
1630
1731        $ candidates  = $ strategy ::getCandidates (HttpClient::class);
1832        $ candidate  = array_shift ($ candidates );
@@ -25,7 +39,18 @@ public function testGetCandidates()
2539
2640    public  function  testGetCandidatesEmpty ()
2741    {
28-         $ strategy  = new  ConfiguredClientsStrategy (null , null );
42+         $ locator  = $ this  ->getLocatorMock ();
43+         $ locator
44+             ->expects ($ this  ->exactly (2 ))
45+             ->method ('has ' )
46+             ->willReturn (false )
47+         ;
48+         $ locator
49+             ->expects ($ this  ->never ())
50+             ->method ('get ' )
51+         ;
52+ 
53+         $ strategy  = new  ConfiguredClientsStrategy ($ locator , 'httplug.auto_discovery.auto_discovered_client ' , 'httplug.auto_discovery.auto_discovered_async ' );
2954
3055        $ candidates  = $ strategy ::getCandidates (HttpClient::class);
3156        $ this  ->assertEquals ([], $ candidates );
@@ -37,7 +62,23 @@ public function testGetCandidatesEmpty()
3762    public  function  testGetCandidatesEmptyAsync ()
3863    {
3964        $ httpClient  = $ this  ->getMockBuilder (HttpClient::class)->getMock ();
40-         $ strategy  = new  ConfiguredClientsStrategy ($ httpClient , null );
65+ 
66+         $ locator  = $ this  ->getLocatorMock ();
67+         $ locator
68+             ->expects ($ this  ->exactly (2 ))
69+             ->method ('has ' )
70+             ->willReturnMap ([
71+                 ['httplug.auto_discovery.auto_discovered_client ' , true ],
72+                 ['httplug.auto_discovery.auto_discovered_async ' , false ],
73+             ])
74+         ;
75+         $ locator
76+             ->expects ($ this  ->once ())
77+             ->method ('get ' )
78+             ->willReturn ($ httpClient )
79+         ;
80+ 
81+         $ strategy  = new  ConfiguredClientsStrategy ($ locator , 'httplug.auto_discovery.auto_discovered_client ' , 'httplug.auto_discovery.auto_discovered_async ' );
4182
4283        $ candidates  = $ strategy ::getCandidates (HttpClient::class);
4384        $ candidate  = array_shift ($ candidates );
@@ -50,7 +91,23 @@ public function testGetCandidatesEmptyAsync()
5091    public  function  testGetCandidatesEmptySync ()
5192    {
5293        $ httpAsyncClient  = $ this  ->getMockBuilder (HttpAsyncClient::class)->getMock ();
53-         $ strategy  = new  ConfiguredClientsStrategy (null , $ httpAsyncClient );
94+ 
95+         $ locator  = $ this  ->getLocatorMock ();
96+         $ locator
97+             ->expects ($ this  ->exactly (2 ))
98+             ->method ('has ' )
99+             ->willReturnMap ([
100+                 ['httplug.auto_discovery.auto_discovered_client ' , false ],
101+                 ['httplug.auto_discovery.auto_discovered_async ' , true ],
102+             ])
103+         ;
104+         $ locator
105+             ->expects ($ this  ->once ())
106+             ->method ('get ' )
107+             ->willReturn ($ httpAsyncClient )
108+         ;
109+ 
110+         $ strategy  = new  ConfiguredClientsStrategy ($ locator , 'httplug.auto_discovery.auto_discovered_client ' , 'httplug.auto_discovery.auto_discovered_async ' );
54111
55112        $ candidates  = $ strategy ::getCandidates (HttpClient::class);
56113        $ this  ->assertEquals ([], $ candidates );
@@ -59,4 +116,16 @@ public function testGetCandidatesEmptySync()
59116        $ candidate  = array_shift ($ candidates );
60117        $ this  ->assertEquals ($ httpAsyncClient , $ candidate ['class ' ]());
61118    }
119+ 
120+     /** 
121+      * @return ContainerInterface|ServiceLocator 
122+      */ 
123+     private  function  getLocatorMock ()
124+     {
125+         if  (class_exists (ServiceLocator::class)) {
126+             return  $ this  ->getMockBuilder (ServiceLocator::class)->disableOriginalConstructor ()->getMock ();
127+         }
128+ 
129+         return  $ this  ->getMockBuilder (ContainerInterface::class)->getMock ();
130+     }
62131}
0 commit comments