3
3
namespace Intercom \Test ;
4
4
5
5
use DateTimeImmutable ;
6
- use GuzzleHttp \Client as GuzzleClient ;
7
- use GuzzleHttp \Handler \MockHandler ;
8
- use GuzzleHttp \HandlerStack ;
9
- use GuzzleHttp \Middleware ;
10
6
use GuzzleHttp \Psr7 \Response ;
11
- use Http \Adapter \Guzzle6 \Client ;
7
+ use Http \Client \Common \Plugin \ErrorPlugin ;
8
+ use Http \Client \Common \PluginClient ;
12
9
use Http \Client \Exception ;
10
+ use Http \Discovery \HttpClientDiscovery ;
11
+ use Http \Discovery \Strategy \MockClientStrategy ;
12
+ use Http \Mock \Client ;
13
13
use Intercom \IntercomClient ;
14
- use PHPUnit \Framework \TestCase ;
15
14
use stdClass ;
16
15
17
16
class IntercomClientTest extends TestCase
18
17
{
19
- public function testBasicClient ()
18
+ protected function setUp (): void
20
19
{
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);
43
21
}
44
22
45
- public function testExtendedClient ()
23
+ public function testBasicClient ()
46
24
{
47
- $ mock = new MockHandler ([
25
+ $ httpClient = new Client ();
26
+ $ httpClient ->addResponse (
48
27
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
+ );
57
29
58
30
$ client = new IntercomClient ('u ' , 'p ' );
59
31
$ client ->setHttpClient ($ httpClient );
@@ -62,24 +34,18 @@ public function testExtendedClient()
62
34
63
35
]);
64
36
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 );
68
40
}
69
41
}
70
42
71
43
public function testClientWithExtraHeaders ()
72
44
{
73
- $ mock = new MockHandler ([
45
+ $ httpClient = new Client ();
46
+ $ httpClient ->addResponse (
74
47
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
+ );
83
49
84
50
$ client = new IntercomClient ('u ' , 'p ' , ['Custom-Header ' => 'value ' ]);
85
51
$ client ->setHttpClient ($ httpClient );
@@ -88,8 +54,8 @@ public function testClientWithExtraHeaders()
88
54
89
55
]);
90
56
91
- foreach ($ container as $ transaction ) {
92
- $ headers = $ transaction [ ' request ' ] ->getHeaders ();
57
+ foreach ($ httpClient -> getRequests () as $ request ) {
58
+ $ headers = $ request ->getHeaders ();
93
59
$ this ->assertSame ('application/json ' , $ headers ['Accept ' ][0 ]);
94
60
$ this ->assertSame ('application/json ' , $ headers ['Content-Type ' ][0 ]);
95
61
$ this ->assertSame ('value ' , $ headers ['Custom-Header ' ][0 ]);
@@ -98,16 +64,11 @@ public function testClientWithExtraHeaders()
98
64
99
65
public function testClientErrorHandling ()
100
66
{
101
- $ mock = new MockHandler ([
67
+ $ httpClient = new Client ();
68
+ $ httpClient ->addResponse (
102
69
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 ()]);
111
72
112
73
$ client = new IntercomClient ('u ' , 'p ' );
113
74
$ client ->setHttpClient ($ httpClient );
@@ -120,16 +81,11 @@ public function testClientErrorHandling()
120
81
121
82
public function testServerErrorHandling ()
122
83
{
123
- $ mock = new MockHandler ([
84
+ $ httpClient = new Client ();
85
+ $ httpClient ->addResponse (
124
86
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 ()]);
133
89
134
90
$ client = new IntercomClient ('u ' , 'p ' );
135
91
$ client ->setHttpClient ($ httpClient );
@@ -142,16 +98,10 @@ public function testServerErrorHandling()
142
98
143
99
public function testPaginationHelper ()
144
100
{
145
- $ mock = new MockHandler ([
101
+ $ httpClient = new Client ();
102
+ $ httpClient ->addResponse (
146
103
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
+ );
155
105
156
106
$ client = new IntercomClient ('u ' , 'p ' );
157
107
$ client ->setHttpClient ($ httpClient );
@@ -161,8 +111,8 @@ public function testPaginationHelper()
161
111
162
112
$ client ->nextPage ($ pages );
163
113
164
- foreach ($ container as $ transaction ) {
165
- $ host = $ transaction [ ' request ' ] ->getUri ()->getHost ();
114
+ foreach ($ httpClient -> getRequests () as $ request ) {
115
+ $ host = $ request ->getUri ()->getHost ();
166
116
$ this ->assertSame ("foo.com " , $ host );
167
117
}
168
118
}
@@ -171,7 +121,9 @@ public function testRateLimitDetails()
171
121
{
172
122
date_default_timezone_set ('UTC ' );
173
123
$ time = time () + 7 ;
174
- $ mock = new MockHandler ([
124
+
125
+ $ httpClient = new Client ();
126
+ $ httpClient ->addResponse (
175
127
new Response (
176
128
200 ,
177
129
[
@@ -181,14 +133,7 @@ public function testRateLimitDetails()
181
133
],
182
134
"{ \"foo \": \"bar \"} "
183
135
)
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
+ );
192
137
193
138
$ client = new IntercomClient ('u ' , 'p ' );
194
139
$ client ->setHttpClient ($ httpClient );
0 commit comments