1
|
|
|
<?php |
2
|
|
|
namespace Chadicus\Marvel\Api\Adapter; |
3
|
|
|
|
4
|
|
|
use Chadicus\Marvel\Api\Request; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Unit tests for \Chadicus\Marvel\Api\Adapter\CurlAdapter class. |
8
|
|
|
* |
9
|
|
|
* @coversDefaultClass \Chadicus\Marvel\Api\Adapter\CurlAdapter |
10
|
|
|
* @covers ::<private> |
11
|
|
|
*/ |
12
|
|
|
final class CurlAdapterTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* set up each test. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
\Chadicus\FunctionRegistry::reset(__NAMESPACE__, ['curl']); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Verify basic behavior of send. |
26
|
|
|
* |
27
|
|
|
* @test |
28
|
|
|
* @covers ::send |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function send() |
33
|
|
|
{ |
34
|
|
|
\Chadicus\FunctionRegistry::set( |
35
|
|
|
__NAMESPACE__, |
36
|
|
|
'curl_init', |
37
|
|
|
function () { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
\Chadicus\FunctionRegistry::set( |
43
|
|
|
__NAMESPACE__, |
44
|
|
|
'curl_setopt_array', |
45
|
|
|
function ($curl, array $options) { |
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
\Chadicus\FunctionRegistry::set( |
51
|
|
|
__NAMESPACE__, |
52
|
|
|
'curl_exec', |
53
|
|
|
function ($curl) { |
|
|
|
|
54
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: application/json\r\n\n{\"foo\":\"bar\"}"; |
55
|
|
|
} |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
\Chadicus\FunctionRegistry::set( |
59
|
|
|
__NAMESPACE__, |
60
|
|
|
'curl_error', |
61
|
|
|
function ($curl) { |
|
|
|
|
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
\Chadicus\FunctionRegistry::set( |
67
|
|
|
__NAMESPACE__, |
68
|
|
|
'curl_getinfo', |
69
|
|
|
function ($curl, $option) { |
70
|
|
|
if ($option === CURLINFO_HEADER_SIZE) { |
71
|
|
|
return 69; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($option === CURLINFO_HTTP_CODE) { |
75
|
|
|
return 200; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$response = (new CurlAdapter())->send(new Request('not under test', 'get', [], ['foo' => 'bar'])); |
81
|
|
|
$this->assertSame(200, $response->getHttpCode()); |
82
|
|
|
$this->assertSame( |
83
|
|
|
[ |
84
|
|
|
'Response Code' => 200, |
85
|
|
|
'Response Status' => 'OK', |
86
|
|
|
'Content-Length' => '13', |
87
|
|
|
'Content-Type' => 'application/json', |
88
|
|
|
], |
89
|
|
|
$response->getHeaders() |
90
|
|
|
); |
91
|
|
|
$this->assertSame(['foo' => 'bar'], $response->getBody()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Verify basic behavior of send. |
96
|
|
|
* |
97
|
|
|
* @test |
98
|
|
|
* @covers ::send |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function sendSetRequestHeaders() |
103
|
|
|
{ |
104
|
|
|
\Chadicus\FunctionRegistry::set( |
105
|
|
|
__NAMESPACE__, |
106
|
|
|
'curl_init', |
107
|
|
|
function () { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$actualHeaders = []; |
113
|
|
|
|
114
|
|
|
\Chadicus\FunctionRegistry::set( |
115
|
|
|
__NAMESPACE__, |
116
|
|
|
'curl_setopt_array', |
117
|
|
|
function ($curl, array $options) use (&$actualHeaders) { |
118
|
|
|
$actualHeaders = $options[CURLOPT_HTTPHEADER]; |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
\Chadicus\FunctionRegistry::set( |
124
|
|
|
__NAMESPACE__, |
125
|
|
|
'curl_exec', |
126
|
|
|
function ($curl) { |
|
|
|
|
127
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\n[]"; |
128
|
|
|
} |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
\Chadicus\FunctionRegistry::set( |
132
|
|
|
__NAMESPACE__, |
133
|
|
|
'curl_error', |
134
|
|
|
function ($curl) { |
|
|
|
|
135
|
|
|
return ''; |
136
|
|
|
} |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
\Chadicus\FunctionRegistry::set( |
140
|
|
|
__NAMESPACE__, |
141
|
|
|
'curl_getinfo', |
142
|
|
|
function ($curl, $option) { |
143
|
|
|
if ($option === CURLINFO_HEADER_SIZE) { |
144
|
|
|
return 69; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($option === CURLINFO_HTTP_CODE) { |
148
|
|
|
return 200; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', ['foo' => 'bar'], [])); |
154
|
|
|
|
155
|
|
|
$this->assertSame(['Expect:', 'foo: bar'], $actualHeaders); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Verify Exception is thrown when $method is not valid. |
160
|
|
|
* |
161
|
|
|
* @test |
162
|
|
|
* @covers ::send |
163
|
|
|
* @expectedException \Exception |
164
|
|
|
* @expectedExceptionMessage Unsupported method 'foo' given |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function sendWithInvalidMethod() |
169
|
|
|
{ |
170
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'foo', [], [])); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Verify Exception is thrown when curl_init fails. |
175
|
|
|
* |
176
|
|
|
* @test |
177
|
|
|
* @covers ::send |
178
|
|
|
* @expectedException \Exception |
179
|
|
|
* @expectedExceptionMessage Unable to initialize connection |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function sendCurlInitFails() |
184
|
|
|
{ |
185
|
|
|
\Chadicus\FunctionRegistry::set( |
186
|
|
|
__NAMESPACE__, |
187
|
|
|
'curl_init', |
188
|
|
|
function () { |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Verify Exception is thrown when curl_setopt_array fails. |
198
|
|
|
* |
199
|
|
|
* @test |
200
|
|
|
* @covers ::send |
201
|
|
|
* @expectedException \Exception |
202
|
|
|
* @expectedExceptionMessage Unable to prepare connection |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
public function sendCurlSetoptArrayFails() |
207
|
|
|
{ |
208
|
|
|
\Chadicus\FunctionRegistry::set( |
209
|
|
|
__NAMESPACE__, |
210
|
|
|
'curl_setopt_array', |
211
|
|
|
function () { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Verify Exception is thrown when curl_exec fails. |
221
|
|
|
* |
222
|
|
|
* @test |
223
|
|
|
* @covers ::send |
224
|
|
|
* @expectedException \Exception |
225
|
|
|
* @expectedExceptionMessage the error |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
public function sendCurlExecFails() |
230
|
|
|
{ |
231
|
|
|
\Chadicus\FunctionRegistry::set( |
232
|
|
|
__NAMESPACE__, |
233
|
|
|
'curl_exec', |
234
|
|
|
function () { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
\Chadicus\FunctionRegistry::set( |
240
|
|
|
__NAMESPACE__, |
241
|
|
|
'curl_error', |
242
|
|
|
function () { |
243
|
|
|
return 'the error'; |
244
|
|
|
} |
245
|
|
|
); |
246
|
|
|
|
247
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Verify behavior when curl_getinfo return false for CURLINFO_HEADER_SIZE. |
252
|
|
|
* |
253
|
|
|
* @test |
254
|
|
|
* @covers ::send |
255
|
|
|
* @expectedException \Exception |
256
|
|
|
* @expectedExceptionMessage Unable to determine header size |
257
|
|
|
* |
258
|
|
|
* @return void |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function sendCurlGetinfoFailsOnHeaderSize() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
\Chadicus\FunctionRegistry::set( |
263
|
|
|
__NAMESPACE__, |
264
|
|
|
'curl_init', |
265
|
|
|
function () { |
266
|
|
|
return true; |
267
|
|
|
} |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
\Chadicus\FunctionRegistry::set( |
271
|
|
|
__NAMESPACE__, |
272
|
|
|
'curl_setopt_array', |
273
|
|
|
function ($curl, array $options) { |
|
|
|
|
274
|
|
|
return true; |
275
|
|
|
} |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
\Chadicus\FunctionRegistry::set( |
279
|
|
|
__NAMESPACE__, |
280
|
|
|
'curl_exec', |
281
|
|
|
function ($curl) { |
|
|
|
|
282
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Type: application/json\r\n\n[]"; |
283
|
|
|
} |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
\Chadicus\FunctionRegistry::set( |
287
|
|
|
__NAMESPACE__, |
288
|
|
|
'curl_error', |
289
|
|
|
function ($curl) { |
|
|
|
|
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
\Chadicus\FunctionRegistry::set( |
295
|
|
|
__NAMESPACE__, |
296
|
|
|
'curl_getinfo', |
297
|
|
|
function ($curl, $option) { |
298
|
|
|
if ($option === CURLINFO_HEADER_SIZE) { |
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Verify behavior when curl_getinfo return false for CURLINFO_HTTP_CODE. |
309
|
|
|
* |
310
|
|
|
* @test |
311
|
|
|
* @covers ::send |
312
|
|
|
* @expectedException \Exception |
313
|
|
|
* @expectedExceptionMessage Unable to determine response HTTP code |
314
|
|
|
* |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
View Code Duplication |
public function sendCurlGetinfoFailsOnHttpCode() |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
\Chadicus\FunctionRegistry::set( |
320
|
|
|
__NAMESPACE__, |
321
|
|
|
'curl_init', |
322
|
|
|
function () { |
323
|
|
|
return true; |
324
|
|
|
} |
325
|
|
|
); |
326
|
|
|
|
327
|
|
|
\Chadicus\FunctionRegistry::set( |
328
|
|
|
__NAMESPACE__, |
329
|
|
|
'curl_setopt_array', |
330
|
|
|
function ($curl, array $options) { |
|
|
|
|
331
|
|
|
return true; |
332
|
|
|
} |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
\Chadicus\FunctionRegistry::set( |
336
|
|
|
__NAMESPACE__, |
337
|
|
|
'curl_exec', |
338
|
|
|
function ($curl) { |
|
|
|
|
339
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\n[]"; |
340
|
|
|
} |
341
|
|
|
); |
342
|
|
|
|
343
|
|
|
\Chadicus\FunctionRegistry::set( |
344
|
|
|
__NAMESPACE__, |
345
|
|
|
'curl_error', |
346
|
|
|
function ($curl) { |
|
|
|
|
347
|
|
|
return ''; |
348
|
|
|
} |
349
|
|
|
); |
350
|
|
|
|
351
|
|
|
\Chadicus\FunctionRegistry::set( |
352
|
|
|
__NAMESPACE__, |
353
|
|
|
'curl_getinfo', |
354
|
|
|
function ($curl, $option) { |
355
|
|
|
if ($option === CURLINFO_HEADER_SIZE) { |
356
|
|
|
return 69; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
if ($option === CURLINFO_HTTP_CODE) { |
360
|
|
|
return false; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
); |
364
|
|
|
|
365
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Verify behavior when json_last_error returns a value other than JSON_ERROR_NONE. |
370
|
|
|
* |
371
|
|
|
* @test |
372
|
|
|
* @covers ::send |
373
|
|
|
* @expectedException \Exception |
374
|
|
|
* @expectedExceptionMessage Unable to parse response: Syntax error |
375
|
|
|
* |
376
|
|
|
* @return void |
377
|
|
|
*/ |
378
|
|
View Code Duplication |
public function sendInvalidJsonInResult() |
|
|
|
|
379
|
|
|
{ |
380
|
|
|
\Chadicus\FunctionRegistry::set( |
381
|
|
|
__NAMESPACE__, |
382
|
|
|
'curl_init', |
383
|
|
|
function () { |
384
|
|
|
return true; |
385
|
|
|
} |
386
|
|
|
); |
387
|
|
|
|
388
|
|
|
\Chadicus\FunctionRegistry::set( |
389
|
|
|
__NAMESPACE__, |
390
|
|
|
'curl_setopt_array', |
391
|
|
|
function ($curl, array $options) { |
|
|
|
|
392
|
|
|
return true; |
393
|
|
|
} |
394
|
|
|
); |
395
|
|
|
|
396
|
|
|
\Chadicus\FunctionRegistry::set( |
397
|
|
|
__NAMESPACE__, |
398
|
|
|
'curl_exec', |
399
|
|
|
function ($curl) { |
|
|
|
|
400
|
|
|
// contains syntax error |
401
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\n{xx}}"; |
402
|
|
|
} |
403
|
|
|
); |
404
|
|
|
|
405
|
|
|
\Chadicus\FunctionRegistry::set( |
406
|
|
|
__NAMESPACE__, |
407
|
|
|
'curl_error', |
408
|
|
|
function ($curl) { |
|
|
|
|
409
|
|
|
return ''; |
410
|
|
|
} |
411
|
|
|
); |
412
|
|
|
|
413
|
|
|
\Chadicus\FunctionRegistry::set( |
414
|
|
|
__NAMESPACE__, |
415
|
|
|
'curl_getinfo', |
416
|
|
|
function ($curl, $option) { |
417
|
|
|
if ($option === CURLINFO_HEADER_SIZE) { |
418
|
|
|
return 69; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
if ($option === CURLINFO_HTTP_CODE) { |
422
|
|
|
return 200; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
); |
426
|
|
|
|
427
|
|
|
(new CurlAdapter())->send(new Request('not under test', 'get', [], [])); |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.