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 PHPUnit\Framework\TestCase; |
12
|
|
|
use DavisPeixoto\BlogCore\Entity\Tag; |
13
|
|
|
use DavisPeixoto\BlogCore\Service\CreateTag; |
14
|
|
|
use DavisPeixoto\BlogCore\Service\EditTag; |
15
|
|
|
use DavisPeixoto\BlogCore\Service\DeleteTag; |
16
|
|
|
use DavisPeixoto\BlogCore\Service\GetTag; |
17
|
|
|
use DavisPeixoto\BlogCore\Service\ListTags; |
18
|
|
|
use DavisPeixoto\BlogCore\Repository\AbstractTagRepository; |
19
|
|
|
use Ramsey\Uuid\Uuid; |
20
|
|
|
use Ramsey\Uuid\UuidInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use DateTime; |
23
|
|
|
use Exception; |
24
|
|
|
|
25
|
|
View Code Duplication |
class TestTagServices extends TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @RepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $tagRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @UuidInterface |
39
|
|
|
*/ |
40
|
|
|
private $uuid; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @Tag |
44
|
|
|
*/ |
45
|
|
|
private $tag; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @array |
49
|
|
|
*/ |
50
|
|
|
private $filters; |
51
|
|
|
|
52
|
|
|
public function setUp() |
53
|
|
|
{ |
54
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
55
|
|
|
$this->tagRepository = $this->getMockForAbstractClass(AbstractTagRepository::class); |
56
|
|
|
$this->uuid = Uuid::uuid4(); |
57
|
|
|
$this->tag = new Tag($this->uuid, 'Tag 1'); |
58
|
|
|
$this->filters = []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testCreateTagService() |
62
|
|
|
{ |
63
|
|
|
$this->tagRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
64
|
|
|
$service = new CreateTag($this->tagRepository, $this->tag, $this->logger); |
65
|
|
|
$this->assertEquals($this->uuid, $service->run(), 'called once!'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testCreateTagServiceShouldReturnNullOnFailure() |
69
|
|
|
{ |
70
|
|
|
$this->logger->expects($this->once())->method('error'); |
71
|
|
|
$this->tagRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
72
|
|
|
$service = new CreateTag($this->tagRepository, $this->tag, $this->logger); |
73
|
|
|
$this->assertEquals(null, $service->run(), 'Test exception'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testEditTagService() |
77
|
|
|
{ |
78
|
|
|
$this->tagRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
79
|
|
|
$service = new EditTag($this->tagRepository, $this->tag, $this->logger); |
80
|
|
|
$this->assertEquals($this->uuid, $service->run(), 'called once!'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testEditTagServiceShouldReturnNullOnFailure() |
84
|
|
|
{ |
85
|
|
|
$this->logger->expects($this->once())->method('error'); |
86
|
|
|
$this->tagRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
87
|
|
|
$service = new EditTag($this->tagRepository, $this->tag, $this->logger); |
88
|
|
|
$this->assertEquals(null, $service->run(), 'Test exception'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testDeleteTagService() |
92
|
|
|
{ |
93
|
|
|
$this->tagRepository->expects($this->once())->method('delete')->will($this->returnValue(true)); |
94
|
|
|
$service = new DeleteTag($this->tagRepository, $this->tag, $this->logger); |
95
|
|
|
$this->assertEquals(true, $service->run(), 'Success'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testDeleteTagServiceReturningFalse() |
99
|
|
|
{ |
100
|
|
|
$this->tagRepository->expects($this->once())->method('delete')->will($this->returnValue(false)); |
101
|
|
|
$service = new DeleteTag($this->tagRepository, $this->tag, $this->logger); |
102
|
|
|
$this->assertEquals(false, $service->run(), 'Success'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testDeleteTagServiceShouldReturnFalseOnFailure() |
106
|
|
|
{ |
107
|
|
|
$this->logger->expects($this->once())->method('error'); |
108
|
|
|
$this->tagRepository->expects($this->once())->method('delete')->will($this->throwException(new Exception())); |
109
|
|
|
$service = new DeleteTag($this->tagRepository, $this->tag, $this->logger); |
110
|
|
|
$this->assertEquals(false, $service->run(), 'Test exception'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGetTagService() |
114
|
|
|
{ |
115
|
|
|
$this->tagRepository->expects($this->once())->method('get')->will($this->returnValue($this->tag)); |
116
|
|
|
$service = new GetTag($this->tagRepository, $this->uuid, $this->logger); |
117
|
|
|
$this->assertEquals($this->tag, $service->run(), 'Success'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetTagServiceReturningException() |
121
|
|
|
{ |
122
|
|
|
$this->logger->expects($this->once())->method('error'); |
123
|
|
|
$this->tagRepository->expects($this->once())->method('get')->will($this->throwException(new Exception())); |
124
|
|
|
$service = new GetTag($this->tagRepository, $this->uuid, $this->logger); |
125
|
|
|
$this->assertEquals(false, $service->run(), 'Exception'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetTagServiceReturningFalse() |
129
|
|
|
{ |
130
|
|
|
$this->tagRepository->expects($this->once())->method('get')->will($this->returnValue(false)); |
131
|
|
|
$service = new GetTag($this->tagRepository, $this->uuid, $this->logger); |
132
|
|
|
$this->assertEquals(false, $service->run(), 'False'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testGetTagsListService() |
136
|
|
|
{ |
137
|
|
|
$this->tagRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->returnValue([$this->tag])); |
138
|
|
|
$service = new ListTags($this->tagRepository, $this->filters, $this->logger); |
139
|
|
|
$this->assertEquals([$this->tag], $service->run(), 'Success'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetTagsListServiceException() |
143
|
|
|
{ |
144
|
|
|
$this->logger->expects($this->once())->method('error'); |
145
|
|
|
$this->tagRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->throwException(new Exception())); |
146
|
|
|
$service = new ListTags($this->tagRepository, $this->filters, $this->logger); |
147
|
|
|
$this->assertEquals([], $service->run(), 'Exception'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.