33namespace Intercom \Test ;
44
55use DateTimeImmutable ;
6- use GuzzleHttp \Client as GuzzleClient ;
7- use GuzzleHttp \Handler \MockHandler ;
8- use GuzzleHttp \HandlerStack ;
9- use GuzzleHttp \Middleware ;
106use GuzzleHttp \Psr7 \Response ;
11- use Http \Adapter \Guzzle6 \Client ;
7+ use Http \Client \Common \Plugin \ErrorPlugin ;
8+ use Http \Client \Common \PluginClient ;
129use Http \Client \Exception ;
10+ use Http \Discovery \HttpClientDiscovery ;
11+ use Http \Discovery \Strategy \MockClientStrategy ;
12+ use Http \Mock \Client ;
1313use Intercom \IntercomClient ;
14- use PHPUnit \Framework \TestCase ;
1514use stdClass ;
1615
1716class IntercomClientTest extends TestCase
1817{
19- public function testBasicClient ()
18+ protected function setUp (): void
2019 {
21- $ mock = new MockHandler ([
22- new Response (200 , ['X-Foo ' => 'Bar ' ], "{ \"foo \": \"bar \"} " )
23- ]);
24-
25- $ container = [];
26- $ history = Middleware::history ($ container );
27- $ stack = HandlerStack::create ($ mock );
28- $ stack ->push ($ history );
29-
30- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
31-
32- $ client = new IntercomClient ('u ' , 'p ' );
33- $ client ->setHttpClient ($ httpClient );
34-
35- $ client ->users ->create ([
36- 37- ]);
38-
39- foreach ($ container as $ transaction ) {
40- $ basic = $ transaction ['request ' ]->getHeaders ()['Authorization ' ][0 ];
41- $ this ->assertSame ("Basic dTpw " , $ basic );
42- }
20+ HttpClientDiscovery::prependStrategy (MockClientStrategy::class);
4321 }
4422
45- public function testExtendedClient ()
23+ public function testBasicClient ()
4624 {
47- $ mock = new MockHandler ([
25+ $ httpClient = new Client ();
26+ $ httpClient ->addResponse (
4827 new Response (200 , ['X-Foo ' => 'Bar ' ], "{ \"foo \": \"bar \"} " )
49- ]);
50-
51- $ container = [];
52- $ history = Middleware::history ($ container );
53- $ stack = HandlerStack::create ($ mock );
54- $ stack ->push ($ history );
55-
56- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack , 'connect_timeout ' => 10 ]));
28+ );
5729
5830 $ client = new IntercomClient ('u ' , 'p ' );
5931 $ client ->setHttpClient ($ httpClient );
@@ -62,24 +34,18 @@ public function testExtendedClient()
62346335 ]);
6436
65- foreach ($ container as $ transaction ) {
66- $ options = $ transaction [ ' options ' ];
67- $ this ->assertSame (10 , $ options [ ' connect_timeout ' ] );
37+ foreach ($ httpClient -> getRequests () as $ request ) {
38+ $ basic = $ request -> getHeaders ()[ ' Authorization ' ][ 0 ];
39+ $ this ->assertSame (" Basic dTpw " , $ basic );
6840 }
6941 }
7042
7143 public function testClientWithExtraHeaders ()
7244 {
73- $ mock = new MockHandler ([
45+ $ httpClient = new Client ();
46+ $ httpClient ->addResponse (
7447 new Response (200 , ['X-Foo ' => 'Bar ' ], "{ \"foo \": \"bar \"} " )
75- ]);
76-
77- $ container = [];
78- $ history = Middleware::history ($ container );
79- $ stack = HandlerStack::create ($ mock );
80- $ stack ->push ($ history );
81-
82- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
48+ );
8349
8450 $ client = new IntercomClient ('u ' , 'p ' , ['Custom-Header ' => 'value ' ]);
8551 $ client ->setHttpClient ($ httpClient );
@@ -88,8 +54,8 @@ public function testClientWithExtraHeaders()
88548955 ]);
9056
91- foreach ($ container as $ transaction ) {
92- $ headers = $ transaction [ ' request ' ] ->getHeaders ();
57+ foreach ($ httpClient -> getRequests () as $ request ) {
58+ $ headers = $ request ->getHeaders ();
9359 $ this ->assertSame ('application/json ' , $ headers ['Accept ' ][0 ]);
9460 $ this ->assertSame ('application/json ' , $ headers ['Content-Type ' ][0 ]);
9561 $ this ->assertSame ('value ' , $ headers ['Custom-Header ' ][0 ]);
@@ -98,16 +64,11 @@ public function testClientWithExtraHeaders()
9864
9965 public function testClientErrorHandling ()
10066 {
101- $ mock = new MockHandler ([
67+ $ httpClient = new Client ();
68+ $ httpClient ->addResponse (
10269 new Response (404 )
103- ]);
104-
105- $ container = [];
106- $ history = Middleware::history ($ container );
107- $ stack = HandlerStack::create ($ mock );
108- $ stack ->push ($ history );
109-
110- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
70+ );
71+ $ httpClient = new PluginClient ($ httpClient , [new ErrorPlugin ()]);
11172
11273 $ client = new IntercomClient ('u ' , 'p ' );
11374 $ client ->setHttpClient ($ httpClient );
@@ -120,16 +81,11 @@ public function testClientErrorHandling()
12081
12182 public function testServerErrorHandling ()
12283 {
123- $ mock = new MockHandler ([
84+ $ httpClient = new Client ();
85+ $ httpClient ->addResponse (
12486 new Response (500 )
125- ]);
126-
127- $ container = [];
128- $ history = Middleware::history ($ container );
129- $ stack = HandlerStack::create ($ mock );
130- $ stack ->push ($ history );
131-
132- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
87+ );
88+ $ httpClient = new PluginClient ($ httpClient , [new ErrorPlugin ()]);
13389
13490 $ client = new IntercomClient ('u ' , 'p ' );
13591 $ client ->setHttpClient ($ httpClient );
@@ -142,16 +98,10 @@ public function testServerErrorHandling()
14298
14399 public function testPaginationHelper ()
144100 {
145- $ mock = new MockHandler ([
101+ $ httpClient = new Client ();
102+ $ httpClient ->addResponse (
146103 new Response (200 , ['X-Foo ' => 'Bar ' ], "{ \"foo \": \"bar \"} " )
147- ]);
148-
149- $ container = [];
150- $ history = Middleware::history ($ container );
151- $ stack = HandlerStack::create ($ mock );
152- $ stack ->push ($ history );
153-
154- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
104+ );
155105
156106 $ client = new IntercomClient ('u ' , 'p ' );
157107 $ client ->setHttpClient ($ httpClient );
@@ -161,8 +111,8 @@ public function testPaginationHelper()
161111
162112 $ client ->nextPage ($ pages );
163113
164- foreach ($ container as $ transaction ) {
165- $ host = $ transaction [ ' request ' ] ->getUri ()->getHost ();
114+ foreach ($ httpClient -> getRequests () as $ request ) {
115+ $ host = $ request ->getUri ()->getHost ();
166116 $ this ->assertSame ("foo.com " , $ host );
167117 }
168118 }
@@ -171,7 +121,9 @@ public function testRateLimitDetails()
171121 {
172122 date_default_timezone_set ('UTC ' );
173123 $ time = time () + 7 ;
174- $ mock = new MockHandler ([
124+
125+ $ httpClient = new Client ();
126+ $ httpClient ->addResponse (
175127 new Response (
176128 200 ,
177129 [
@@ -181,14 +133,7 @@ public function testRateLimitDetails()
181133 ],
182134 "{ \"foo \": \"bar \"} "
183135 )
184- ]);
185-
186- $ container = [];
187- $ history = Middleware::history ($ container );
188- $ stack = HandlerStack::create ($ mock );
189- $ stack ->push ($ history );
190-
191- $ httpClient = new Client (new GuzzleClient (['handler ' => $ stack ]));
136+ );
192137
193138 $ client = new IntercomClient ('u ' , 'p ' );
194139 $ client ->setHttpClient ($ httpClient );
0 commit comments