Completed
Push — master ( 2a331f...b1d0a6 )
by Al3x
04:54
created

ObjectManagerTest::testGetAllClientsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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