1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModuleTest\Service; |
5
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Options\Interfaces\AuthOptionsInterface; |
7
|
|
|
use InvoiceNinjaModule\Options\Interfaces\RequestOptionsInterface; |
8
|
|
|
use InvoiceNinjaModule\Options\Interfaces\ModuleOptionsInterface; |
9
|
|
|
use InvoiceNinjaModule\Service\Interfaces\RequestServiceInterface; |
10
|
|
|
use InvoiceNinjaModule\Service\RequestService; |
11
|
|
|
use Zend\Http\Client; |
12
|
|
|
use Zend\Http\Header\HeaderInterface; |
13
|
|
|
use Zend\Http\Headers; |
14
|
|
|
use Zend\Http\Request; |
15
|
|
|
use Zend\Http\Response; |
16
|
|
|
use Zend\Stdlib\RequestInterface; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class RequestServiceTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** @var RequestServiceInterface */ |
22
|
|
|
private $manager; |
23
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
24
|
|
|
private $settingsMock; |
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
private $httpClientMock; |
27
|
|
|
/** @var string */ |
28
|
|
|
private $reqMethod; |
29
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
30
|
|
|
private $requestOptions; |
31
|
|
|
|
32
|
|
|
protected function setUp() :void |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
$this->settingsMock = $this->createMock(ModuleOptionsInterface::class); |
36
|
|
|
$this->httpClientMock = $this->createMock(Client::class); |
37
|
|
|
$this->reqMethod = Request::METHOD_GET; |
38
|
|
|
$this->requestOptions = $this->createMock(RequestOptionsInterface::class); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCreate() :void |
45
|
|
|
{ |
46
|
|
|
self::assertInstanceOf(RequestServiceInterface::class, $this->manager); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testDispatchRequest() :void |
50
|
|
|
{ |
51
|
|
|
$testTokenType = 'testtokentype'; |
52
|
|
|
$testToken = 'testtoken'; |
53
|
|
|
$testReqRoute = 'testroute'; |
54
|
|
|
|
55
|
|
|
$headers = $this->createMock(Headers::class); |
56
|
|
|
$headers->expects(self::once()) |
57
|
|
|
->method('get') |
58
|
|
|
->with(self::stringContains('Content-disposition')) |
59
|
|
|
->willReturn(false); |
60
|
|
|
|
61
|
|
|
$response = $this->createMock(Response::class); |
62
|
|
|
$response->expects(self::once()) |
63
|
|
|
->method('getStatusCode') |
64
|
|
|
->willReturn(Response::STATUS_CODE_200); |
65
|
|
|
|
66
|
|
|
$response->expects(self::once()) |
67
|
|
|
->method('getHeaders') |
68
|
|
|
->willReturn($headers); |
69
|
|
|
|
70
|
|
|
$response->expects(self::once()) |
71
|
|
|
->method('getBody') |
72
|
|
|
->willReturn('{"data": [0]}'); |
73
|
|
|
|
74
|
|
|
$this->settingsMock->expects(self::once()) |
75
|
|
|
->method('getTimeout') |
76
|
|
|
->willReturn(10); |
77
|
|
|
|
78
|
|
|
$this->settingsMock->expects(self::once()) |
79
|
|
|
->method('getTokenType') |
80
|
|
|
->willReturn($testTokenType); |
81
|
|
|
|
82
|
|
|
$this->settingsMock->expects(self::once()) |
83
|
|
|
->method('getToken') |
84
|
|
|
->willReturn($testToken); |
85
|
|
|
|
86
|
|
|
$this->httpClientMock->expects(self::once()) |
87
|
|
|
->method('send') |
88
|
|
|
->with( |
89
|
|
|
self::logicalAnd( |
90
|
|
|
self::isInstanceOf(RequestInterface::class), |
91
|
|
|
self::callback(function ($request) { |
92
|
|
|
/** @var Request $request */ |
93
|
|
|
return $request->getAllowCustomMethods() === false; |
94
|
|
|
}) |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
->willReturn($response); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
101
|
|
|
|
102
|
|
|
self::assertInternalType( |
103
|
|
|
'array', |
104
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testDispatchRequestAuth() :void |
109
|
|
|
{ |
110
|
|
|
$testTokenType = 'testtokentype'; |
111
|
|
|
$testToken = 'testtoken'; |
112
|
|
|
$testReqRoute = 'testroute'; |
113
|
|
|
|
114
|
|
|
$authOptions = $this->createMock(AuthOptionsInterface::class); |
115
|
|
|
$authOptions->expects(self::once()) |
116
|
|
|
->method('isAuthorization') |
117
|
|
|
->willReturn(true); |
118
|
|
|
|
119
|
|
|
$authOptions->expects(self::once()) |
120
|
|
|
->method('getUsername') |
121
|
|
|
->willReturn('username'); |
122
|
|
|
|
123
|
|
|
$authOptions->expects(self::once()) |
124
|
|
|
->method('getPassword') |
125
|
|
|
->willReturn('password'); |
126
|
|
|
|
127
|
|
|
$authOptions->expects(self::once()) |
128
|
|
|
->method('getAuthType') |
129
|
|
|
->willReturn(Client::AUTH_BASIC); |
130
|
|
|
|
131
|
|
|
$headers = $this->createMock(Headers::class); |
132
|
|
|
$headers->expects(self::once()) |
133
|
|
|
->method('get') |
134
|
|
|
->with(self::stringContains('Content-disposition')) |
135
|
|
|
->willReturn(false); |
136
|
|
|
|
137
|
|
|
$response = $this->createMock(Response::class); |
138
|
|
|
$response->expects(self::once()) |
139
|
|
|
->method('getStatusCode') |
140
|
|
|
->willReturn(Response::STATUS_CODE_200); |
141
|
|
|
|
142
|
|
|
$response->expects(self::once()) |
143
|
|
|
->method('getHeaders') |
144
|
|
|
->willReturn($headers); |
145
|
|
|
|
146
|
|
|
$response->expects(self::once()) |
147
|
|
|
->method('getBody') |
148
|
|
|
->willReturn('{"data": [0]}'); |
149
|
|
|
|
150
|
|
|
$this->settingsMock->expects(self::once()) |
151
|
|
|
->method('getTimeout') |
152
|
|
|
->willReturn(10); |
153
|
|
|
|
154
|
|
|
$this->settingsMock->expects(self::once()) |
155
|
|
|
->method('getTokenType') |
156
|
|
|
->willReturn($testTokenType); |
157
|
|
|
|
158
|
|
|
$this->settingsMock->expects(self::once()) |
159
|
|
|
->method('getToken') |
160
|
|
|
->willReturn($testToken); |
161
|
|
|
|
162
|
|
|
$this->settingsMock->expects(self::once()) |
163
|
|
|
->method('getAuthOptions') |
164
|
|
|
->willReturn($authOptions); |
165
|
|
|
|
166
|
|
|
$this->httpClientMock->expects(self::once()) |
167
|
|
|
->method('send') |
168
|
|
|
->with( |
169
|
|
|
self::logicalAnd( |
170
|
|
|
self::isInstanceOf(RequestInterface::class), |
171
|
|
|
self::callback(function ($request) { |
172
|
|
|
/** @var Request $request */ |
173
|
|
|
return $request->getAllowCustomMethods() === false; |
174
|
|
|
}) |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
->willReturn($response); |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
181
|
|
|
|
182
|
|
|
self::assertInternalType( |
183
|
|
|
'array', |
184
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testDispatchRequestFile() :void |
189
|
|
|
{ |
190
|
|
|
$testTokenType = 'testtokentype'; |
191
|
|
|
$testToken = 'testtoken'; |
192
|
|
|
$testReqRoute = 'testroute'; |
193
|
|
|
|
194
|
|
|
$header = $this->createMock(HeaderInterface::class); |
195
|
|
|
$header->expects(self::once()) |
196
|
|
|
->method('getFieldValue') |
197
|
|
|
->willReturn('attachment; filename="test.pdf"'); |
198
|
|
|
|
199
|
|
|
$headers = $this->createMock(Headers::class); |
200
|
|
|
$headers->expects(self::once()) |
201
|
|
|
->method('get') |
202
|
|
|
->with(self::stringContains('Content-disposition')) |
203
|
|
|
->willReturn($header); |
204
|
|
|
|
205
|
|
|
$response = $this->createMock(Response::class); |
206
|
|
|
$response->expects(self::once()) |
207
|
|
|
->method('getStatusCode') |
208
|
|
|
->willReturn(Response::STATUS_CODE_200); |
209
|
|
|
|
210
|
|
|
$response->expects(self::once()) |
211
|
|
|
->method('getHeaders') |
212
|
|
|
->willReturn($headers); |
213
|
|
|
|
214
|
|
|
$response->expects(self::once()) |
215
|
|
|
->method('getBody') |
216
|
|
|
->willReturn('testfilecontent'); |
217
|
|
|
|
218
|
|
|
$this->settingsMock->expects(self::once()) |
219
|
|
|
->method('getTimeout') |
220
|
|
|
->willReturn(10); |
221
|
|
|
|
222
|
|
|
$this->settingsMock->expects(self::once()) |
223
|
|
|
->method('getTokenType') |
224
|
|
|
->willReturn($testTokenType); |
225
|
|
|
|
226
|
|
|
$this->settingsMock->expects(self::once()) |
227
|
|
|
->method('getToken') |
228
|
|
|
->willReturn($testToken); |
229
|
|
|
|
230
|
|
|
$this->httpClientMock->expects(self::once()) |
231
|
|
|
->method('send') |
232
|
|
|
->with( |
233
|
|
|
self::logicalAnd( |
234
|
|
|
self::isInstanceOf(RequestInterface::class), |
235
|
|
|
self::callback(function ($request) { |
236
|
|
|
/** @var Request $request*/ |
237
|
|
|
return $request->getAllowCustomMethods() === false; |
238
|
|
|
}) |
239
|
|
|
) |
240
|
|
|
) |
241
|
|
|
->willReturn($response); |
242
|
|
|
|
243
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
244
|
|
|
$result = $this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions); |
245
|
|
|
self::assertInternalType('array', $result); |
246
|
|
|
self::assertArrayHasKey('test.pdf', $result); |
247
|
|
|
self::assertEquals('testfilecontent', $result['test.pdf']); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testDispatchRequestFileInvalidHeader() :void |
251
|
|
|
{ |
252
|
|
|
$testTokenType = 'testtokentype'; |
253
|
|
|
$testToken = 'testtoken'; |
254
|
|
|
$testReqRoute = 'testroute'; |
255
|
|
|
|
256
|
|
|
$header = $this->createMock(HeaderInterface::class); |
257
|
|
|
$header->expects(self::once()) |
258
|
|
|
->method('getFieldValue') |
259
|
|
|
->willReturn('attachTESTment; filename="test.pdf"'); |
260
|
|
|
|
261
|
|
|
$headers = $this->createMock(Headers::class); |
262
|
|
|
$headers->expects(self::once()) |
263
|
|
|
->method('get') |
264
|
|
|
->with(self::stringContains('Content-disposition')) |
265
|
|
|
->willReturn($header); |
266
|
|
|
|
267
|
|
|
$response = $this->createMock(Response::class); |
268
|
|
|
$response->expects(self::once()) |
269
|
|
|
->method('getStatusCode') |
270
|
|
|
->willReturn(Response::STATUS_CODE_200); |
271
|
|
|
|
272
|
|
|
$response->expects(self::once()) |
273
|
|
|
->method('getHeaders') |
274
|
|
|
->willReturn($headers); |
275
|
|
|
|
276
|
|
|
$response->expects(self::never()) |
277
|
|
|
->method('getBody') |
278
|
|
|
->willReturn('testfilecontent'); |
279
|
|
|
|
280
|
|
|
$this->settingsMock->expects(self::once()) |
281
|
|
|
->method('getTimeout') |
282
|
|
|
->willReturn(10); |
283
|
|
|
|
284
|
|
|
$this->settingsMock->expects(self::once()) |
285
|
|
|
->method('getTokenType') |
286
|
|
|
->willReturn($testTokenType); |
287
|
|
|
|
288
|
|
|
$this->settingsMock->expects(self::once()) |
289
|
|
|
->method('getToken') |
290
|
|
|
->willReturn($testToken); |
291
|
|
|
|
292
|
|
|
$this->httpClientMock->expects(self::once()) |
293
|
|
|
->method('send') |
294
|
|
|
->with( |
295
|
|
|
self::logicalAnd( |
296
|
|
|
self::isInstanceOf(RequestInterface::class), |
297
|
|
|
self::callback(function ($request) { |
298
|
|
|
/** @var Request $request*/ |
299
|
|
|
return $request->getAllowCustomMethods() === false; |
300
|
|
|
}) |
301
|
|
|
) |
302
|
|
|
) |
303
|
|
|
->willReturn($response); |
304
|
|
|
|
305
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
306
|
|
|
$result = $this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions); |
307
|
|
|
self::assertInternalType('array', $result); |
308
|
|
|
self::assertEmpty($result); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testDispatchRequestWithOptions() :void |
312
|
|
|
{ |
313
|
|
|
$testTokenType = 'testtokentype'; |
314
|
|
|
$testToken = 'testtoken'; |
315
|
|
|
$testReqRoute = 'testroute'; |
316
|
|
|
|
317
|
|
|
$this->requestOptions->expects(self::once()) |
318
|
|
|
->method('getQueryArray') |
319
|
|
|
->willReturn([]); |
320
|
|
|
|
321
|
|
|
$headers = $this->createMock(Headers::class); |
322
|
|
|
$headers->expects(self::once()) |
323
|
|
|
->method('get') |
324
|
|
|
->with(self::stringContains('Content-disposition')) |
325
|
|
|
->willReturn(false); |
326
|
|
|
|
327
|
|
|
$response = $this->createMock(Response::class); |
328
|
|
|
$response->expects(self::once()) |
329
|
|
|
->method('getStatusCode') |
330
|
|
|
->willReturn(Response::STATUS_CODE_200); |
331
|
|
|
|
332
|
|
|
$response->expects(self::once()) |
333
|
|
|
->method('getHeaders') |
334
|
|
|
->willReturn($headers); |
335
|
|
|
|
336
|
|
|
$response->expects(self::once()) |
337
|
|
|
->method('getBody') |
338
|
|
|
->willReturn('{"data": [0]}'); |
339
|
|
|
|
340
|
|
|
$this->settingsMock->expects(self::once()) |
341
|
|
|
->method('getTimeout') |
342
|
|
|
->willReturn(10); |
343
|
|
|
|
344
|
|
|
$this->settingsMock->expects(self::once()) |
345
|
|
|
->method('getTokenType') |
346
|
|
|
->willReturn($testTokenType); |
347
|
|
|
|
348
|
|
|
$this->settingsMock->expects(self::once()) |
349
|
|
|
->method('getToken') |
350
|
|
|
->willReturn($testToken); |
351
|
|
|
|
352
|
|
|
$this->httpClientMock->expects(self::once()) |
353
|
|
|
->method('send') |
354
|
|
|
->with( |
355
|
|
|
self::logicalAnd( |
356
|
|
|
self::isInstanceOf(RequestInterface::class), |
357
|
|
|
self::callback(function ($request) { |
358
|
|
|
/** @var Request $request*/ |
359
|
|
|
return $request->getAllowCustomMethods() === false; |
360
|
|
|
}) |
361
|
|
|
) |
362
|
|
|
) |
363
|
|
|
->willReturn($response); |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
367
|
|
|
self::assertInternalType( |
368
|
|
|
'array', |
369
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
370
|
|
|
); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\EmptyResponseException |
376
|
|
|
*/ |
377
|
|
|
public function testDispatchRequestEmptyExceptionEmptyData() :void |
378
|
|
|
{ |
379
|
|
|
$testTokenType = 'testtokentype'; |
380
|
|
|
$testToken = 'testtoken'; |
381
|
|
|
$testReqRoute = 'testroute'; |
382
|
|
|
|
383
|
|
|
$headers = $this->createMock(Headers::class); |
384
|
|
|
$headers->expects(self::once()) |
385
|
|
|
->method('get') |
386
|
|
|
->with(self::stringContains('Content-disposition')) |
387
|
|
|
->willReturn(false); |
388
|
|
|
|
389
|
|
|
$response = $this->createMock(Response::class); |
390
|
|
|
$response->expects(self::once()) |
391
|
|
|
->method('getStatusCode') |
392
|
|
|
->willReturn(Response::STATUS_CODE_200); |
393
|
|
|
|
394
|
|
|
$response->expects(self::once()) |
395
|
|
|
->method('getHeaders') |
396
|
|
|
->willReturn($headers); |
397
|
|
|
|
398
|
|
|
$response->expects(self::once()) |
399
|
|
|
->method('getBody') |
400
|
|
|
->willReturn('{"data": []}'); |
401
|
|
|
|
402
|
|
|
$this->settingsMock->expects(self::once()) |
403
|
|
|
->method('getTimeout') |
404
|
|
|
->willReturn(10); |
405
|
|
|
|
406
|
|
|
$this->settingsMock->expects(self::once()) |
407
|
|
|
->method('getTokenType') |
408
|
|
|
->willReturn($testTokenType); |
409
|
|
|
|
410
|
|
|
$this->settingsMock->expects(self::once()) |
411
|
|
|
->method('getToken') |
412
|
|
|
->willReturn($testToken); |
413
|
|
|
|
414
|
|
|
$this->httpClientMock->expects(self::once()) |
415
|
|
|
->method('send') |
416
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
417
|
|
|
->willReturn($response); |
418
|
|
|
|
419
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
420
|
|
|
self::assertInternalType( |
421
|
|
|
'array', |
422
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
423
|
|
|
); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\EmptyResponseException |
428
|
|
|
*/ |
429
|
|
|
public function testDispatchRequestEmptyExceptionMissingData() :void |
430
|
|
|
{ |
431
|
|
|
$testTokenType = 'testtokentype'; |
432
|
|
|
$testToken = 'testtoken'; |
433
|
|
|
$testReqRoute = 'testroute'; |
434
|
|
|
|
435
|
|
|
$headers = $this->createMock(Headers::class); |
436
|
|
|
$headers->expects(self::once()) |
437
|
|
|
->method('get') |
438
|
|
|
->with(self::stringContains('Content-disposition')) |
439
|
|
|
->willReturn(false); |
440
|
|
|
|
441
|
|
|
$response = $this->createMock(Response::class); |
442
|
|
|
$response->expects(self::once()) |
443
|
|
|
->method('getStatusCode') |
444
|
|
|
->willReturn(Response::STATUS_CODE_200); |
445
|
|
|
|
446
|
|
|
$response->expects(self::once()) |
447
|
|
|
->method('getHeaders') |
448
|
|
|
->willReturn($headers); |
449
|
|
|
|
450
|
|
|
$response->expects(self::once()) |
451
|
|
|
->method('getBody') |
452
|
|
|
->willReturn('{}'); |
453
|
|
|
|
454
|
|
|
$this->settingsMock->expects(self::once()) |
455
|
|
|
->method('getTimeout') |
456
|
|
|
->willReturn(10); |
457
|
|
|
|
458
|
|
|
$this->settingsMock->expects(self::once()) |
459
|
|
|
->method('getTokenType') |
460
|
|
|
->willReturn($testTokenType); |
461
|
|
|
|
462
|
|
|
$this->settingsMock->expects(self::once()) |
463
|
|
|
->method('getToken') |
464
|
|
|
->willReturn($testToken); |
465
|
|
|
|
466
|
|
|
$this->httpClientMock->expects(self::once()) |
467
|
|
|
->method('send') |
468
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
469
|
|
|
->willReturn($response); |
470
|
|
|
|
471
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
472
|
|
|
self::assertInternalType( |
473
|
|
|
'array', |
474
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
475
|
|
|
); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\HttpClientException |
480
|
|
|
*/ |
481
|
|
|
public function testDispatchRequestHttpClientException() :void |
482
|
|
|
{ |
483
|
|
|
$testTokenType = 'testtokentype'; |
484
|
|
|
$testToken = 'testtoken'; |
485
|
|
|
$testReqRoute = 'testroute'; |
486
|
|
|
|
487
|
|
|
$response = $this->createMock(Response::class); |
488
|
|
|
$response->expects(self::exactly(2)) |
489
|
|
|
->method('getStatusCode') |
490
|
|
|
->willReturn(Response::STATUS_CODE_500); |
491
|
|
|
|
492
|
|
|
$this->settingsMock->expects(self::once()) |
493
|
|
|
->method('getTimeout') |
494
|
|
|
->willReturn(10); |
495
|
|
|
|
496
|
|
|
$this->settingsMock->expects(self::once()) |
497
|
|
|
->method('getTokenType') |
498
|
|
|
->willReturn($testTokenType); |
499
|
|
|
|
500
|
|
|
$this->settingsMock->expects(self::once()) |
501
|
|
|
->method('getToken') |
502
|
|
|
->willReturn($testToken); |
503
|
|
|
|
504
|
|
|
$this->httpClientMock->expects(self::once()) |
505
|
|
|
->method('send') |
506
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
507
|
|
|
->willReturn($response); |
508
|
|
|
|
509
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
510
|
|
|
self::assertInternalType( |
511
|
|
|
'array', |
512
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
513
|
|
|
); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\HttpClientException |
518
|
|
|
*/ |
519
|
|
|
public function testDispatchRequestHttpAuthClientException() :void |
520
|
|
|
{ |
521
|
|
|
$testTokenType = 'testtokentype'; |
522
|
|
|
$testToken = 'testtoken'; |
523
|
|
|
$testReqRoute = 'testroute'; |
524
|
|
|
|
525
|
|
|
$response = $this->createMock(Response::class); |
526
|
|
|
$response->expects(self::exactly(2)) |
527
|
|
|
->method('getStatusCode') |
528
|
|
|
->willReturn(Response::STATUS_CODE_401); |
529
|
|
|
|
530
|
|
|
$this->settingsMock->expects(self::once()) |
531
|
|
|
->method('getTimeout') |
532
|
|
|
->willReturn(10); |
533
|
|
|
|
534
|
|
|
$this->settingsMock->expects(self::once()) |
535
|
|
|
->method('getTokenType') |
536
|
|
|
->willReturn($testTokenType); |
537
|
|
|
|
538
|
|
|
$this->settingsMock->expects(self::once()) |
539
|
|
|
->method('getToken') |
540
|
|
|
->willReturn($testToken); |
541
|
|
|
|
542
|
|
|
$this->httpClientMock->expects(self::once()) |
543
|
|
|
->method('send') |
544
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
545
|
|
|
->willReturn($response); |
546
|
|
|
|
547
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
548
|
|
|
self::assertInternalType( |
549
|
|
|
'array', |
550
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
551
|
|
|
); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\ApiAuthException |
556
|
|
|
*/ |
557
|
|
|
public function testDispatchRequestApiAuthClientException() :void |
558
|
|
|
{ |
559
|
|
|
$testTokenType = 'testtokentype'; |
560
|
|
|
$testToken = 'testtoken'; |
561
|
|
|
$testReqRoute = 'testroute'; |
562
|
|
|
|
563
|
|
|
$response = $this->createMock(Response::class); |
564
|
|
|
$response->expects(self::exactly(2)) |
565
|
|
|
->method('getStatusCode') |
566
|
|
|
->willReturn(Response::STATUS_CODE_403); |
567
|
|
|
|
568
|
|
|
$this->settingsMock->expects(self::once()) |
569
|
|
|
->method('getTimeout') |
570
|
|
|
->willReturn(10); |
571
|
|
|
|
572
|
|
|
$this->settingsMock->expects(self::once()) |
573
|
|
|
->method('getTokenType') |
574
|
|
|
->willReturn($testTokenType); |
575
|
|
|
|
576
|
|
|
$this->settingsMock->expects(self::once()) |
577
|
|
|
->method('getToken') |
578
|
|
|
->willReturn($testToken); |
579
|
|
|
|
580
|
|
|
$this->httpClientMock->expects(self::once()) |
581
|
|
|
->method('send') |
582
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
583
|
|
|
->willReturn($response); |
584
|
|
|
|
585
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
586
|
|
|
self::assertInternalType( |
587
|
|
|
'array', |
588
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
589
|
|
|
); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\HttpClientException |
594
|
|
|
*/ |
595
|
|
|
public function testDispatchRequestHttpClientExceptionInvalidArgument() :void |
596
|
|
|
{ |
597
|
|
|
$this->reqMethod = 'TESTPUT'; |
598
|
|
|
$testReqRoute = 'testroute'; |
599
|
|
|
|
600
|
|
|
self::assertInternalType( |
601
|
|
|
'array', |
602
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
603
|
|
|
); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\HttpClientException |
608
|
|
|
*/ |
609
|
|
|
public function testDispatchRequestHttpClientExceptionInvalidRuntime() :void |
610
|
|
|
{ |
611
|
|
|
$testTokenType = 'testtokentype'; |
612
|
|
|
$testToken = 'testtoken'; |
613
|
|
|
$testReqRoute = 'testroute'; |
614
|
|
|
|
615
|
|
|
$this->settingsMock->expects(self::once()) |
616
|
|
|
->method('getTimeout') |
617
|
|
|
->willReturn(10); |
618
|
|
|
|
619
|
|
|
$this->settingsMock->expects(self::once()) |
620
|
|
|
->method('getTokenType') |
621
|
|
|
->willReturn($testTokenType); |
622
|
|
|
|
623
|
|
|
$this->settingsMock->expects(self::once()) |
624
|
|
|
->method('getToken') |
625
|
|
|
->willReturn($testToken); |
626
|
|
|
|
627
|
|
|
$this->httpClientMock->expects(self::once()) |
628
|
|
|
->method('send') |
629
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
630
|
|
|
->willThrowException(new Client\Exception\RuntimeException()); |
631
|
|
|
|
632
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
633
|
|
|
self::assertInternalType( |
634
|
|
|
'array', |
635
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
636
|
|
|
); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\EmptyResponseException |
641
|
|
|
*/ |
642
|
|
|
public function testDispatchRequestEmpty() :void |
643
|
|
|
{ |
644
|
|
|
$testTokenType = 'testtokentype'; |
645
|
|
|
$testToken = 'testtoken'; |
646
|
|
|
$testReqRoute = 'testroute'; |
647
|
|
|
|
648
|
|
|
$headers = $this->createMock(Headers::class); |
649
|
|
|
$headers->expects(self::once()) |
650
|
|
|
->method('get') |
651
|
|
|
->with(self::stringContains('Content-disposition')) |
652
|
|
|
->willReturn(false); |
653
|
|
|
|
654
|
|
|
$response = $this->createMock(Response::class); |
655
|
|
|
$response->expects(self::once()) |
656
|
|
|
->method('getStatusCode') |
657
|
|
|
->willReturn(Response::STATUS_CODE_200); |
658
|
|
|
|
659
|
|
|
$response->expects(self::once()) |
660
|
|
|
->method('getHeaders') |
661
|
|
|
->willReturn($headers); |
662
|
|
|
|
663
|
|
|
$response->expects(self::once()) |
664
|
|
|
->method('getBody') |
665
|
|
|
->willReturn(''); |
666
|
|
|
|
667
|
|
|
$this->settingsMock->expects(self::once()) |
668
|
|
|
->method('getTimeout') |
669
|
|
|
->willReturn(10); |
670
|
|
|
|
671
|
|
|
$this->settingsMock->expects(self::once()) |
672
|
|
|
->method('getTokenType') |
673
|
|
|
->willReturn($testTokenType); |
674
|
|
|
|
675
|
|
|
$this->settingsMock->expects(self::once()) |
676
|
|
|
->method('getToken') |
677
|
|
|
->willReturn($testToken); |
678
|
|
|
|
679
|
|
|
$this->httpClientMock->expects(self::once()) |
680
|
|
|
->method('send') |
681
|
|
|
->with(self::isInstanceOf(RequestInterface::class)) |
682
|
|
|
->willReturn($response); |
683
|
|
|
|
684
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
685
|
|
|
self::assertInternalType( |
686
|
|
|
'array', |
687
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
688
|
|
|
); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
public function testDispatchRequestPost() :void |
692
|
|
|
{ |
693
|
|
|
$testTokenType = 'testtokentype'; |
694
|
|
|
$testToken = 'testtoken'; |
695
|
|
|
$testReqRoute = 'testroute'; |
696
|
|
|
|
697
|
|
|
|
698
|
|
|
$this->requestOptions = $this->createMock(RequestOptionsInterface::class); |
699
|
|
|
$this->requestOptions->expects(self::once()) |
700
|
|
|
->method('getPostArray') |
701
|
|
|
->willReturn(['test']); |
702
|
|
|
|
703
|
|
|
$headers = $this->createMock(Headers::class); |
704
|
|
|
$headers->expects(self::once()) |
705
|
|
|
->method('get') |
706
|
|
|
->with(self::stringContains('Content-disposition')) |
707
|
|
|
->willReturn(false); |
708
|
|
|
|
709
|
|
|
$response = $this->createMock(Response::class); |
710
|
|
|
$response->expects(self::once()) |
711
|
|
|
->method('getStatusCode') |
712
|
|
|
->willReturn(Response::STATUS_CODE_200); |
713
|
|
|
|
714
|
|
|
$response->expects(self::once()) |
715
|
|
|
->method('getHeaders') |
716
|
|
|
->willReturn($headers); |
717
|
|
|
|
718
|
|
|
$response->expects(self::once()) |
719
|
|
|
->method('getBody') |
720
|
|
|
->willReturn('{"data": [0]}'); |
721
|
|
|
|
722
|
|
|
$this->settingsMock->expects(self::once()) |
723
|
|
|
->method('getTimeout') |
724
|
|
|
->willReturn(10); |
725
|
|
|
|
726
|
|
|
$this->settingsMock->expects(self::once()) |
727
|
|
|
->method('getTokenType') |
728
|
|
|
->willReturn($testTokenType); |
729
|
|
|
|
730
|
|
|
$this->settingsMock->expects(self::once()) |
731
|
|
|
->method('getToken') |
732
|
|
|
->willReturn($testToken); |
733
|
|
|
|
734
|
|
|
$this->httpClientMock->expects(self::once()) |
735
|
|
|
->method('send') |
736
|
|
|
->with( |
737
|
|
|
self::logicalAnd( |
738
|
|
|
self::isInstanceOf(RequestInterface::class), |
739
|
|
|
self::callback(function ($request) { |
740
|
|
|
$return = true; |
741
|
|
|
/** @var Request $request */ |
742
|
|
|
if ($request->getContent() === '') { |
743
|
|
|
$return = false; |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
if ($request->getAllowCustomMethods() === true) { |
747
|
|
|
$return = false; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
return $return; |
751
|
|
|
}) |
752
|
|
|
) |
753
|
|
|
) |
754
|
|
|
->willReturn($response); |
755
|
|
|
|
756
|
|
|
$this->manager = new RequestService($this->settingsMock, $this->httpClientMock); |
757
|
|
|
self::assertInternalType( |
758
|
|
|
'array', |
759
|
|
|
$this->manager->dispatchRequest($this->reqMethod, $testReqRoute, $this->requestOptions) |
760
|
|
|
); |
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|