1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Service\Execute; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
8
|
|
|
use AbterPhp\Files\Domain\Entities\FileDownload as Entity; |
9
|
|
|
use AbterPhp\Files\Orm\FileDownloadRepo as GridRepo; |
10
|
|
|
use AbterPhp\Files\Validation\Factory\FileDownload as ValidatorFactory; |
11
|
|
|
use Cocur\Slugify\Slugify; |
12
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
13
|
|
|
use Opulence\Orm\IUnitOfWork; |
14
|
|
|
use Opulence\Validation\IValidator; |
15
|
|
|
use Opulence\Validation\Rules\Errors\ErrorCollection; |
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class FileDownloadTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** @var FileDownload - System Under Test */ |
22
|
|
|
protected $sut; |
23
|
|
|
|
24
|
|
|
/** @var GridRepo|MockObject */ |
25
|
|
|
protected $gridRepoMock; |
26
|
|
|
|
27
|
|
|
/** @var ValidatorFactory|MockObject */ |
28
|
|
|
protected $validatorFactoryMock; |
29
|
|
|
|
30
|
|
|
/** @var IUnitOfWork|MockObject */ |
31
|
|
|
protected $unitOfWorkMock; |
32
|
|
|
|
33
|
|
|
/** @var IEventDispatcher|MockObject */ |
34
|
|
|
protected $eventDispatcherMock; |
35
|
|
|
|
36
|
|
|
/** @var Slugify|MockObject */ |
37
|
|
|
protected $slugifyMock; |
38
|
|
|
|
39
|
|
|
public function setUp(): void |
40
|
|
|
{ |
41
|
|
|
parent::setUp(); |
42
|
|
|
|
43
|
|
|
$this->gridRepoMock = $this->createMock(GridRepo::class); |
44
|
|
|
$this->validatorFactoryMock = $this->createMock(ValidatorFactory::class); |
45
|
|
|
$this->unitOfWorkMock = $this->createMock(IUnitOfWork::class); |
46
|
|
|
$this->eventDispatcherMock = $this->createMock(IEventDispatcher::class); |
47
|
|
|
$this->slugifyMock = $this->createMock(Slugify::class); |
48
|
|
|
|
49
|
|
|
$this->sut = new FileDownload( |
50
|
|
|
$this->gridRepoMock, |
51
|
|
|
$this->validatorFactoryMock, |
52
|
|
|
$this->unitOfWorkMock, |
53
|
|
|
$this->eventDispatcherMock, |
54
|
|
|
$this->slugifyMock |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCreateEntity() |
59
|
|
|
{ |
60
|
|
|
$id = 'foo'; |
61
|
|
|
|
62
|
|
|
$actualResult = $this->sut->createEntity($id); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
65
|
|
|
$this->assertSame($id, $actualResult->getId()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testCreate() |
69
|
|
|
{ |
70
|
|
|
$fileId = '4567f007-6efa-4dae-b0af-795a3dbfe44e'; |
71
|
|
|
$userId = '66822f5c-e32c-434c-a220-552a41138653'; |
72
|
|
|
$postData = [ |
73
|
|
|
'file_id' => $fileId, |
74
|
|
|
'user_id' => $userId, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$this->gridRepoMock->expects($this->once())->method('add'); |
78
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
79
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
80
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
81
|
|
|
|
82
|
|
|
/** @var IStringerEntity|Entity $actualResult */ |
83
|
|
|
$actualResult = $this->sut->create($postData, []); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
86
|
|
|
$this->assertEmpty($actualResult->getId()); |
87
|
|
|
$this->assertSame($fileId, $actualResult->getFile()->getId()); |
88
|
|
|
$this->assertSame($userId, $actualResult->getUser()->getId()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testUpdate() |
92
|
|
|
{ |
93
|
|
|
$id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb'; |
94
|
|
|
$fileId = '4567f007-6efa-4dae-b0af-795a3dbfe44e'; |
95
|
|
|
$userId = '66822f5c-e32c-434c-a220-552a41138653'; |
96
|
|
|
$postData = [ |
97
|
|
|
'id' => $id, |
98
|
|
|
'file_id' => $fileId, |
99
|
|
|
'user_id' => $userId, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$entity = $this->sut->createEntity($id); |
103
|
|
|
|
104
|
|
|
$this->gridRepoMock->expects($this->never())->method('add'); |
105
|
|
|
$this->gridRepoMock->expects($this->never())->method('delete'); |
106
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
107
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
108
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
109
|
|
|
|
110
|
|
|
$actualResult = $this->sut->update($entity, $postData, []); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($actualResult); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testUpdateThrowsExceptionWhenCalledWithWrongEntity() |
116
|
|
|
{ |
117
|
|
|
$this->expectException(\InvalidArgumentException::class); |
118
|
|
|
|
119
|
|
|
/** @var IStringerEntity|MockObject $entityStub */ |
120
|
|
|
$entityStub = $this->createMock(IStringerEntity::class); |
121
|
|
|
|
122
|
|
|
$this->sut->update($entityStub, [], []); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testDelete() |
126
|
|
|
{ |
127
|
|
|
$id = 'foo'; |
128
|
|
|
$entity = $this->sut->createEntity($id); |
129
|
|
|
|
130
|
|
|
$this->gridRepoMock->expects($this->once())->method('delete'); |
131
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
132
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
133
|
|
|
|
134
|
|
|
$actualResult = $this->sut->delete($entity); |
135
|
|
|
|
136
|
|
|
$this->assertTrue($actualResult); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testRetrieveEntity() |
140
|
|
|
{ |
141
|
|
|
$id = 'foo'; |
142
|
|
|
$entity = $this->sut->createEntity($id); |
143
|
|
|
|
144
|
|
|
$this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity); |
145
|
|
|
|
146
|
|
|
$actualResult = $this->sut->retrieveEntity($id); |
147
|
|
|
|
148
|
|
|
$this->assertSame($entity, $actualResult); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testRetrieveList() |
152
|
|
|
{ |
153
|
|
|
$offset = 0; |
154
|
|
|
$limit = 2; |
155
|
|
|
$orders = []; |
156
|
|
|
$conditions = []; |
157
|
|
|
$params = []; |
158
|
|
|
|
159
|
|
|
$id0 = 'foo'; |
160
|
|
|
$entity0 = $this->sut->createEntity($id0); |
161
|
|
|
$id1 = 'bar'; |
162
|
|
|
$entity1 = $this->sut->createEntity($id1); |
163
|
|
|
$expectedResult = [$entity0, $entity1]; |
164
|
|
|
|
165
|
|
|
$this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult); |
166
|
|
|
|
167
|
|
|
$actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params); |
168
|
|
|
|
169
|
|
|
$this->assertSame($expectedResult, $actualResult); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testValidateFormSuccess() |
173
|
|
|
{ |
174
|
|
|
$postData = ['foo' => 'bar']; |
175
|
|
|
|
176
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
177
|
|
|
$validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true); |
178
|
|
|
$validatorMock->expects($this->never())->method('getErrors'); |
179
|
|
|
|
180
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
181
|
|
|
|
182
|
|
|
$result = $this->sut->validateForm($postData); |
183
|
|
|
|
184
|
|
|
$this->assertSame([], $result); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testValidateFormFailure() |
188
|
|
|
{ |
189
|
|
|
$postData = ['foo' => 'bar']; |
190
|
|
|
|
191
|
|
|
$errorsStub = new ErrorCollection(); |
192
|
|
|
$errorsStub['foo'] = ['foo error']; |
193
|
|
|
|
194
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
195
|
|
|
$validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false); |
196
|
|
|
$validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub); |
197
|
|
|
|
198
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
199
|
|
|
|
200
|
|
|
$result = $this->sut->validateForm($postData); |
201
|
|
|
|
202
|
|
|
$this->assertSame(['foo' => ['foo error']], $result); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testValidateCreatesOnlyOneValidator() |
206
|
|
|
{ |
207
|
|
|
$postData = ['foo' => 'bar']; |
208
|
|
|
|
209
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
210
|
|
|
$validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true); |
211
|
|
|
$validatorMock->expects($this->any())->method('getErrors'); |
212
|
|
|
|
213
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
214
|
|
|
|
215
|
|
|
$firstRun = $this->sut->validateForm($postData); |
216
|
|
|
$secondRun = $this->sut->validateForm($postData); |
217
|
|
|
|
218
|
|
|
$this->assertSame($firstRun, $secondRun); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|