|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModuleTest\Service; |
|
5
|
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Exception\ApiException; |
|
7
|
|
|
use InvoiceNinjaModule\Exception\EmptyResponseException; |
|
8
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
|
9
|
|
|
use InvoiceNinjaModule\Model\Interfaces\RequestOptionsInterface; |
|
10
|
|
|
use InvoiceNinjaModule\Service\Interfaces\RequestServiceInterface; |
|
11
|
|
|
use InvoiceNinjaModule\Service\ObjectService; |
|
12
|
|
|
use Zend\Http\Request; |
|
13
|
|
|
use Zend\Hydrator\HydratorInterface; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class ObjectManagerTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ObjectService */ |
|
19
|
|
|
private $objectManager; |
|
20
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
21
|
|
|
private $requestServiceMock; |
|
22
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
23
|
|
|
private $hydratorMock; |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $testRoute; |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp() :void |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->requestServiceMock = $this->createMock(RequestServiceInterface::class); |
|
32
|
|
|
$this->hydratorMock = $this->createMock(HydratorInterface::class); |
|
33
|
|
|
$this->testRoute = '/tests'; |
|
34
|
|
|
|
|
35
|
|
|
$this->objectManager = new ObjectService($this->requestServiceMock, $this->hydratorMock); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testCreate() :void |
|
39
|
|
|
{ |
|
40
|
|
|
self::assertInstanceOf(ObjectService::class, $this->objectManager); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testCreateObject() :void |
|
44
|
|
|
{ |
|
45
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
46
|
|
|
|
|
47
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
48
|
|
|
->method('dispatchRequest') |
|
49
|
|
|
->with( |
|
50
|
|
|
self::stringContains(Request::METHOD_POST), |
|
51
|
|
|
self::stringContains($this->testRoute), |
|
52
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
53
|
|
|
) |
|
54
|
|
|
->willReturn([]); |
|
55
|
|
|
|
|
56
|
|
|
$this->hydratorMock->expects(self::once()) |
|
57
|
|
|
->method('extract') |
|
58
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
|
59
|
|
|
->willReturn([]); |
|
60
|
|
|
|
|
61
|
|
|
$this->hydratorMock->expects(self::once()) |
|
62
|
|
|
->method('hydrate') |
|
63
|
|
|
->with( |
|
64
|
|
|
self::isType('array'), |
|
65
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
66
|
|
|
) |
|
67
|
|
|
->willReturn($baseMock); |
|
68
|
|
|
|
|
69
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->createObject($baseMock, $this->testRoute)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testDeleteObject() :void |
|
73
|
|
|
{ |
|
74
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
75
|
|
|
$baseMock->expects(self::once()) |
|
76
|
|
|
->method('getId') |
|
77
|
|
|
->willReturn(777); |
|
78
|
|
|
|
|
79
|
|
|
$this->hydratorMock->expects(self::once()) |
|
80
|
|
|
->method('hydrate') |
|
81
|
|
|
->with( |
|
82
|
|
|
self::isType('array'), |
|
83
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
84
|
|
|
) |
|
85
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
86
|
|
|
|
|
87
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
88
|
|
|
->method('dispatchRequest') |
|
89
|
|
|
->with( |
|
90
|
|
|
self::stringContains(Request::METHOD_DELETE), |
|
91
|
|
|
self::stringContains($this->testRoute), |
|
92
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
93
|
|
|
) |
|
94
|
|
|
->willReturn([]); |
|
95
|
|
|
|
|
96
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->deleteObject($baseMock, $this->testRoute)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public function testGetObjectById() :void |
|
101
|
|
|
{ |
|
102
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
103
|
|
|
|
|
104
|
|
|
$this->hydratorMock->expects(self::once()) |
|
105
|
|
|
->method('hydrate') |
|
106
|
|
|
->with( |
|
107
|
|
|
self::isType('array'), |
|
108
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
109
|
|
|
) |
|
110
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
111
|
|
|
|
|
112
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
113
|
|
|
->method('dispatchRequest') |
|
114
|
|
|
->with( |
|
115
|
|
|
self::stringContains(Request::METHOD_GET), |
|
116
|
|
|
self::stringContains($this->testRoute), |
|
117
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
118
|
|
|
) |
|
119
|
|
|
->willReturn([]); |
|
120
|
|
|
|
|
121
|
|
|
self::assertInstanceOf( |
|
122
|
|
|
BaseInterface::class, |
|
123
|
|
|
$this->objectManager->getObjectById($baseMock, 777, $this->testRoute) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\NotFoundException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testGetObjectByIdException() :void |
|
131
|
|
|
{ |
|
132
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
133
|
|
|
|
|
134
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
135
|
|
|
->method('dispatchRequest') |
|
136
|
|
|
->with( |
|
137
|
|
|
self::stringContains(Request::METHOD_GET), |
|
138
|
|
|
self::stringContains($this->testRoute), |
|
139
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
140
|
|
|
) |
|
141
|
|
|
->willThrowException(new ApiException()); |
|
142
|
|
|
|
|
143
|
|
|
self::assertInstanceOf( |
|
144
|
|
|
BaseInterface::class, |
|
145
|
|
|
$this->objectManager->getObjectById($baseMock, 777, $this->testRoute) |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\InvalidParameterException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function testFindObjectByException() :void |
|
153
|
|
|
{ |
|
154
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
155
|
|
|
$searchTerm = []; |
|
156
|
|
|
self::assertInstanceOf( |
|
157
|
|
|
BaseInterface::class, |
|
158
|
|
|
$this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testFindObjectByEmpty() :void |
|
163
|
|
|
{ |
|
164
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
165
|
|
|
$searchTerm = ['test' => 'tester123']; |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
$this->hydratorMock->expects(self::never()) |
|
169
|
|
|
->method('hydrate'); |
|
170
|
|
|
|
|
171
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
172
|
|
|
->method('dispatchRequest') |
|
173
|
|
|
->with( |
|
174
|
|
|
self::stringContains(Request::METHOD_GET), |
|
175
|
|
|
self::stringContains($this->testRoute), |
|
176
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
177
|
|
|
) |
|
178
|
|
|
->willReturn([]); |
|
179
|
|
|
|
|
180
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
|
181
|
|
|
|
|
182
|
|
|
self::assertEmpty($result); |
|
183
|
|
|
self::assertInternalType('array', $result); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testFindObjectByEmptyException() :void |
|
187
|
|
|
{ |
|
188
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
189
|
|
|
$searchTerm = ['test' => 'tester123']; |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
$this->hydratorMock->expects(self::never()) |
|
193
|
|
|
->method('hydrate'); |
|
194
|
|
|
|
|
195
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
196
|
|
|
->method('dispatchRequest') |
|
197
|
|
|
->with( |
|
198
|
|
|
self::stringContains(Request::METHOD_GET), |
|
199
|
|
|
self::stringContains($this->testRoute), |
|
200
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
201
|
|
|
) |
|
202
|
|
|
->willThrowException(new EmptyResponseException()); |
|
203
|
|
|
|
|
204
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
|
205
|
|
|
|
|
206
|
|
|
self::assertEmpty($result); |
|
207
|
|
|
self::assertInternalType('array', $result); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testFindObjectBy() :void |
|
211
|
|
|
{ |
|
212
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
213
|
|
|
$searchTerm = ['test' => 'tester123']; |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
$this->hydratorMock->expects(self::exactly(2)) |
|
217
|
|
|
->method('hydrate') |
|
218
|
|
|
->with( |
|
219
|
|
|
self::isType('array'), |
|
220
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
221
|
|
|
) |
|
222
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
223
|
|
|
|
|
224
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
225
|
|
|
->method('dispatchRequest') |
|
226
|
|
|
->with( |
|
227
|
|
|
self::stringContains(Request::METHOD_GET), |
|
228
|
|
|
self::stringContains($this->testRoute), |
|
229
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
230
|
|
|
) |
|
231
|
|
|
->willReturn(['test1' => [], 'test2' => [],]); |
|
232
|
|
|
|
|
233
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
|
234
|
|
|
|
|
235
|
|
|
self::assertNotEmpty($result); |
|
236
|
|
|
self::assertInternalType('array', $result); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\ApiException |
|
241
|
|
|
*/ |
|
242
|
|
|
public function testFindObjectByApiException() :void |
|
243
|
|
|
{ |
|
244
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
245
|
|
|
$searchTerm = ['test' => 'tester123']; |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
$this->hydratorMock->expects(self::never()) |
|
249
|
|
|
->method('hydrate'); |
|
250
|
|
|
|
|
251
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
252
|
|
|
->method('dispatchRequest') |
|
253
|
|
|
->with( |
|
254
|
|
|
self::stringContains(Request::METHOD_GET), |
|
255
|
|
|
self::stringContains($this->testRoute), |
|
256
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
257
|
|
|
) |
|
258
|
|
|
->willThrowException(new ApiException()); |
|
259
|
|
|
|
|
260
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
|
261
|
|
|
|
|
262
|
|
|
self::assertEmpty($result); |
|
263
|
|
|
self::assertInternalType('array', $result); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
public function testUpdate() :void |
|
267
|
|
|
{ |
|
268
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
269
|
|
|
$baseMock->expects(self::once()) |
|
270
|
|
|
->method('getId') |
|
271
|
|
|
->willReturn(777); |
|
272
|
|
|
|
|
273
|
|
|
$this->hydratorMock->expects(self::once()) |
|
274
|
|
|
->method('extract') |
|
275
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
|
276
|
|
|
->willReturn([]); |
|
277
|
|
|
|
|
278
|
|
|
$this->hydratorMock->expects(self::once()) |
|
279
|
|
|
->method('hydrate') |
|
280
|
|
|
->with( |
|
281
|
|
|
self::isType('array'), |
|
282
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
283
|
|
|
) |
|
284
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
285
|
|
|
|
|
286
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
287
|
|
|
->method('dispatchRequest') |
|
288
|
|
|
->with( |
|
289
|
|
|
self::stringContains(Request::METHOD_PUT), |
|
290
|
|
|
self::stringContains($this->testRoute), |
|
291
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
292
|
|
|
) |
|
293
|
|
|
->willReturn([]); |
|
294
|
|
|
|
|
295
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->updateObject($baseMock, $this->testRoute)); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
public function testArchive() :void |
|
299
|
|
|
{ |
|
300
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
301
|
|
|
$baseMock->expects(self::once()) |
|
302
|
|
|
->method('getId') |
|
303
|
|
|
->willReturn(777); |
|
304
|
|
|
|
|
305
|
|
|
$this->hydratorMock->expects(self::never()) |
|
306
|
|
|
->method('extract') |
|
307
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
|
308
|
|
|
->willReturn([]); |
|
309
|
|
|
|
|
310
|
|
|
$this->hydratorMock->expects(self::once()) |
|
311
|
|
|
->method('hydrate') |
|
312
|
|
|
->with( |
|
313
|
|
|
self::isType('array'), |
|
314
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
315
|
|
|
) |
|
316
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
317
|
|
|
|
|
318
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
319
|
|
|
->method('dispatchRequest') |
|
320
|
|
|
->with( |
|
321
|
|
|
self::stringContains(Request::METHOD_PUT), |
|
322
|
|
|
self::stringContains($this->testRoute), |
|
323
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
324
|
|
|
) |
|
325
|
|
|
->willReturn([]); |
|
326
|
|
|
|
|
327
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->archiveObject($baseMock, $this->testRoute)); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public function testRestore() :void |
|
331
|
|
|
{ |
|
332
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
333
|
|
|
$baseMock->expects(self::once()) |
|
334
|
|
|
->method('getId') |
|
335
|
|
|
->willReturn(777); |
|
336
|
|
|
|
|
337
|
|
|
$this->hydratorMock->expects(self::never()) |
|
338
|
|
|
->method('extract') |
|
339
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
|
340
|
|
|
->willReturn([]); |
|
341
|
|
|
|
|
342
|
|
|
$this->hydratorMock->expects(self::once()) |
|
343
|
|
|
->method('hydrate') |
|
344
|
|
|
->with( |
|
345
|
|
|
self::isType('array'), |
|
346
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
347
|
|
|
) |
|
348
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
349
|
|
|
|
|
350
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
351
|
|
|
->method('dispatchRequest') |
|
352
|
|
|
->with( |
|
353
|
|
|
self::stringContains(Request::METHOD_PUT), |
|
354
|
|
|
self::stringContains($this->testRoute), |
|
355
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
356
|
|
|
) |
|
357
|
|
|
->willReturn([]); |
|
358
|
|
|
|
|
359
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->restoreObject($baseMock, $this->testRoute)); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
public function testGetAllObjectsEmpty() :void |
|
363
|
|
|
{ |
|
364
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
365
|
|
|
|
|
366
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
367
|
|
|
->method('dispatchRequest') |
|
368
|
|
|
->with( |
|
369
|
|
|
self::stringContains(Request::METHOD_GET), |
|
370
|
|
|
self::stringContains($this->testRoute), |
|
371
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
372
|
|
|
) |
|
373
|
|
|
->willReturn([]); |
|
374
|
|
|
|
|
375
|
|
|
self::assertInternalType('array', $this->objectManager->getAllObjects($baseMock, $this->testRoute)); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
public function testGetAllClients() :void |
|
379
|
|
|
{ |
|
380
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
|
381
|
|
|
|
|
382
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
383
|
|
|
->method('dispatchRequest') |
|
384
|
|
|
->with( |
|
385
|
|
|
self::stringContains(Request::METHOD_GET), |
|
386
|
|
|
self::stringContains($this->testRoute), |
|
387
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
388
|
|
|
) |
|
389
|
|
|
->willReturn(['test' => ['id' => 1] ]); |
|
390
|
|
|
|
|
391
|
|
|
$this->hydratorMock->expects(self::once()) |
|
392
|
|
|
->method('hydrate') |
|
393
|
|
|
->with( |
|
394
|
|
|
self::isType('array'), |
|
395
|
|
|
self::isInstanceOf(BaseInterface::class) |
|
396
|
|
|
) |
|
397
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
|
398
|
|
|
|
|
399
|
|
|
self::assertInternalType('array', $this->objectManager->getAllObjects($baseMock, $this->testRoute)); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
public function testDownloadFile() :void |
|
403
|
|
|
{ |
|
404
|
|
|
$this->requestServiceMock->expects(self::once()) |
|
405
|
|
|
->method('dispatchRequest') |
|
406
|
|
|
->with( |
|
407
|
|
|
self::stringContains(Request::METHOD_GET), |
|
408
|
|
|
self::stringContains('/download'), |
|
409
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
|
410
|
|
|
) |
|
411
|
|
|
->willReturn(['test' => 'test2' ]); |
|
412
|
|
|
self::assertInternalType('array', $this->objectManager->downloadFile(1)); |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|