1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Filesystem; |
6
|
|
|
|
7
|
|
|
use League\Flysystem\Filesystem; |
8
|
|
|
use League\Flysystem\UnableToDeleteFile; |
9
|
|
|
use League\Flysystem\UnableToReadFile; |
10
|
|
|
use League\Flysystem\UnableToRetrieveMetadata; |
11
|
|
|
use Opulence\Http\Requests\UploadedFile; |
12
|
|
|
use Opulence\Http\Requests\UploadException; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class UploaderTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected const FILE_MANAGER_PATH = '/root/to/path'; |
19
|
|
|
|
20
|
|
|
/** @var Uploader - System Under Test */ |
21
|
|
|
protected Uploader $sut; |
22
|
|
|
|
23
|
|
|
/** @var Filesystem|MockObject */ |
24
|
|
|
protected $filesystemMock; |
25
|
|
|
|
26
|
|
|
public function setUp(): void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
$this->filesystemMock = $this->createMock(Filesystem::class); |
31
|
|
|
|
32
|
|
|
$this->sut = new Uploader($this->filesystemMock, self::FILE_MANAGER_PATH); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testPersistWithoutFileData(): void |
36
|
|
|
{ |
37
|
|
|
$actualResult = $this->sut->persist([]); |
38
|
|
|
|
39
|
|
|
$this->assertEquals([], $actualResult); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testPersist(): void |
43
|
|
|
{ |
44
|
|
|
$fileType = 'foo'; |
45
|
|
|
|
46
|
|
|
$uploadedFileMock = $this->createMock(UploadedFile::class); |
47
|
|
|
|
48
|
|
|
$fileData = []; |
49
|
|
|
$fileData[$fileType] = $uploadedFileMock; |
50
|
|
|
|
51
|
|
|
$actualResult = $this->sut->persist($fileData); |
52
|
|
|
|
53
|
|
|
$this->assertArrayHasKey($fileType, $actualResult); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testPersistPopulatesErrorsIfUploadedFileThrowsException(): void |
57
|
|
|
{ |
58
|
|
|
$fileType = 'foo'; |
59
|
|
|
$msg = 'bar'; |
60
|
|
|
|
61
|
|
|
$uploadedFileMock = $this->createMock(UploadedFile::class); |
62
|
|
|
|
63
|
|
|
$uploadedFileMock |
64
|
|
|
->expects($this->atLeastOnce()) |
65
|
|
|
->method('move') |
66
|
|
|
->willThrowException(new UploadException($msg)); |
67
|
|
|
|
68
|
|
|
$fileData = []; |
69
|
|
|
$fileData[$fileType] = $uploadedFileMock; |
70
|
|
|
|
71
|
|
|
$actualResult = $this->sut->persist($fileData); |
72
|
|
|
|
73
|
|
|
$this->assertEquals([], $actualResult); |
74
|
|
|
$this->assertEquals([$fileType => $msg], $this->sut->getErrors()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testDeleteReturnsFalseIfPathDoesNotExist(): void |
78
|
|
|
{ |
79
|
|
|
$path = 'example.foo'; |
80
|
|
|
|
81
|
|
|
$this->filesystemMock->expects($this->once())->method('fileExists')->willReturn(false); |
82
|
|
|
$this->filesystemMock->expects($this->never())->method('delete'); |
83
|
|
|
|
84
|
|
|
$actualResult = $this->sut->delete($path); |
85
|
|
|
|
86
|
|
|
$this->assertFalse($actualResult); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testDeleteReturnsFalseIfFileSystemThrowsException(): void |
90
|
|
|
{ |
91
|
|
|
$path = 'example.foo'; |
92
|
|
|
|
93
|
|
|
$exception = new UnableToDeleteFile($path); |
94
|
|
|
|
95
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
96
|
|
|
$this->filesystemMock->expects($this->once())->method('delete')->willThrowException($exception); |
97
|
|
|
|
98
|
|
|
$actualResult = $this->sut->delete($path); |
99
|
|
|
|
100
|
|
|
$this->assertFalse($actualResult); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testDelete(): void |
104
|
|
|
{ |
105
|
|
|
$path = 'example.foo'; |
106
|
|
|
|
107
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
108
|
|
|
$this->filesystemMock->expects($this->once())->method('delete'); |
109
|
|
|
|
110
|
|
|
$actualResult = $this->sut->delete($path); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($actualResult); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetContentReturnsNullIfPathDoesNotExist(): void |
116
|
|
|
{ |
117
|
|
|
$path = 'example.foo'; |
118
|
|
|
|
119
|
|
|
$this->filesystemMock->expects($this->once())->method('fileExists')->willReturn(false); |
120
|
|
|
$this->filesystemMock->expects($this->never())->method('read'); |
121
|
|
|
|
122
|
|
|
$actualResult = $this->sut->getContent($path); |
123
|
|
|
|
124
|
|
|
$this->assertNull($actualResult); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGetContentReturnsFalseIfFileSystemThrowsException(): void |
128
|
|
|
{ |
129
|
|
|
$path = 'example.foo'; |
130
|
|
|
|
131
|
|
|
$exception = new UnableToReadFile($path); |
132
|
|
|
|
133
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
134
|
|
|
$this->filesystemMock->expects($this->once())->method('read')->willThrowException($exception); |
135
|
|
|
|
136
|
|
|
$actualResult = $this->sut->getContent($path); |
137
|
|
|
|
138
|
|
|
$this->assertNull($actualResult); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testGetContent(): void |
142
|
|
|
{ |
143
|
|
|
$path = 'example.foo'; |
144
|
|
|
$content = 'This is the content'; |
145
|
|
|
|
146
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
147
|
|
|
$this->filesystemMock->expects($this->once())->method('read')->willReturn($content); |
148
|
|
|
|
149
|
|
|
$actualResult = $this->sut->getContent($path); |
150
|
|
|
|
151
|
|
|
$this->assertEquals($content, $actualResult); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetContentReturnsNullIfReadingFails(): void |
155
|
|
|
{ |
156
|
|
|
$path = 'example.foo'; |
157
|
|
|
$exception = UnableToReadFile::fromLocation($path); |
158
|
|
|
|
159
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
160
|
|
|
$this->filesystemMock->expects($this->once())->method('read')->willThrowException($exception); |
161
|
|
|
|
162
|
|
|
$actualResult = $this->sut->getContent($path); |
163
|
|
|
|
164
|
|
|
$this->assertNull($actualResult); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testGetStreamReturnsFalselIfPathDoesNotExist(): void |
168
|
|
|
{ |
169
|
|
|
$path = 'example.foo'; |
170
|
|
|
|
171
|
|
|
$this->filesystemMock->expects($this->once())->method('fileExists')->willReturn(false); |
172
|
|
|
$this->filesystemMock->expects($this->never())->method('readStream'); |
173
|
|
|
|
174
|
|
|
$actualResult = $this->sut->getStream($path); |
175
|
|
|
|
176
|
|
|
$this->assertFalse($actualResult); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testGetStreamReturnsFalseIfFileSystemThrowsException(): void |
180
|
|
|
{ |
181
|
|
|
$path = 'example.foo'; |
182
|
|
|
|
183
|
|
|
$exception = new UnableToReadFile($path); |
184
|
|
|
|
185
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
186
|
|
|
$this->filesystemMock->expects($this->once())->method('readStream')->willThrowException($exception); |
187
|
|
|
|
188
|
|
|
$actualResult = $this->sut->getStream($path); |
189
|
|
|
|
190
|
|
|
$this->assertFalse($actualResult); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testGetStream(): void |
194
|
|
|
{ |
195
|
|
|
$path = 'example.foo'; |
196
|
|
|
$content = "foo"; |
197
|
|
|
|
198
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
199
|
|
|
$this->filesystemMock->expects($this->once())->method('readStream')->willReturn($content); |
200
|
|
|
|
201
|
|
|
$actualResult = $this->sut->getStream($path); |
202
|
|
|
|
203
|
|
|
$this->assertEquals($content, $actualResult); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testGetSizeReturnsNullIfPathDoesNotExist(): void |
207
|
|
|
{ |
208
|
|
|
$path = 'example.foo'; |
209
|
|
|
|
210
|
|
|
$this->filesystemMock->expects($this->once())->method('fileExists')->willReturn(false); |
211
|
|
|
$this->filesystemMock->expects($this->never())->method('fileSize'); |
212
|
|
|
|
213
|
|
|
$actualResult = $this->sut->getSize($path); |
214
|
|
|
|
215
|
|
|
$this->assertNull($actualResult); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testGetSizeReturnsNullIfFileSystemThrowsException(): void |
219
|
|
|
{ |
220
|
|
|
$path = 'example.foo'; |
221
|
|
|
|
222
|
|
|
$exception = new UnableToRetrieveMetadata($path); |
223
|
|
|
|
224
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
225
|
|
|
$this->filesystemMock->expects($this->once())->method('fileSize')->willThrowException($exception); |
226
|
|
|
|
227
|
|
|
$actualResult = $this->sut->getSize($path); |
228
|
|
|
|
229
|
|
|
$this->assertNull($actualResult); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testGetSizeReturnsNullIfSizeIsNotAvailable(): void |
233
|
|
|
{ |
234
|
|
|
$path = 'example.foo'; |
235
|
|
|
$exception = UnableToRetrieveMetadata::create($path, 'size'); |
236
|
|
|
|
237
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
238
|
|
|
$this->filesystemMock->expects($this->once())->method('fileSize')->willThrowException($exception); |
239
|
|
|
|
240
|
|
|
$actualResult = $this->sut->getSize($path); |
241
|
|
|
|
242
|
|
|
$this->assertNull($actualResult); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testGetSize(): void |
246
|
|
|
{ |
247
|
|
|
$path = 'example.foo'; |
248
|
|
|
$size = 3; |
249
|
|
|
|
250
|
|
|
$this->filesystemMock->expects($this->any())->method('fileExists')->willReturn(true); |
251
|
|
|
$this->filesystemMock->expects($this->once())->method('fileSize')->willReturn($size); |
252
|
|
|
|
253
|
|
|
$actualResult = $this->sut->getSize($path); |
254
|
|
|
|
255
|
|
|
$this->assertEquals($size, $actualResult); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testUploaderWillUseRootDirectoryByDefault(): void |
259
|
|
|
{ |
260
|
|
|
$this->sut = new Uploader($this->filesystemMock); |
261
|
|
|
|
262
|
|
|
$key = 'foo'; |
263
|
|
|
$path = 'example.foo'; |
264
|
|
|
|
265
|
|
|
$expectedResult = '/example.foo'; |
266
|
|
|
|
267
|
|
|
$actualResult = $this->sut->getPath($key, $path); |
268
|
|
|
|
269
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testUploaderWillUsePathFactoryIfSet(): void |
273
|
|
|
{ |
274
|
|
|
$this->sut = new Uploader($this->filesystemMock); |
275
|
|
|
|
276
|
|
|
$fooPathFactory = fn ($filename) => "/foo-base-dir/$filename"; |
277
|
|
|
|
278
|
|
|
$key = 'foo'; |
279
|
|
|
$path = 'example.foo'; |
280
|
|
|
|
281
|
|
|
$this->sut->setPathFactory($key, $fooPathFactory); |
282
|
|
|
|
283
|
|
|
$expectedResult = '/foo-base-dir/example.foo'; |
284
|
|
|
|
285
|
|
|
$actualResult = $this->sut->getPath($key, $path); |
286
|
|
|
|
287
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|