Passed
Pull Request — master (#407)
by Kirill
11:08 queued 03:58
created

StorageEngineWriterTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 452
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 219
c 1
b 0
f 0
dl 0
loc 452
rs 10
wmc 18

18 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeleteFile() 0 12 1
A testSetVisibility() 0 13 1
A testTempFileNameThrowsException() 0 18 1
A testMoveFileInSameSystem() 0 17 1
A testMoveFileAcrossSystems() 0 25 1
A testMoveFileInSameSystemThrowsException() 0 22 1
A testMoveSameFileInSameSystem() 0 14 1
A testWriteStream() 0 16 1
A testDeleteFileThrowsException() 0 20 1
A testCopyFileInSameSystem() 0 17 1
A testCopyFileUnknownDestinationSystem() 0 15 1
A testMoveFileUnknownDestinationFs() 0 15 1
A testSetVisibilityThrowsException() 0 23 1
A testWriteFile() 0 15 1
A testWriteStreamThrowsException() 0 27 1
A testWriteFileThrowsException() 0 21 1
A testCopyFileAcrossSystems() 0 22 1
A testCopyFileInSameSystemThrowsException() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit;
6
7
use League\Flysystem\FilesystemOperator;
8
use League\Flysystem\UnableToCopyFile;
9
use League\Flysystem\UnableToDeleteFile;
10
use League\Flysystem\UnableToMoveFile;
11
use League\Flysystem\UnableToReadFile;
12
use League\Flysystem\UnableToSetVisibility;
13
use League\Flysystem\UnableToWriteFile;
14
use Spiral\Storage\Exception\FileOperationException;
15
use Spiral\Storage\Exception\MountException;
16
use Spiral\Storage\Exception\StorageException;
17
18
/**
19
 * tests for StorageWriterInterface methods
20
 */
21
class StorageEngineWriterTest extends StorageEngineAbstractTest
22
{
23
    private const LOCAL_FS = 'local';
24
25
    /**
26
     * @throws StorageException
27
     * @throws \ReflectionException
28
     */
29
    public function testTempFileNameThrowsException(): void
30
    {
31
        $uri = 'local://file.txt';
32
33
        $localFs = $this->createMock(FilesystemOperator::class);
34
        $localFs->expects($this->once())
35
            ->method('readStream')
36
            ->with('file.txt')
37
            ->willThrowException(
38
                UnableToReadFile::fromLocation('file.txt')
39
            );
40
41
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
42
43
        $this->expectException(FileOperationException::class);
44
        $this->expectExceptionMessage('Unable to read file from location: file.txt.');
45
46
        $storage->tempFilename($uri);
47
    }
48
49
    /**
50
     * @throws StorageException
51
     * @throws \ReflectionException
52
     */
53
    public function testWriteFile(): void
54
    {
55
        $fileName = 'newFile.txt';
56
        $fileContent = 'new File content';
57
58
        $localFs = $this->createMock(FilesystemOperator::class);
59
        $localFs->expects($this->once())
60
            ->method('write')
61
            ->with($fileName, $fileContent, []);
62
63
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
64
65
        $this->assertEquals(
66
            'local://newFile.txt',
67
            $storage->write(static::LOCAL_FS, $fileName, $fileContent)
68
        );
69
    }
70
71
    /**
72
     * @throws StorageException
73
     * @throws \ReflectionException
74
     */
75
    public function testWriteFileThrowsException(): void
76
    {
77
        $fileName = 'newFile.txt';
78
        $fileContent = 'new File content';
79
80
        $localFs = $this->createMock(FilesystemOperator::class);
81
        $localFs->expects($this->once())
82
            ->method('write')
83
            ->with($fileName, $fileContent, [])
84
            ->willThrowException(
85
                UnableToWriteFile::atLocation($fileName, 'test reason')
86
            );
87
88
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
89
90
        $this->expectException(FileOperationException::class);
91
        $this->expectExceptionMessageMatches(
92
            '/^Unable to write file at location: newFile.txt. test reason/'
93
        );
94
95
        $storage->write(static::LOCAL_FS, $fileName, $fileContent);
96
    }
97
98
    /**
99
     * @throws StorageException
100
     */
101
    public function testWriteStream(): void
102
    {
103
        $fileName = 'newFile.txt';
104
        $fileContent = 'new File content';
105
        $config = ['visibility' => 'public'];
106
107
        $localFs = $this->createMock(FilesystemOperator::class);
108
        $localFs->expects($this->once())
109
            ->method('writeStream')
110
            ->with($fileName, $fileContent, $config);
111
112
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
113
114
        $this->assertEquals(
115
            'local://newFile.txt',
116
            $storage->writeStream(static::LOCAL_FS, $fileName, $fileContent, $config)
117
        );
118
    }
119
120
    /**
121
     * @throws StorageException
122
     * @throws \ReflectionException
123
     */
124
    public function testWriteStreamThrowsException(): void
125
    {
126
        $fileName = 'newFile.txt';
127
128
        $localFs = $this->createMock(FilesystemOperator::class);
129
        $localFs->expects($this->once())
130
            ->method('writeStream')
131
            ->willThrowException(
132
                UnableToWriteFile::atLocation($fileName, 'test reason')
133
            );
134
135
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
136
137
        $this->expectException(FileOperationException::class);
138
        $this->expectExceptionMessageMatches(
139
            '/^Unable to write file at location: newFile.txt. test reason/'
140
        );
141
142
        $resource = fopen('php://memory', 'rb+');
143
144
        $storage->writeStream(
145
            static::LOCAL_FS,
146
            $fileName,
147
            stream_get_contents($resource)
148
        );
149
150
        fclose($resource);
151
    }
152
153
    /**
154
     * @throws StorageException
155
     * @throws \ReflectionException
156
     */
157
    public function testSetVisibility(): void
158
    {
159
        $uri = 'local://newFile.txt';
160
        $newVisibility = 'private';
161
162
        $localFs = $this->createMock(FilesystemOperator::class);
163
        $localFs->expects($this->once())
164
            ->method('setVisibility')
165
            ->with('newFile.txt', $newVisibility);
166
167
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
168
169
        $storage->setVisibility($uri, $newVisibility);
170
    }
171
172
    /**
173
     * @throws StorageException
174
     * @throws \ReflectionException
175
     */
176
    public function testSetVisibilityThrowsException(): void
177
    {
178
        $storage = $this->buildSimpleStorageEngine();
0 ignored issues
show
Unused Code introduced by
The assignment to $storage is dead and can be removed.
Loading history...
179
180
        $uri = 'local://newFile.txt';
181
        $newVisibility = 'private';
182
183
        $localFs = $this->createMock(FilesystemOperator::class);
184
        $localFs->expects($this->once())
185
            ->method('setVisibility')
186
            ->with('newFile.txt', $newVisibility)
187
            ->willThrowException(
188
                UnableToSetVisibility::atLocation('newFile.txt', 'test reason')
189
            );
190
191
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
192
193
        $this->expectException(FileOperationException::class);
194
        $this->expectExceptionMessageMatches(
195
            '/^Unable to set visibility for file newFile.txt. test reason/'
196
        );
197
198
        $storage->setVisibility($uri, $newVisibility);
199
    }
200
201
    /**
202
     * @throws StorageException
203
     * @throws \ReflectionException
204
     */
205
    public function testDeleteFile(): void
206
    {
207
        $uri = 'local://file.txt';
208
209
        $localFs = $this->createMock(FilesystemOperator::class);
210
        $localFs->expects($this->once())
211
            ->method('delete')
212
            ->with('file.txt');
213
214
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
215
216
        $storage->delete($uri);
217
    }
218
219
    /**
220
     * @throws StorageException
221
     */
222
    public function testDeleteFileThrowsException(): void
223
    {
224
        $uri = 'local://file.txt';
225
226
        $localFs = $this->createMock(FilesystemOperator::class);
227
        $localFs->expects($this->once())
228
            ->method('delete')
229
            ->with('file.txt')
230
            ->willThrowException(
231
                UnableToDeleteFile::atLocation('file.txt', 'test reason')
232
            );
233
234
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
235
236
        $this->expectException(FileOperationException::class);
237
        $this->expectExceptionMessageMatches(
238
            '/^Unable to delete file located at: file.txt. test reason/'
239
        );
240
241
        $storage->delete($uri);
242
    }
243
244
    /**
245
     * @throws StorageException
246
     * @throws \ReflectionException
247
     */
248
    public function testMoveFileInSameSystem(): void
249
    {
250
        $sourceUri = 'local://file.txt';
251
        $destinationFs = 'local';
252
        $targetFilePath = 'movedFile.txt';
253
        $config = ['visibility' => 'private'];
254
255
        $localFs = $this->createMock(FilesystemOperator::class);
256
        $localFs->expects($this->once())
257
            ->method('move')
258
            ->with('file.txt', $targetFilePath, $config);
259
260
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
261
262
        $this->assertEquals(
263
            \sprintf('%s://%s', $destinationFs, $targetFilePath),
264
            $storage->move($sourceUri, $destinationFs, $targetFilePath, $config)
265
        );
266
    }
267
268
    /**
269
     * @throws StorageException
270
     * @throws \ReflectionException
271
     */
272
    public function testMoveSameFileInSameSystem(): void
273
    {
274
        $sourceUri = 'local://file.txt';
275
        $destinationFs = 'local';
276
277
        $localFs = $this->createMock(FilesystemOperator::class);
278
        $localFs->expects($this->never())
279
            ->method('move');
280
281
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
282
283
        $this->assertEquals(
284
            \sprintf('%s://%s', $destinationFs, 'file.txt'),
285
            $storage->move($sourceUri, $destinationFs)
286
        );
287
    }
288
289
    /**
290
     * @throws StorageException
291
     * @throws \ReflectionException
292
     */
293
    public function testMoveFileAcrossSystems(): void
294
    {
295
        $sourceUri = 'local://file.txt';
296
        $filePath = 'file.txt';
297
        $destinationFs = 'local2';
298
299
        $localFs = $this->createMock(FilesystemOperator::class);
300
        $localFs->expects($this->once())
301
            ->method('readStream')
302
            ->with($filePath);
303
        $localFs->expects($this->once())
304
            ->method('delete')
305
            ->with($filePath);
306
307
308
        $localFs2 = $this->createMock(FilesystemOperator::class);
309
        $localFs2->expects($this->once())
310
            ->method('writeStream');
311
312
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
313
        $this->mountStorageEngineFileSystem($storage, $destinationFs, $localFs2);
314
315
        $this->assertEquals(
316
            \sprintf('%s://%s', $destinationFs, $filePath),
317
            $storage->move($sourceUri, $destinationFs)
318
        );
319
    }
320
321
    /**
322
     * @throws StorageException
323
     * @throws \ReflectionException
324
     */
325
    public function testMoveFileUnknownDestinationFs(): void
326
    {
327
        $sourceUri = 'local://file.txt';
328
        $destinationFs = 'missed';
329
330
        $localFs = $this->createMock(FilesystemOperator::class);
331
332
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
333
334
        $this->expectException(MountException::class);
335
        $this->expectExceptionMessage('Filesystem `missed` has not been defined');
336
337
        $this->assertEquals(
338
            \sprintf('%s://%s', $destinationFs, 'file.txt'),
339
            $storage->move($sourceUri, $destinationFs)
340
        );
341
    }
342
343
    /**
344
     * @throws StorageException
345
     * @throws \ReflectionException
346
     */
347
    public function testMoveFileInSameSystemThrowsException(): void
348
    {
349
        $sourceUri = 'local://file.txt';
350
        $destinationFs = 'local';
351
        $targetFilePath = 'movedFile.txt';
352
353
        $localFs = $this->createMock(FilesystemOperator::class);
354
        $localFs->expects($this->once())
355
            ->method('move')
356
            ->with('file.txt', $targetFilePath, [])
357
            ->willThrowException(
358
                UnableToMoveFile::fromLocationTo('file.txt', $targetFilePath)
359
            );
360
361
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
362
363
        $this->expectException(FileOperationException::class);
364
        $this->expectExceptionMessage('Unable to move file from file.txt to movedFile.txt');
365
366
        $this->assertEquals(
367
            \sprintf('%s://%s', $destinationFs, $targetFilePath),
368
            $storage->move($sourceUri, $destinationFs, $targetFilePath)
369
        );
370
    }
371
372
    /**
373
     * @throws StorageException
374
     * @throws \ReflectionException
375
     */
376
    public function testCopyFileInSameSystem(): void
377
    {
378
        $sourceUri = 'local://file.txt';
379
        $destinationFs = 'local';
380
        $targetFilePath = 'copiedFile.txt';
381
        $config = ['visibility' => 'private'];
382
383
        $localFs = $this->createMock(FilesystemOperator::class);
384
        $localFs->expects($this->once())
385
            ->method('copy')
386
            ->with('file.txt', $targetFilePath, $config);
387
388
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
389
390
        $this->assertEquals(
391
            \sprintf('%s://%s', $destinationFs, $targetFilePath),
392
            $storage->copy($sourceUri, $destinationFs, $targetFilePath, $config)
393
        );
394
    }
395
396
    /**
397
     * @throws StorageException
398
     * @throws \ReflectionException
399
     */
400
    public function testCopyFileAcrossSystems(): void
401
    {
402
        $sourceUri = 'local://file.txt';
403
        $filePath = 'file.txt';
404
        $destinationFs = 'local2';
405
        $config = ['visibility' => 'public'];
406
407
        $localFs = $this->createMock(FilesystemOperator::class);
408
        $localFs->expects($this->once())
409
            ->method('readStream')
410
            ->with($filePath);
411
412
        $localFs2 = $this->createMock(FilesystemOperator::class);
413
        $localFs2->expects($this->once())
414
            ->method('writeStream');
415
416
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
417
        $this->mountStorageEngineFileSystem($storage, $destinationFs, $localFs2);
418
419
        $this->assertEquals(
420
            \sprintf('%s://%s', $destinationFs, $filePath),
421
            $storage->copy($sourceUri, $destinationFs, null, $config)
422
        );
423
    }
424
425
    /**
426
     * @throws StorageException
427
     * @throws \ReflectionException
428
     */
429
    public function testCopyFileUnknownDestinationSystem(): void
430
    {
431
        $sourceUri = 'local://file.txt';
432
        $destinationFs = 'missed';
433
434
        $localFs = $this->createMock(FilesystemOperator::class);
435
436
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
437
438
        $this->expectException(MountException::class);
439
        $this->expectExceptionMessage('Filesystem `missed` has not been defined');
440
441
        $this->assertEquals(
442
            \sprintf('%s://%s', $destinationFs, 'file.txt'),
443
            $storage->copy($sourceUri, $destinationFs)
444
        );
445
    }
446
447
    /**
448
     * @throws StorageException
449
     * @throws \ReflectionException
450
     */
451
    public function testCopyFileInSameSystemThrowsException(): void
452
    {
453
        $sourceUri = 'local://file.txt';
454
        $destinationFs = 'local';
455
        $targetFilePath = 'movedFile.txt';
456
457
        $localFs = $this->createMock(FilesystemOperator::class);
458
        $localFs->expects($this->once())
459
            ->method('copy')
460
            ->with('file.txt', $targetFilePath, [])
461
            ->willThrowException(
462
                UnableToCopyFile::fromLocationTo('file.txt', $targetFilePath)
463
            );
464
465
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
466
467
        $this->expectException(FileOperationException::class);
468
        $this->expectExceptionMessage('Unable to move file from file.txt to movedFile.txt');
469
470
        $this->assertEquals(
471
            \sprintf('%s://%s', $destinationFs, $targetFilePath),
472
            $storage->copy($sourceUri, $destinationFs, $targetFilePath)
473
        );
474
    }
475
}
476