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