|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Service\Execute; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Files\Domain\Entities\File as Entity; |
|
8
|
|
|
use AbterPhp\Files\Domain\Entities\FileCategory; |
|
|
|
|
|
|
9
|
|
|
use AbterPhp\Files\Orm\FileCategoryRepo; |
|
10
|
|
|
use AbterPhp\Files\Orm\FileRepo as GridRepo; |
|
11
|
|
|
use AbterPhp\Files\Validation\Factory\File as ValidatorFactory; |
|
12
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
13
|
|
|
use AbterPhp\Framework\Filesystem\Uploader; |
|
14
|
|
|
use Cocur\Slugify\Slugify; |
|
15
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
16
|
|
|
use Opulence\Http\Requests\UploadedFile; |
|
17
|
|
|
use Opulence\Orm\IUnitOfWork; |
|
18
|
|
|
use Opulence\Validation\IValidator; |
|
19
|
|
|
use Opulence\Validation\Rules\Errors\ErrorCollection; |
|
20
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
class FileTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var File - System Under Test */ |
|
26
|
|
|
protected $sut; |
|
27
|
|
|
|
|
28
|
|
|
/** @var GridRepo|MockObject */ |
|
29
|
|
|
protected $gridRepoMock; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ValidatorFactory|MockObject */ |
|
32
|
|
|
protected $validatorFactoryMock; |
|
33
|
|
|
|
|
34
|
|
|
/** @var IUnitOfWork|MockObject */ |
|
35
|
|
|
protected $unitOfWorkMock; |
|
36
|
|
|
|
|
37
|
|
|
/** @var IEventDispatcher|MockObject */ |
|
38
|
|
|
protected $eventDispatcherMock; |
|
39
|
|
|
|
|
40
|
|
|
/** @var Slugify|MockObject */ |
|
41
|
|
|
protected $slugifyMock; |
|
42
|
|
|
|
|
43
|
|
|
/** @var FileCategoryRepo|MockObject */ |
|
44
|
|
|
protected $fileCategoryRepo; |
|
45
|
|
|
|
|
46
|
|
|
/** @var Uploader|MockObject */ |
|
47
|
|
|
protected $uploaderMock; |
|
48
|
|
|
|
|
49
|
|
|
public function setUp(): void |
|
50
|
|
|
{ |
|
51
|
|
|
parent::setUp(); |
|
52
|
|
|
|
|
53
|
|
|
$this->gridRepoMock = $this->createMock(GridRepo::class); |
|
54
|
|
|
$this->validatorFactoryMock = $this->createMock(ValidatorFactory::class); |
|
55
|
|
|
$this->unitOfWorkMock = $this->createMock(IUnitOfWork::class); |
|
56
|
|
|
$this->eventDispatcherMock = $this->createMock(IEventDispatcher::class); |
|
57
|
|
|
$this->slugifyMock = $this->createMock(Slugify::class); |
|
58
|
|
|
$this->fileCategoryRepo = $this->createMock(FileCategoryRepo::class); |
|
59
|
|
|
$this->uploaderMock = $this->createMock(Uploader::class); |
|
60
|
|
|
|
|
61
|
|
|
$this->sut = new File( |
|
62
|
|
|
$this->gridRepoMock, |
|
63
|
|
|
$this->validatorFactoryMock, |
|
64
|
|
|
$this->unitOfWorkMock, |
|
65
|
|
|
$this->eventDispatcherMock, |
|
66
|
|
|
$this->slugifyMock, |
|
67
|
|
|
$this->fileCategoryRepo, |
|
68
|
|
|
$this->uploaderMock |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testCreateEntity() |
|
73
|
|
|
{ |
|
74
|
|
|
$id = 'foo'; |
|
75
|
|
|
|
|
76
|
|
|
$actualResult = $this->sut->createEntity($id); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
|
79
|
|
|
$this->assertSame($id, $actualResult->getId()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testCreate() |
|
83
|
|
|
{ |
|
84
|
|
|
$description = 'foo'; |
|
85
|
|
|
$categoryId = null; |
|
86
|
|
|
$postData = [ |
|
87
|
|
|
'description' => $description, |
|
88
|
|
|
'category_id' => $categoryId, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
$this->gridRepoMock->expects($this->once())->method('add'); |
|
92
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
|
93
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
|
94
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
|
95
|
|
|
$this->uploaderMock->expects($this->any())->method('getErrors')->willReturn([]); |
|
96
|
|
|
|
|
97
|
|
|
/** @var IStringerEntity|Entity $actualResult */ |
|
98
|
|
|
$actualResult = $this->sut->create($postData, []); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
|
101
|
|
|
$this->assertEmpty($actualResult->getId()); |
|
102
|
|
|
$this->assertSame($description, $actualResult->getDescription()); |
|
103
|
|
|
$this->assertNull($actualResult->getCategory()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testCreateWithCategory() |
|
107
|
|
|
{ |
|
108
|
|
|
$description = 'foo'; |
|
109
|
|
|
$categoryId = 'a9338468-1094-4070-af03-bcdec333fea9'; |
|
110
|
|
|
$postData = [ |
|
111
|
|
|
'description' => $description, |
|
112
|
|
|
'category_id' => $categoryId, |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
$fileCategory = new FileCategory($categoryId, '', '', false, []); |
|
116
|
|
|
|
|
117
|
|
|
$this->gridRepoMock->expects($this->once())->method('add'); |
|
118
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
|
119
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
|
120
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
|
121
|
|
|
$this->uploaderMock->expects($this->any())->method('getErrors')->willReturn([]); |
|
122
|
|
|
$this->fileCategoryRepo->expects($this->any())->method('getById')->willReturn($fileCategory); |
|
123
|
|
|
|
|
124
|
|
|
/** @var IStringerEntity|Entity $actualResult */ |
|
125
|
|
|
$actualResult = $this->sut->create($postData, []); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
|
128
|
|
|
$this->assertEmpty($actualResult->getId()); |
|
129
|
|
|
$this->assertSame($description, $actualResult->getDescription()); |
|
130
|
|
|
$this->assertSame($categoryId, $actualResult->getCategory()->getId()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testCreateDoesNotCommitIfUploadHadError() |
|
134
|
|
|
{ |
|
135
|
|
|
$description = 'foo'; |
|
136
|
|
|
$categoryId = 'a9338468-1094-4070-af03-bcdec333fea9'; |
|
137
|
|
|
$postData = [ |
|
138
|
|
|
'description' => $description, |
|
139
|
|
|
'category_id' => $categoryId, |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$fileCategory = new FileCategory($categoryId, '', '', false, []); |
|
143
|
|
|
|
|
144
|
|
|
$this->gridRepoMock->expects($this->never())->method('add'); |
|
145
|
|
|
$this->eventDispatcherMock->expects($this->never())->method('dispatch'); |
|
146
|
|
|
$this->unitOfWorkMock->expects($this->never())->method('commit'); |
|
147
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
|
148
|
|
|
$this->uploaderMock->expects($this->any())->method('getErrors')->willReturn(['foo' => ['bar']]); |
|
149
|
|
|
$this->fileCategoryRepo->expects($this->any())->method('getById')->willReturn($fileCategory); |
|
150
|
|
|
|
|
151
|
|
|
/** @var IStringerEntity|Entity $actualResult */ |
|
152
|
|
|
$actualResult = $this->sut->create($postData, []); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertInstanceOf(Entity::class, $actualResult); |
|
155
|
|
|
$this->assertEmpty($actualResult->getId()); |
|
156
|
|
|
$this->assertSame($description, $actualResult->getDescription()); |
|
157
|
|
|
$this->assertSame($categoryId, $actualResult->getCategory()->getId()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testUpdate() |
|
161
|
|
|
{ |
|
162
|
|
|
$id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb'; |
|
163
|
|
|
|
|
164
|
|
|
$entity = $this->sut->createEntity($id); |
|
165
|
|
|
|
|
166
|
|
|
$identifier = 'bar'; |
|
167
|
|
|
$description = 'foo'; |
|
168
|
|
|
$categoryId = null; |
|
169
|
|
|
$postData = [ |
|
170
|
|
|
'identifier' => $identifier, |
|
171
|
|
|
'description' => $description, |
|
172
|
|
|
'category_id' => $categoryId, |
|
173
|
|
|
]; |
|
174
|
|
|
|
|
175
|
|
|
$this->gridRepoMock->expects($this->never())->method('add'); |
|
176
|
|
|
$this->gridRepoMock->expects($this->never())->method('delete'); |
|
177
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
|
178
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
|
179
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
|
180
|
|
|
|
|
181
|
|
|
$actualResult = $this->sut->update($entity, $postData, []); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertTrue($actualResult); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testUpdateThrowsExceptionWhenCalledWithWrongEntity() |
|
187
|
|
|
{ |
|
188
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
189
|
|
|
|
|
190
|
|
|
/** @var IStringerEntity|MockObject $entityStub */ |
|
191
|
|
|
$entityStub = $this->createMock(IStringerEntity::class); |
|
192
|
|
|
|
|
193
|
|
|
$this->sut->update($entityStub, [], []); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function testUpdateHandlesFileUpdate() |
|
197
|
|
|
{ |
|
198
|
|
|
$id = '5c003d37-c59e-43eb-a471-e7b3c031fbeb'; |
|
199
|
|
|
|
|
200
|
|
|
$entity = $this->sut->createEntity($id); |
|
201
|
|
|
|
|
202
|
|
|
$identifier = 'bar'; |
|
203
|
|
|
$description = 'foo'; |
|
204
|
|
|
$tmpFilename = 'baz'; |
|
205
|
|
|
$tmpFsName = 'qux'; |
|
206
|
|
|
$filename = 'quux'; |
|
207
|
|
|
$categoryId = null; |
|
208
|
|
|
|
|
209
|
|
|
$fileUploadMock = $this->createMock(UploadedFile::class); |
|
210
|
|
|
$fileUploadMock->expects($this->any())->method('getTempFilename')->willReturn($tmpFilename); |
|
211
|
|
|
$fileUploadMock->expects($this->any())->method('getFilename')->willReturn($filename); |
|
212
|
|
|
|
|
213
|
|
|
$postData = [ |
|
214
|
|
|
'identifier' => $identifier, |
|
215
|
|
|
'description' => $description, |
|
216
|
|
|
'category_id' => $categoryId, |
|
217
|
|
|
]; |
|
218
|
|
|
$fileData = [ |
|
219
|
|
|
'file' => $fileUploadMock, |
|
220
|
|
|
]; |
|
221
|
|
|
$paths = [ |
|
222
|
|
|
'file' => $tmpFsName, |
|
223
|
|
|
]; |
|
224
|
|
|
|
|
225
|
|
|
$this->gridRepoMock->expects($this->any())->method('add'); |
|
226
|
|
|
$this->gridRepoMock->expects($this->any())->method('delete'); |
|
227
|
|
|
$this->eventDispatcherMock->expects($this->any())->method('dispatch'); |
|
228
|
|
|
$this->unitOfWorkMock->expects($this->any())->method('commit'); |
|
229
|
|
|
$this->slugifyMock->expects($this->any())->method('slugify')->willReturnArgument(0); |
|
230
|
|
|
$this->uploaderMock->expects($this->atLeastOnce())->method('delete'); |
|
231
|
|
|
$this->uploaderMock->expects($this->atLeastOnce())->method('persist')->wilLReturn($paths); |
|
232
|
|
|
|
|
233
|
|
|
$actualResult = $this->sut->update($entity, $postData, $fileData); |
|
234
|
|
|
|
|
235
|
|
|
$this->assertTrue($actualResult); |
|
236
|
|
|
$this->assertSame($tmpFsName, $entity->getFilesystemName()); |
|
237
|
|
|
$this->assertSame($tmpFilename, $entity->getPublicName()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function testDelete() |
|
241
|
|
|
{ |
|
242
|
|
|
$id = 'foo'; |
|
243
|
|
|
$entity = $this->sut->createEntity($id); |
|
244
|
|
|
|
|
245
|
|
|
$this->gridRepoMock->expects($this->once())->method('delete'); |
|
246
|
|
|
$this->eventDispatcherMock->expects($this->atLeastOnce())->method('dispatch'); |
|
247
|
|
|
$this->unitOfWorkMock->expects($this->once())->method('commit'); |
|
248
|
|
|
|
|
249
|
|
|
$actualResult = $this->sut->delete($entity); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertTrue($actualResult); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function testRetrieveEntity() |
|
255
|
|
|
{ |
|
256
|
|
|
$id = 'foo'; |
|
257
|
|
|
$entity = $this->sut->createEntity($id); |
|
258
|
|
|
|
|
259
|
|
|
$this->gridRepoMock->expects($this->once())->method('getById')->willReturn($entity); |
|
260
|
|
|
|
|
261
|
|
|
$actualResult = $this->sut->retrieveEntity($id); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertSame($entity, $actualResult); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
public function testRetrieveList() |
|
267
|
|
|
{ |
|
268
|
|
|
$offset = 0; |
|
269
|
|
|
$limit = 2; |
|
270
|
|
|
$orders = []; |
|
271
|
|
|
$conditions = []; |
|
272
|
|
|
$params = []; |
|
273
|
|
|
|
|
274
|
|
|
$id0 = 'foo'; |
|
275
|
|
|
$entity0 = $this->sut->createEntity($id0); |
|
276
|
|
|
$id1 = 'bar'; |
|
277
|
|
|
$entity1 = $this->sut->createEntity($id1); |
|
278
|
|
|
$expectedResult = [$entity0, $entity1]; |
|
279
|
|
|
|
|
280
|
|
|
$this->gridRepoMock->expects($this->once())->method('getPage')->willReturn($expectedResult); |
|
281
|
|
|
|
|
282
|
|
|
$actualResult = $this->sut->retrieveList($offset, $limit, $orders, $conditions, $params); |
|
283
|
|
|
|
|
284
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function testValidateFormSuccess() |
|
288
|
|
|
{ |
|
289
|
|
|
$postData = ['foo' => 'bar']; |
|
290
|
|
|
|
|
291
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
|
292
|
|
|
$validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(true); |
|
293
|
|
|
$validatorMock->expects($this->never())->method('getErrors'); |
|
294
|
|
|
|
|
295
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
|
296
|
|
|
|
|
297
|
|
|
$result = $this->sut->validateForm($postData); |
|
298
|
|
|
|
|
299
|
|
|
$this->assertSame([], $result); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function testValidateFormFailure() |
|
303
|
|
|
{ |
|
304
|
|
|
$postData = ['foo' => 'bar']; |
|
305
|
|
|
|
|
306
|
|
|
$errorsStub = new ErrorCollection(); |
|
307
|
|
|
$errorsStub['foo'] = ['foo error']; |
|
308
|
|
|
|
|
309
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
|
310
|
|
|
$validatorMock->expects($this->once())->method('isValid')->with($postData)->willReturn(false); |
|
311
|
|
|
$validatorMock->expects($this->once())->method('getErrors')->willReturn($errorsStub); |
|
312
|
|
|
|
|
313
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
|
314
|
|
|
|
|
315
|
|
|
$result = $this->sut->validateForm($postData); |
|
316
|
|
|
|
|
317
|
|
|
$this->assertSame(['foo' => ['foo error']], $result); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
public function testValidateCreatesOnlyOneValidator() |
|
321
|
|
|
{ |
|
322
|
|
|
$postData = ['foo' => 'bar']; |
|
323
|
|
|
|
|
324
|
|
|
$validatorMock = $this->createMock(IValidator::class); |
|
325
|
|
|
$validatorMock->expects($this->any())->method('isValid')->with($postData)->willReturn(true); |
|
326
|
|
|
$validatorMock->expects($this->any())->method('getErrors'); |
|
327
|
|
|
|
|
328
|
|
|
$this->validatorFactoryMock->expects($this->once())->method('createValidator')->willReturn($validatorMock); |
|
329
|
|
|
|
|
330
|
|
|
$firstRun = $this->sut->validateForm($postData); |
|
331
|
|
|
$secondRun = $this->sut->validateForm($postData); |
|
332
|
|
|
|
|
333
|
|
|
$this->assertSame($firstRun, $secondRun); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: