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 DavisPeixoto\BlogCore\Entity\Author; |
12
|
|
|
use DavisPeixoto\BlogCore\Entity\Post; |
13
|
|
|
use DavisPeixoto\BlogCore\Entity\Trail; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use DavisPeixoto\BlogCore\Service\CreateTrail; |
16
|
|
|
use DavisPeixoto\BlogCore\Service\EditTrail; |
17
|
|
|
use DavisPeixoto\BlogCore\Service\DeleteTrail; |
18
|
|
|
use DavisPeixoto\BlogCore\Service\GetTrail; |
19
|
|
|
use DavisPeixoto\BlogCore\Service\ListTrails; |
20
|
|
|
use DavisPeixoto\BlogCore\Repository\AbstractTrailRepository; |
21
|
|
|
use Ramsey\Uuid\Uuid; |
22
|
|
|
use Ramsey\Uuid\UuidInterface; |
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
use DateTime; |
25
|
|
|
use Exception; |
26
|
|
|
|
27
|
|
|
class TestTrailServices extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @RepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $trailRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @UuidInterface |
41
|
|
|
*/ |
42
|
|
|
private $uuid; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @UuidInterface |
46
|
|
|
*/ |
47
|
|
|
private $authorUuid; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @UuidInterface |
51
|
|
|
*/ |
52
|
|
|
private $postUuid; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @Trail |
56
|
|
|
*/ |
57
|
|
|
private $trail; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Author |
61
|
|
|
*/ |
62
|
|
|
private $author; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Post |
66
|
|
|
*/ |
67
|
|
|
private $post; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @array |
71
|
|
|
*/ |
72
|
|
|
private $filters; |
73
|
|
|
|
74
|
|
|
public function setUp() |
75
|
|
|
{ |
76
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
77
|
|
|
$this->trailRepository = $this->getMockForAbstractClass(AbstractTrailRepository::class); |
78
|
|
|
$this->authorUuid = Uuid::uuid4(); |
79
|
|
|
$this->author = new Author($this->authorUuid, 'Davis', '[email protected]', 'Some string', new DateTime()); |
80
|
|
|
$this->postUuid = Uuid::uuid4(); |
81
|
|
|
$this->uuid = Uuid::uuid4(); |
82
|
|
|
$this->post = new Post($this->postUuid, 'A Post', 'Lorem ipsum', $this->author, [], null); |
83
|
|
|
$this->trail = new Trail($this->uuid, 'A trail', 'An amazing trail', [$this->post]); |
84
|
|
|
$this->filters = []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCreateTrailService() |
88
|
|
|
{ |
89
|
|
|
$this->trailRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
90
|
|
|
$service = new CreateTrail($this->trailRepository, $this->trail, $this->logger); |
91
|
|
|
$this->assertEquals($this->uuid, $service->run(), 'called once!'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testCreateTrailServiceShouldReturnNullOnFailure() |
95
|
|
|
{ |
96
|
|
|
$this->logger->expects($this->once())->method('error'); |
97
|
|
|
$this->trailRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
98
|
|
|
$service = new CreateTrail($this->trailRepository, $this->trail, $this->logger); |
99
|
|
|
$this->assertEquals(null, $service->run(), 'Test exception'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testEditTrailService() |
103
|
|
|
{ |
104
|
|
|
$this->trailRepository->expects($this->once())->method('save')->will($this->returnValue($this->uuid)); |
105
|
|
|
$service = new EditTrail($this->trailRepository, $this->trail, $this->logger); |
106
|
|
|
$this->assertEquals($this->uuid, $service->run(), 'called once!'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testEditTrailServiceShouldReturnNullOnFailure() |
110
|
|
|
{ |
111
|
|
|
$this->logger->expects($this->once())->method('error'); |
112
|
|
|
$this->trailRepository->expects($this->once())->method('save')->will($this->throwException(new Exception())); |
113
|
|
|
$service = new EditTrail($this->trailRepository, $this->trail, $this->logger); |
114
|
|
|
$this->assertEquals(null, $service->run(), 'Test exception'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testDeleteTrailService() |
118
|
|
|
{ |
119
|
|
|
$this->trailRepository->expects($this->once())->method('delete')->will($this->returnValue(true)); |
120
|
|
|
$service = new DeleteTrail($this->trailRepository, $this->trail, $this->logger); |
121
|
|
|
$this->assertEquals(true, $service->run(), 'Success'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testDeleteTrailServiceReturningFalse() |
125
|
|
|
{ |
126
|
|
|
$this->trailRepository->expects($this->once())->method('delete')->will($this->returnValue(false)); |
127
|
|
|
$service = new DeleteTrail($this->trailRepository, $this->trail, $this->logger); |
128
|
|
|
$this->assertEquals(false, $service->run(), 'Success'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testDeleteTrailServiceShouldReturnFalseOnFailure() |
132
|
|
|
{ |
133
|
|
|
$this->logger->expects($this->once())->method('error'); |
134
|
|
|
$this->trailRepository->expects($this->once())->method('delete')->will($this->throwException(new Exception())); |
135
|
|
|
$service = new DeleteTrail($this->trailRepository, $this->trail, $this->logger); |
136
|
|
|
$this->assertEquals(false, $service->run(), 'Test exception'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testGetTrailService() |
140
|
|
|
{ |
141
|
|
|
$this->trailRepository->expects($this->once())->method('get')->will($this->returnValue($this->trail)); |
142
|
|
|
$service = new GetTrail($this->trailRepository, $this->uuid, $this->logger); |
143
|
|
|
$this->assertEquals($this->trail, $service->run(), 'Success'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testGetTrailServiceReturningException() |
147
|
|
|
{ |
148
|
|
|
$this->logger->expects($this->once())->method('error'); |
149
|
|
|
$this->trailRepository->expects($this->once())->method('get')->will($this->throwException(new Exception())); |
150
|
|
|
$service = new GetTrail($this->trailRepository, $this->uuid, $this->logger); |
151
|
|
|
$this->assertEquals(false, $service->run(), 'Exception'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetTrailServiceReturningFalse() |
155
|
|
|
{ |
156
|
|
|
$this->trailRepository->expects($this->once())->method('get')->will($this->returnValue(false)); |
157
|
|
|
$service = new GetTrail($this->trailRepository, $this->uuid, $this->logger); |
158
|
|
|
$this->assertEquals(false, $service->run(), 'False'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testGetTrailsListService() |
162
|
|
|
{ |
163
|
|
|
$this->trailRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->returnValue([$this->trail])); |
164
|
|
|
$service = new ListTrails($this->trailRepository, $this->filters, $this->logger); |
165
|
|
|
$this->assertEquals([$this->trail], $service->run(), 'Success'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testGetTrailsListServiceException() |
169
|
|
|
{ |
170
|
|
|
$this->logger->expects($this->once())->method('error'); |
171
|
|
|
$this->trailRepository->expects($this->once())->method('getList')->with($this->filters)->will($this->throwException(new Exception())); |
172
|
|
|
$service = new ListTrails($this->trailRepository, $this->filters, $this->logger); |
173
|
|
|
$this->assertEquals([], $service->run(), 'Exception'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|