Passed
Push — master ( 9df494...3ab971 )
by Davis
37s
created

TestServices::testIsExceptionService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 7
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 7/23/17
6
 * Time: 2:12 PM
7
 */
8
9
namespace DavisPeixoto\BlogCore\Tests\Services;
10
11
use DateTime;
12
use DavisPeixoto\BlogCore\Entity\AbstractEntity;
13
use DavisPeixoto\BlogCore\Entity\Author;
14
use DavisPeixoto\BlogCore\Entity\Post;
15
use DavisPeixoto\BlogCore\Entity\Tag;
16
use DavisPeixoto\BlogCore\Entity\Trail;
17
use DavisPeixoto\BlogCore\Interfaces\RepositoryInterface;
18
use DavisPeixoto\BlogCore\Interfaces\ServiceInterface;
19
use DavisPeixoto\BlogCore\Repository\AbstractAuthorRepository;
20
use DavisPeixoto\BlogCore\Repository\AbstractPostRepository;
21
use DavisPeixoto\BlogCore\Repository\AbstractTagRepository;
22
use DavisPeixoto\BlogCore\Repository\AbstractTrailRepository;
23
use DavisPeixoto\BlogCore\Service\CreateAuthor;
24
use DavisPeixoto\BlogCore\Service\CreatePost;
25
use DavisPeixoto\BlogCore\Service\CreateTag;
26
use DavisPeixoto\BlogCore\Service\CreateTrail;
27
use DavisPeixoto\BlogCore\Service\DeleteAuthor;
28
use DavisPeixoto\BlogCore\Service\DeletePost;
29
use DavisPeixoto\BlogCore\Service\DeleteTag;
30
use DavisPeixoto\BlogCore\Service\DeleteTrail;
31
use DavisPeixoto\BlogCore\Service\EditAuthor;
32
use DavisPeixoto\BlogCore\Service\EditPost;
33
use DavisPeixoto\BlogCore\Service\EditTag;
34
use DavisPeixoto\BlogCore\Service\EditTrail;
35
use DavisPeixoto\BlogCore\Service\GetAuthor;
36
use DavisPeixoto\BlogCore\Service\GetPost;
37
use DavisPeixoto\BlogCore\Service\GetTag;
38
use DavisPeixoto\BlogCore\Service\GetTrail;
39
use DavisPeixoto\BlogCore\Service\ListAuthors;
40
use DavisPeixoto\BlogCore\Service\ListPosts;
41
use DavisPeixoto\BlogCore\Service\ListTags;
42
use DavisPeixoto\BlogCore\Service\ListTrails;
43
use Exception;
44
use PHPUnit\Framework\TestCase;
45
use Psr\Log\LoggerInterface;
46
use Ramsey\Uuid\Uuid;
47
48
class TestServices extends TestCase
49
{
50
    /**
51
     * @param RepositoryInterface $repository
52
     * @param AbstractEntity $entity
53
     * @param LoggerInterface $logger
54
     * @param ServiceInterface $serviceName
55
     * @param string $method
56
     * @param mixed $willReturn
57
     * @param string $expected
58
     * @param string $message
59
     * @dataProvider successServiceProvider
60
     */
61 View Code Duplication
    public function testIsSuccessService(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
        $serviceName,
63
        $method,
64
        $willReturn,
65
        $repository,
66
        $entity,
67
        $logger,
68
        $expected,
69
        $message
70
    ) {
71
        $repository->expects($this->once())->method($method)->will($this->returnValue($willReturn));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<DavisPeixoto\Blog...es\RepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        $service = new $serviceName($repository, $entity, $logger);
73
        $this->assertEquals($expected, $service->run(), $message);
74
    }
75
76
    /**
77
     * @param $serviceName
78
     * @param $method
79
     * @param $willReturn
80
     * @param $repository
81
     * @param $entity
82
     * @param $logger
83
     * @param $expected
84
     * @param $message
85
     * @dataProvider failureServiceProvider
86
     */
87 View Code Duplication
    public function testIsFailureService(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
        $serviceName,
89
        $method,
90
        $willReturn,
91
        $repository,
92
        $entity,
93
        $logger,
94
        $expected,
95
        $message
96
    ) {
97
        $repository->expects($this->once())->method($method)->will($this->returnValue($willReturn));
98
        $service = new $serviceName($repository, $entity, $logger);
99
        $this->assertEquals($expected, $service->run(), $message);
100
    }
101
102
    /**
103
     * @param $serviceName
104
     * @param $method
105
     * @param $repository
106
     * @param $entity
107
     * @param $logger
108
     * @param $expected
109
     * @param $message
110
     * @dataProvider exceptionServiceProvider
111
     */
112
    public function testIsExceptionService($serviceName, $method, $repository, $entity, $logger, $expected, $message)
113
    {
114
        $logger->expects($this->once())->method('error');
115
        $repository->expects($this->once())->method($method)->will($this->throwException(new Exception()));
116
        $service = new $serviceName($repository, $entity, $logger);
117
        $this->assertEquals($expected, $service->run(), $message);
118
    }
119
120
    public function successServiceProvider()
121
    {
122
        $authorUuid = Uuid::uuid4()->toString();
123
        $postUuid = Uuid::uuid4()->toString();
124
        $tagUuid = Uuid::uuid4()->toString();
125
        $trailUuid = Uuid::uuid4()->toString();
126
127
        $author = new Author('Davis', '[email protected]', 'Some string', $authorUuid, new DateTime());
128
        $post = new Post('A Post', 'Lorem ipsum', $author, $postUuid, [], null);
129
        $tag = new Tag('A tag', $tagUuid);
130
        $trail = new Trail('A trail', 'An amazing trail', $trailUuid, [$post]);
131
        $filters = [];
132
133
        return [
134
            [
135
                CreateAuthor::class,
136
                'save',
137
                $authorUuid,
138
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
139
                $author,
140
                $this->createMock(LoggerInterface::class),
141
                $authorUuid,
142
                'Create author service',
143
            ],
144
            [
145
                EditAuthor::class,
146
                'save',
147
                $authorUuid,
148
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
149
                $author,
150
                $this->createMock(LoggerInterface::class),
151
                $authorUuid,
152
                'Edit author service',
153
            ],
154
            [
155
                DeleteAuthor::class,
156
                'delete',
157
                true,
158
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
159
                $author,
160
                $this->createMock(LoggerInterface::class),
161
                true,
162
                'Delete author service',
163
            ],
164
            [
165
                GetAuthor::class,
166
                'get',
167
                $author,
168
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
169
                $authorUuid,
170
                $this->createMock(LoggerInterface::class),
171
                $author,
172
                'Get author service',
173
            ],
174
            [
175
                ListAuthors::class,
176
                'getList',
177
                [$author],
178
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
179
                $filters,
180
                $this->createMock(LoggerInterface::class),
181
                [$author],
182
                'List authors service',
183
            ],
184
185
            [
186
                CreatePost::class,
187
                'save',
188
                $postUuid,
189
                $this->getMockForAbstractClass(AbstractPostRepository::class),
190
                $post,
191
                $this->createMock(LoggerInterface::class),
192
                $postUuid,
193
                'Create post service',
194
            ],
195
            [
196
                EditPost::class,
197
                'save',
198
                $postUuid,
199
                $this->getMockForAbstractClass(AbstractPostRepository::class),
200
                $post,
201
                $this->createMock(LoggerInterface::class),
202
                $postUuid,
203
                'Edit post service',
204
            ],
205
            [
206
                DeletePost::class,
207
                'delete',
208
                true,
209
                $this->getMockForAbstractClass(AbstractPostRepository::class),
210
                $post,
211
                $this->createMock(LoggerInterface::class),
212
                true,
213
                'Delete post service',
214
            ],
215
            [
216
                GetPost::class,
217
                'get',
218
                $post,
219
                $this->getMockForAbstractClass(AbstractPostRepository::class),
220
                $postUuid,
221
                $this->createMock(LoggerInterface::class),
222
                $post,
223
                'Get post service',
224
            ],
225
            [
226
                ListPosts::class,
227
                'getList',
228
                [$post],
229
                $this->getMockForAbstractClass(AbstractPostRepository::class),
230
                $filters,
231
                $this->createMock(LoggerInterface::class),
232
                [$post],
233
                'List posts service',
234
            ],
235
236
            [
237
                CreateTag::class,
238
                'save',
239
                $tagUuid,
240
                $this->getMockForAbstractClass(AbstractTagRepository::class),
241
                $tag,
242
                $this->createMock(LoggerInterface::class),
243
                $tagUuid,
244
                'Create tag service',
245
            ],
246
            [
247
                EditTag::class,
248
                'save',
249
                $tagUuid,
250
                $this->getMockForAbstractClass(AbstractTagRepository::class),
251
                $tag,
252
                $this->createMock(LoggerInterface::class),
253
                $tagUuid,
254
                'Edit tag service',
255
            ],
256
            [
257
                DeleteTag::class,
258
                'delete',
259
                true,
260
                $this->getMockForAbstractClass(AbstractTagRepository::class),
261
                $tag,
262
                $this->createMock(LoggerInterface::class),
263
                true,
264
                'Delete tag service',
265
            ],
266
            [
267
                GetTag::class,
268
                'get',
269
                $tag,
270
                $this->getMockForAbstractClass(AbstractTagRepository::class),
271
                $tagUuid,
272
                $this->createMock(LoggerInterface::class),
273
                $tag,
274
                'Get tag service',
275
            ],
276
            [
277
                ListTags::class,
278
                'getList',
279
                [$tag],
280
                $this->getMockForAbstractClass(AbstractTagRepository::class),
281
                $filters,
282
                $this->createMock(LoggerInterface::class),
283
                [$tag],
284
                'List tags service',
285
            ],
286
287
            [
288
                CreateTrail::class,
289
                'save',
290
                $trailUuid,
291
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
292
                $trail,
293
                $this->createMock(LoggerInterface::class),
294
                $trailUuid,
295
                'Create trail service',
296
            ],
297
            [
298
                EditTrail::class,
299
                'save',
300
                $trailUuid,
301
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
302
                $trail,
303
                $this->createMock(LoggerInterface::class),
304
                $trailUuid,
305
                'Edit trail service',
306
            ],
307
            [
308
                DeleteTrail::class,
309
                'delete',
310
                true,
311
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
312
                $trail,
313
                $this->createMock(LoggerInterface::class),
314
                true,
315
                'Delete trail service',
316
            ],
317
            [
318
                GetTrail::class,
319
                'get',
320
                $trail,
321
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
322
                $trailUuid,
323
                $this->createMock(LoggerInterface::class),
324
                $trail,
325
                'Get trail service',
326
            ],
327
            [
328
                ListTrails::class,
329
                'getList',
330
                [$trail],
331
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
332
                $filters,
333
                $this->createMock(LoggerInterface::class),
334
                [$trail],
335
                'List trails service',
336
            ],
337
        ];
338
    }
339
340
    public function failureServiceProvider()
341
    {
342
        $authorUuid = Uuid::uuid4()->toString();
343
        $postUuid = Uuid::uuid4()->toString();
344
        $tagUuid = Uuid::uuid4()->toString();
345
        $trailUuid = Uuid::uuid4()->toString();
346
347
        $author = new Author('Davis', '[email protected]', 'Some string', $authorUuid, new DateTime());
348
        $post = new Post('A Post', 'Lorem ipsum', $author, $postUuid, [], null);
349
        $tag = new Tag('A tag', $tagUuid);
350
        $trail = new Trail('A trail', 'An amazing trail', $trailUuid, [$post]);
351
        $filters = [];
352
353
        return [
354
            [
355
                DeleteAuthor::class,
356
                'delete',
357
                false,
358
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
359
                $author,
360
                $this->createMock(LoggerInterface::class),
361
                false,
362
                'Delete author service',
363
            ],
364
            [
365
                GetAuthor::class,
366
                'get',
367
                null,
368
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
369
                $authorUuid,
370
                $this->createMock(LoggerInterface::class),
371
                null,
372
                'Get author service',
373
            ],
374
            [
375
                ListAuthors::class,
376
                'getList',
377
                [],
378
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
379
                $filters,
380
                $this->createMock(LoggerInterface::class),
381
                [],
382
                'List authors service',
383
            ],
384
385
            [
386
                DeletePost::class,
387
                'delete',
388
                false,
389
                $this->getMockForAbstractClass(AbstractPostRepository::class),
390
                $post,
391
                $this->createMock(LoggerInterface::class),
392
                false,
393
                'Delete post service',
394
            ],
395
            [
396
                GetPost::class,
397
                'get',
398
                null,
399
                $this->getMockForAbstractClass(AbstractPostRepository::class),
400
                $postUuid,
401
                $this->createMock(LoggerInterface::class),
402
                null,
403
                'Get post service',
404
            ],
405
            [
406
                ListPosts::class,
407
                'getList',
408
                [],
409
                $this->getMockForAbstractClass(AbstractPostRepository::class),
410
                $filters,
411
                $this->createMock(LoggerInterface::class),
412
                [],
413
                'List posts service',
414
            ],
415
416
            [
417
                DeleteTag::class,
418
                'delete',
419
                false,
420
                $this->getMockForAbstractClass(AbstractTagRepository::class),
421
                $tag,
422
                $this->createMock(LoggerInterface::class),
423
                false,
424
                'Delete tag service',
425
            ],
426
            [
427
                GetTag::class,
428
                'get',
429
                null,
430
                $this->getMockForAbstractClass(AbstractTagRepository::class),
431
                $tagUuid,
432
                $this->createMock(LoggerInterface::class),
433
                null,
434
                'Get tag service',
435
            ],
436
            [
437
                ListTags::class,
438
                'getList',
439
                [],
440
                $this->getMockForAbstractClass(AbstractTagRepository::class),
441
                $filters,
442
                $this->createMock(LoggerInterface::class),
443
                [],
444
                'List tags service',
445
            ],
446
447
            [
448
                DeleteTrail::class,
449
                'delete',
450
                false,
451
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
452
                $trail,
453
                $this->createMock(LoggerInterface::class),
454
                false,
455
                'Delete trail service',
456
            ],
457
            [
458
                GetTrail::class,
459
                'get',
460
                null,
461
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
462
                $trailUuid,
463
                $this->createMock(LoggerInterface::class),
464
                null,
465
                'Get trail service',
466
            ],
467
            [
468
                ListTrails::class,
469
                'getList',
470
                [],
471
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
472
                $filters,
473
                $this->createMock(LoggerInterface::class),
474
                [],
475
                'List trails service',
476
            ],
477
        ];
478
    }
479
480
    public function exceptionServiceProvider()
481
    {
482
        $authorUuid = Uuid::uuid4()->toString();
483
        $postUuid = Uuid::uuid4()->toString();
484
        $tagUuid = Uuid::uuid4()->toString();
485
        $trailUuid = Uuid::uuid4()->toString();
486
487
        $author = new Author('Davis', '[email protected]', 'Some string', $authorUuid, new DateTime());
488
        $post = new Post('A Post', 'Lorem ipsum', $author, $postUuid, [], null);
489
        $tag = new Tag('A tag', $tagUuid);
490
        $trail = new Trail('A trail', 'An amazing trail', $trailUuid, [$post]);
491
        $filters = [];
492
493
        return [
494
            [
495
                CreateAuthor::class,
496
                'save',
497
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
498
                $author,
499
                $this->createMock(LoggerInterface::class),
500
                null,
501
                'Create author service',
502
            ],
503
            [
504
                EditAuthor::class,
505
                'save',
506
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
507
                $author,
508
                $this->createMock(LoggerInterface::class),
509
                null,
510
                'Edit author service',
511
            ],
512
            [
513
                DeleteAuthor::class,
514
                'delete',
515
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
516
                $author,
517
                $this->createMock(LoggerInterface::class),
518
                false,
519
                'Delete author service',
520
            ],
521
            [
522
                GetAuthor::class,
523
                'get',
524
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
525
                $authorUuid,
526
                $this->createMock(LoggerInterface::class),
527
                null,
528
                'Get author service',
529
            ],
530
            [
531
                ListAuthors::class,
532
                'getList',
533
                $this->getMockForAbstractClass(AbstractAuthorRepository::class),
534
                $filters,
535
                $this->createMock(LoggerInterface::class),
536
                [],
537
                'List authors service',
538
            ],
539
540
            [
541
                CreatePost::class,
542
                'save',
543
                $this->getMockForAbstractClass(AbstractPostRepository::class),
544
                $post,
545
                $this->createMock(LoggerInterface::class),
546
                null,
547
                'Create post service',
548
            ],
549
            [
550
                EditPost::class,
551
                'save',
552
                $this->getMockForAbstractClass(AbstractPostRepository::class),
553
                $post,
554
                $this->createMock(LoggerInterface::class),
555
                null,
556
                'Edit post service',
557
            ],
558
            [
559
                DeletePost::class,
560
                'delete',
561
                $this->getMockForAbstractClass(AbstractPostRepository::class),
562
                $post,
563
                $this->createMock(LoggerInterface::class),
564
                false,
565
                'Delete post service',
566
            ],
567
            [
568
                GetPost::class,
569
                'get',
570
                $this->getMockForAbstractClass(AbstractPostRepository::class),
571
                $postUuid,
572
                $this->createMock(LoggerInterface::class),
573
                null,
574
                'Get post service',
575
            ],
576
            [
577
                ListPosts::class,
578
                'getList',
579
                $this->getMockForAbstractClass(AbstractPostRepository::class),
580
                $filters,
581
                $this->createMock(LoggerInterface::class),
582
                [],
583
                'List posts service',
584
            ],
585
586
            [
587
                CreateTag::class,
588
                'save',
589
                $this->getMockForAbstractClass(AbstractTagRepository::class),
590
                $tag,
591
                $this->createMock(LoggerInterface::class),
592
                null,
593
                'Create tag service',
594
            ],
595
            [
596
                EditTag::class,
597
                'save',
598
                $this->getMockForAbstractClass(AbstractTagRepository::class),
599
                $tag,
600
                $this->createMock(LoggerInterface::class),
601
                null,
602
                'Edit tag service',
603
            ],
604
            [
605
                DeleteTag::class,
606
                'delete',
607
                $this->getMockForAbstractClass(AbstractTagRepository::class),
608
                $tag,
609
                $this->createMock(LoggerInterface::class),
610
                false,
611
                'Delete tag service',
612
            ],
613
            [
614
                GetTag::class,
615
                'get',
616
                $this->getMockForAbstractClass(AbstractTagRepository::class),
617
                $tagUuid,
618
                $this->createMock(LoggerInterface::class),
619
                null,
620
                'Get tag service',
621
            ],
622
            [
623
                ListTags::class,
624
                'getList',
625
                $this->getMockForAbstractClass(AbstractTagRepository::class),
626
                $filters,
627
                $this->createMock(LoggerInterface::class),
628
                [],
629
                'List tags service',
630
            ],
631
632
            [
633
                CreateTrail::class,
634
                'save',
635
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
636
                $trail,
637
                $this->createMock(LoggerInterface::class),
638
                null,
639
                'Create trail service',
640
            ],
641
            [
642
                EditTrail::class,
643
                'save',
644
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
645
                $trail,
646
                $this->createMock(LoggerInterface::class),
647
                null,
648
                'Edit trail service',
649
            ],
650
            [
651
                DeleteTrail::class,
652
                'delete',
653
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
654
                $trail,
655
                $this->createMock(LoggerInterface::class),
656
                false,
657
                'Delete trail service',
658
            ],
659
            [
660
                GetTrail::class,
661
                'get',
662
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
663
                $trailUuid,
664
                $this->createMock(LoggerInterface::class),
665
                null,
666
                'Get trail service',
667
            ],
668
            [
669
                ListTrails::class,
670
                'getList',
671
                $this->getMockForAbstractClass(AbstractTrailRepository::class),
672
                $filters,
673
                $this->createMock(LoggerInterface::class),
674
                [],
675
                'List trails service',
676
            ],
677
        ];
678
    }
679
}
680