1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\LayerContracts\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
6
|
|
|
use EasyHttp\LayerContracts\Exceptions\HttpClientException; |
7
|
|
|
use EasyHttp\LayerContracts\Exceptions\HttpConnectionException; |
8
|
|
|
use EasyHttp\LayerContracts\Exceptions\ImpossibleToParseJsonException; |
9
|
|
|
use EasyHttp\LayerContracts\Tests\Unit\Example\SomeClient; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class AbstractClientTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
protected string $uri = 'http://example.com/api'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function itExecutesARequest(): HttpClientResponse |
20
|
|
|
{ |
21
|
|
|
$client = new SomeClient(); |
22
|
|
|
|
23
|
|
|
$response = $client->call('GET', $this->uri); |
24
|
|
|
|
25
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
26
|
|
|
$this->assertSame('{"key":"value"}', $response->getBody()); |
27
|
|
|
$this->assertSame( |
28
|
|
|
['Server' => 'Apache/2.4.38 (Debian)', 'X-Info' => 'GET ' . $this->uri], |
29
|
|
|
$response->getHeaders() |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
return $response; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
* @depends itExecutesARequest |
38
|
|
|
* @param HttpClientResponse $response |
39
|
|
|
* @throws ImpossibleToParseJsonException |
40
|
|
|
*/ |
41
|
|
|
public function itCanParseAResponseToJson(HttpClientResponse $response) |
42
|
|
|
{ |
43
|
|
|
$this->assertSame(['key' => 'value'], $response->parseJson()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function itPreparesARequestForExecution() |
50
|
|
|
{ |
51
|
|
|
$client = new SomeClient(); |
52
|
|
|
$client->prepareRequest('GET', $this->uri); |
53
|
|
|
|
54
|
|
|
$this->assertSame('GET', $client->getRequest()->getMethod()); |
55
|
|
|
$this->assertSame('http://example.com/api', $client->getRequest()->getUri()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function itExecutesAPreparedRequest() |
62
|
|
|
{ |
63
|
|
|
$client = new SomeClient(); |
64
|
|
|
$client->prepareRequest('GET', $this->uri); |
65
|
|
|
|
66
|
|
|
$response = $client->execute(); |
67
|
|
|
|
68
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
69
|
|
|
$this->assertSame(['key' => 'value'], $response->parseJson()); |
70
|
|
|
$this->assertSame( |
71
|
|
|
['Server' => 'Apache/2.4.38 (Debian)', 'X-Info' => 'GET ' . $this->uri], |
72
|
|
|
$response->getHeaders() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
|
public function itReuseTheAdapterForEachRequest() |
80
|
|
|
{ |
81
|
|
|
$client = new SomeClient(); |
82
|
|
|
|
83
|
|
|
$client->call('GET', $this->uri); |
84
|
|
|
$client->call('GET', $this->uri); |
85
|
|
|
$client->call('GET', $this->uri); |
86
|
|
|
|
87
|
|
|
$this->assertSame(1, $client->getAdapterCounter()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function itSetsHandlers() |
94
|
|
|
{ |
95
|
|
|
$client = new SomeClient(); |
96
|
|
|
|
97
|
|
|
$client->withHandler( |
98
|
|
|
function () { |
99
|
|
|
return [ |
100
|
|
|
'status' => 500, |
101
|
|
|
'headers' => ['Server' => 'Apache/2.4.38 (Ubuntu)'], |
102
|
|
|
'body' => '{"message":"Server Error"}', |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$response = $client->call('GET', $this->uri); |
108
|
|
|
|
109
|
|
|
$this->assertSame(500, $response->getStatusCode()); |
110
|
|
|
$this->assertSame(['message' => 'Server Error'], $response->parseJson()); |
111
|
|
|
$this->assertSame(['Server' => 'Apache/2.4.38 (Ubuntu)'], $response->getHeaders()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
|
public function itFlushTheAdapterAfterSetsHandler() |
118
|
|
|
{ |
119
|
|
|
$client = new SomeClient(); |
120
|
|
|
|
121
|
|
|
$liveResponse = $client->call('GET', $this->uri); |
122
|
|
|
|
123
|
|
|
$client->withHandler( |
124
|
|
|
function () { |
125
|
|
|
return [ |
126
|
|
|
'status' => 500, |
127
|
|
|
'headers' => ['Server' => 'Apache/2.4 (Ubuntu)'], |
128
|
|
|
'body' => 'Server Error - Try later again', |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$mockedResponse = $client->call('GET', $this->uri); |
134
|
|
|
|
135
|
|
|
$this->assertSame(200, $liveResponse->getStatusCode()); |
136
|
|
|
$this->assertSame(['key' => 'value'], $liveResponse->parseJson()); |
137
|
|
|
$this->assertSame( |
138
|
|
|
['Server' => 'Apache/2.4.38 (Debian)', 'X-Info' => 'GET ' . $this->uri], |
139
|
|
|
$liveResponse->getHeaders() |
140
|
|
|
); |
141
|
|
|
$this->assertSame(500, $mockedResponse->getStatusCode()); |
142
|
|
|
$this->assertSame(['Server' => 'Apache/2.4 (Ubuntu)'], $mockedResponse->getHeaders()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* This test is just a mock!. The responsibility for throwing this exception lies |
147
|
|
|
* with the library who is implementing this contracts! |
148
|
|
|
* |
149
|
|
|
* @test |
150
|
|
|
*/ |
151
|
|
|
public function itThrowsClientExceptionWhenFails() |
152
|
|
|
{ |
153
|
|
|
$this->expectException(HttpClientException::class); |
154
|
|
|
|
155
|
|
|
$client = new SomeClient(); |
156
|
|
|
|
157
|
|
|
$client->withHandler( |
158
|
|
|
function () { |
159
|
|
|
throw new HttpClientException('Bad request exception'); |
160
|
|
|
} |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$client->call('GET', $this->uri); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* This test is just a mock!. The responsibility for throwing this exception lies |
168
|
|
|
* with the library who is implementing this contracts! |
169
|
|
|
* |
170
|
|
|
* @test |
171
|
|
|
*/ |
172
|
|
|
public function itThrowsClientExceptionWhenConnectionFails() |
173
|
|
|
{ |
174
|
|
|
$this->expectException(HttpConnectionException::class); |
175
|
|
|
|
176
|
|
|
$client = new SomeClient(); |
177
|
|
|
|
178
|
|
|
$client->withHandler( |
179
|
|
|
function () { |
180
|
|
|
throw new HttpConnectionException('Service is down'); |
181
|
|
|
} |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$client->call('GET', $this->uri); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function itThrowsNotParsedExceptionWhenInvalidJsonIsFound() |
188
|
|
|
{ |
189
|
|
|
$this->expectException(ImpossibleToParseJsonException::class); |
190
|
|
|
|
191
|
|
|
$client = new SomeClient(); |
192
|
|
|
|
193
|
|
|
$client->withHandler( |
194
|
|
|
function () { |
195
|
|
|
return 'HTTP 500 - Server Error'; |
196
|
|
|
} |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$response = $client->call('GET', $this->uri); |
200
|
|
|
$response->parseJson(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|