1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace InvoiceNinjaModuleTest\Service; |
4
|
|
|
|
5
|
|
|
use InvoiceNinjaModule\Exception\ApiException; |
6
|
|
|
use InvoiceNinjaModule\Exception\EmptyResponseException; |
7
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
8
|
|
|
use InvoiceNinjaModule\Model\Interfaces\RequestOptionsInterface; |
9
|
|
|
use InvoiceNinjaModule\Service\Interfaces\RequestServiceInterface; |
10
|
|
|
use InvoiceNinjaModule\Service\ObjectService; |
11
|
|
|
use InvoiceNinjaModule\Service\RequestService; |
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() |
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() |
39
|
|
|
{ |
40
|
|
|
self::assertInstanceOf(ObjectService::class, $this->objectManager); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCreateObject() |
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() |
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() |
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(BaseInterface::class, $this->objectManager->getObjectById($baseMock, 777, $this->testRoute)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\NotFoundException |
126
|
|
|
*/ |
127
|
|
|
public function testGetObjectByIdException() |
128
|
|
|
{ |
129
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
130
|
|
|
|
131
|
|
|
$this->requestServiceMock->expects(self::once()) |
132
|
|
|
->method('dispatchRequest') |
133
|
|
|
->with( |
134
|
|
|
self::stringContains(Request::METHOD_GET), |
135
|
|
|
self::stringContains($this->testRoute), |
136
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
137
|
|
|
) |
138
|
|
|
->willThrowException(new ApiException()); |
139
|
|
|
|
140
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->getObjectById($baseMock, 777, $this->testRoute)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\InvalidParameterException |
145
|
|
|
*/ |
146
|
|
|
public function testFindObjectByException() |
147
|
|
|
{ |
148
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
149
|
|
|
$searchTerm = []; |
150
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testFindObjectByEmpty() |
154
|
|
|
{ |
155
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
156
|
|
|
$searchTerm = ['test' => 'tester123']; |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->hydratorMock->expects(self::never()) |
160
|
|
|
->method('hydrate'); |
161
|
|
|
|
162
|
|
|
$this->requestServiceMock->expects(self::once()) |
163
|
|
|
->method('dispatchRequest') |
164
|
|
|
->with( |
165
|
|
|
self::stringContains(Request::METHOD_GET), |
166
|
|
|
self::stringContains($this->testRoute), |
167
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
168
|
|
|
) |
169
|
|
|
->willReturn([]); |
170
|
|
|
|
171
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
172
|
|
|
|
173
|
|
|
self::assertEmpty($result); |
174
|
|
|
self::assertInternalType('array', $result); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testFindObjectByEmptyException() |
178
|
|
|
{ |
179
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
180
|
|
|
$searchTerm = ['test' => 'tester123']; |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
$this->hydratorMock->expects(self::never()) |
184
|
|
|
->method('hydrate'); |
185
|
|
|
|
186
|
|
|
$this->requestServiceMock->expects(self::once()) |
187
|
|
|
->method('dispatchRequest') |
188
|
|
|
->with( |
189
|
|
|
self::stringContains(Request::METHOD_GET), |
190
|
|
|
self::stringContains($this->testRoute), |
191
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
192
|
|
|
) |
193
|
|
|
->willThrowException(new EmptyResponseException()); |
194
|
|
|
|
195
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
196
|
|
|
|
197
|
|
|
self::assertEmpty($result); |
198
|
|
|
self::assertInternalType('array', $result); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testFindObjectBy() |
202
|
|
|
{ |
203
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
204
|
|
|
$searchTerm = ['test' => 'tester123']; |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
$this->hydratorMock->expects(self::exactly(2)) |
208
|
|
|
->method('hydrate') |
209
|
|
|
->with( |
210
|
|
|
self::isType('array'), |
211
|
|
|
self::isInstanceOf(BaseInterface::class) |
212
|
|
|
) |
213
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
214
|
|
|
|
215
|
|
|
$this->requestServiceMock->expects(self::once()) |
216
|
|
|
->method('dispatchRequest') |
217
|
|
|
->with( |
218
|
|
|
self::stringContains(Request::METHOD_GET), |
219
|
|
|
self::stringContains($this->testRoute), |
220
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
221
|
|
|
) |
222
|
|
|
->willReturn(['test1' => [], 'test2' => [],]); |
223
|
|
|
|
224
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
225
|
|
|
|
226
|
|
|
self::assertNotEmpty($result); |
227
|
|
|
self::assertInternalType('array', $result); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\ApiException |
233
|
|
|
*/ |
234
|
|
|
public function testFindObjectByApiException() |
235
|
|
|
{ |
236
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
237
|
|
|
$searchTerm = ['test' => 'tester123']; |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
$this->hydratorMock->expects(self::never()) |
241
|
|
|
->method('hydrate'); |
242
|
|
|
|
243
|
|
|
$this->requestServiceMock->expects(self::once()) |
244
|
|
|
->method('dispatchRequest') |
245
|
|
|
->with( |
246
|
|
|
self::stringContains(Request::METHOD_GET), |
247
|
|
|
self::stringContains($this->testRoute), |
248
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
249
|
|
|
) |
250
|
|
|
->willThrowException(new ApiException()); |
251
|
|
|
|
252
|
|
|
$result = $this->objectManager->findObjectBy($baseMock, $searchTerm, $this->testRoute); |
253
|
|
|
|
254
|
|
|
self::assertEmpty($result); |
255
|
|
|
self::assertInternalType('array', $result); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
|
260
|
|
|
|
261
|
|
|
public function testUpdate() |
262
|
|
|
{ |
263
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
264
|
|
|
$baseMock->expects(self::once()) |
265
|
|
|
->method('getId') |
266
|
|
|
->willReturn(777); |
267
|
|
|
|
268
|
|
|
$this->hydratorMock->expects(self::once()) |
269
|
|
|
->method('extract') |
270
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
271
|
|
|
->willReturn([]); |
272
|
|
|
|
273
|
|
|
$this->hydratorMock->expects(self::once()) |
274
|
|
|
->method('hydrate') |
275
|
|
|
->with( |
276
|
|
|
self::isType('array'), |
277
|
|
|
self::isInstanceOf(BaseInterface::class) |
278
|
|
|
) |
279
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
280
|
|
|
|
281
|
|
|
$this->requestServiceMock->expects(self::once()) |
282
|
|
|
->method('dispatchRequest') |
283
|
|
|
->with( |
284
|
|
|
self::stringContains(Request::METHOD_PUT), |
285
|
|
|
self::stringContains($this->testRoute), |
286
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
287
|
|
|
) |
288
|
|
|
->willReturn([]); |
289
|
|
|
|
290
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->updateObject($baseMock, $this->testRoute)); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testArchive() |
294
|
|
|
{ |
295
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
296
|
|
|
$baseMock->expects(self::once()) |
297
|
|
|
->method('getId') |
298
|
|
|
->willReturn(777); |
299
|
|
|
|
300
|
|
|
$this->hydratorMock->expects(self::never()) |
301
|
|
|
->method('extract') |
302
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
303
|
|
|
->willReturn([]); |
304
|
|
|
|
305
|
|
|
$this->hydratorMock->expects(self::once()) |
306
|
|
|
->method('hydrate') |
307
|
|
|
->with( |
308
|
|
|
self::isType('array'), |
309
|
|
|
self::isInstanceOf(BaseInterface::class) |
310
|
|
|
) |
311
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
312
|
|
|
|
313
|
|
|
$this->requestServiceMock->expects(self::once()) |
314
|
|
|
->method('dispatchRequest') |
315
|
|
|
->with( |
316
|
|
|
self::stringContains(Request::METHOD_PUT), |
317
|
|
|
self::stringContains($this->testRoute), |
318
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
319
|
|
|
) |
320
|
|
|
->willReturn([]); |
321
|
|
|
|
322
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->archiveObject($baseMock, $this->testRoute)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testRestore() |
326
|
|
|
{ |
327
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
328
|
|
|
$baseMock->expects(self::once()) |
329
|
|
|
->method('getId') |
330
|
|
|
->willReturn(777); |
331
|
|
|
|
332
|
|
|
$this->hydratorMock->expects(self::never()) |
333
|
|
|
->method('extract') |
334
|
|
|
->with(self::isInstanceOf(BaseInterface::class)) |
335
|
|
|
->willReturn([]); |
336
|
|
|
|
337
|
|
|
$this->hydratorMock->expects(self::once()) |
338
|
|
|
->method('hydrate') |
339
|
|
|
->with( |
340
|
|
|
self::isType('array'), |
341
|
|
|
self::isInstanceOf(BaseInterface::class) |
342
|
|
|
) |
343
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
344
|
|
|
|
345
|
|
|
$this->requestServiceMock->expects(self::once()) |
346
|
|
|
->method('dispatchRequest') |
347
|
|
|
->with( |
348
|
|
|
self::stringContains(Request::METHOD_PUT), |
349
|
|
|
self::stringContains($this->testRoute), |
350
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
351
|
|
|
) |
352
|
|
|
->willReturn([]); |
353
|
|
|
|
354
|
|
|
self::assertInstanceOf(BaseInterface::class, $this->objectManager->restoreObject($baseMock, $this->testRoute)); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function testGetAllObjectsEmpty() |
358
|
|
|
{ |
359
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
360
|
|
|
|
361
|
|
|
$this->requestServiceMock->expects(self::once()) |
362
|
|
|
->method('dispatchRequest') |
363
|
|
|
->with( |
364
|
|
|
self::stringContains(Request::METHOD_GET), |
365
|
|
|
self::stringContains($this->testRoute), |
366
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
367
|
|
|
) |
368
|
|
|
->willReturn([]); |
369
|
|
|
|
370
|
|
|
self::assertInternalType('array', $this->objectManager->getAllObjects($baseMock, $this->testRoute)); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function testGetAllClients() |
374
|
|
|
{ |
375
|
|
|
$baseMock = $this->createMock(BaseInterface::class); |
376
|
|
|
|
377
|
|
|
$this->requestServiceMock->expects(self::once()) |
378
|
|
|
->method('dispatchRequest') |
379
|
|
|
->with( |
380
|
|
|
self::stringContains(Request::METHOD_GET), |
381
|
|
|
self::stringContains($this->testRoute), |
382
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
383
|
|
|
) |
384
|
|
|
->willReturn(['test' => ['id' => 1] ]); |
385
|
|
|
|
386
|
|
|
$this->hydratorMock->expects(self::once()) |
387
|
|
|
->method('hydrate') |
388
|
|
|
->with( |
389
|
|
|
self::isType('array'), |
390
|
|
|
self::isInstanceOf(BaseInterface::class) |
391
|
|
|
) |
392
|
|
|
->willReturn($this->createMock(BaseInterface::class)); |
393
|
|
|
|
394
|
|
|
self::assertInternalType('array', $this->objectManager->getAllObjects($baseMock, $this->testRoute)); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function testDownloadFile() |
398
|
|
|
{ |
399
|
|
|
$this->requestServiceMock->expects(self::once()) |
400
|
|
|
->method('dispatchRequest') |
401
|
|
|
->with( |
402
|
|
|
self::stringContains(Request::METHOD_GET), |
403
|
|
|
self::stringContains('/download'), |
404
|
|
|
self::isInstanceOf(RequestOptionsInterface::class) |
405
|
|
|
) |
406
|
|
|
->willReturn(['test' => 'test2' ]); |
407
|
|
|
self::assertInternalType('array', $this->objectManager->downloadFile(1)); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|