Passed
Push — master ( 3ab971...8e05e6 )
by Davis
39s
created

TestServices::testIsSuccessService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 12
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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