5
5
use Http \Client \HttpAsyncClient ;
6
6
use Http \Client \HttpClient ;
7
7
use Http \HttplugBundle \Discovery \ConfiguredClientsStrategy ;
8
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
9
+ use Symfony \Component \DependencyInjection \ServiceLocator ;
8
10
9
11
class ConfiguredClientsStrategyTest extends \PHPUnit_Framework_TestCase
10
12
{
11
13
public function testGetCandidates ()
12
14
{
13
15
$ httpClient = $ this ->getMockBuilder (HttpClient::class)->getMock ();
14
16
$ 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 ' );
16
30
17
31
$ candidates = $ strategy ::getCandidates (HttpClient::class);
18
32
$ candidate = array_shift ($ candidates );
@@ -25,7 +39,18 @@ public function testGetCandidates()
25
39
26
40
public function testGetCandidatesEmpty ()
27
41
{
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 ' );
29
54
30
55
$ candidates = $ strategy ::getCandidates (HttpClient::class);
31
56
$ this ->assertEquals ([], $ candidates );
@@ -37,7 +62,23 @@ public function testGetCandidatesEmpty()
37
62
public function testGetCandidatesEmptyAsync ()
38
63
{
39
64
$ 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 ' );
41
82
42
83
$ candidates = $ strategy ::getCandidates (HttpClient::class);
43
84
$ candidate = array_shift ($ candidates );
@@ -50,7 +91,23 @@ public function testGetCandidatesEmptyAsync()
50
91
public function testGetCandidatesEmptySync ()
51
92
{
52
93
$ 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 ' );
54
111
55
112
$ candidates = $ strategy ::getCandidates (HttpClient::class);
56
113
$ this ->assertEquals ([], $ candidates );
@@ -59,4 +116,16 @@ public function testGetCandidatesEmptySync()
59
116
$ candidate = array_shift ($ candidates );
60
117
$ this ->assertEquals ($ httpAsyncClient , $ candidate ['class ' ]());
61
118
}
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
+ }
62
131
}
0 commit comments