Passed
Pull Request — master (#407)
by Kirill
06:33
created

StorageEngineReaderTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 107
c 1
b 0
f 0
dl 0
loc 262
rs 10
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testFileSizeThrowsException() 0 15 1
A testLastModifiedThrowsException() 0 15 1
A testLastModified() 0 10 1
A testReadThrowsException() 0 15 1
A testRead() 0 10 1
A testFileSize() 0 10 1
A testMimeTypeThrowsException() 0 15 1
A testFileExistsThrowsException() 0 15 1
A testReadStream() 0 10 1
A testMimeType() 0 10 1
A testReadStreamThrowsException() 0 15 1
A testFileExists() 0 10 1
A testVisibilityThrowsException() 0 15 1
A testVisibility() 0 10 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\UnableToCheckFileExistence;
9
use League\Flysystem\UnableToReadFile;
10
use League\Flysystem\UnableToRetrieveMetadata;
11
use Spiral\Storage\Exception\FileOperationException;
12
use Spiral\Storage\Exception\StorageException;
13
14
/**
15
 * tests for StorageReaderInterface methods
16
 */
17
class StorageEngineReaderTest extends StorageEngineAbstractTest
18
{
19
    private const LOCAL_FS = 'local';
20
21
    /**
22
     * @throws StorageException
23
     * @throws \ReflectionException
24
     */
25
    public function testFileExists(): void
26
    {
27
        $localFs = $this->createMock(FilesystemOperator::class);
28
        $localFs->expects($this->once())
29
            ->method('fileExists')
30
            ->with('file.txt');
31
32
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
33
34
        $storage->fileExists('local://file.txt');
35
    }
36
37
    /**
38
     * @throws StorageException
39
     * @throws \ReflectionException
40
     * @throws \Spiral\Storage\Exception\ConfigException
41
     */
42
    public function testFileExistsThrowsException(): void
43
    {
44
        $localFs = $this->createMock(FilesystemOperator::class);
45
        $localFs->expects($this->once())
46
            ->method('fileExists')
47
            ->willThrowException(
48
                UnableToCheckFileExistence::forLocation('file.txt')
49
            );
50
51
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
52
53
        $this->expectException(FileOperationException::class);
54
        $this->expectExceptionMessage('Unable to check file existence for: file.txt');
55
56
        $storage->fileExists('local://file.txt');
57
    }
58
59
    /**
60
     * @throws StorageException
61
     * @throws \ReflectionException
62
     */
63
    public function testRead(): void
64
    {
65
        $localFs = $this->createMock(FilesystemOperator::class);
66
        $localFs->expects($this->once())
67
            ->method('read')
68
            ->with('file.txt');
69
70
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
71
72
        $storage->read('local://file.txt');
73
    }
74
75
    /**
76
     * @throws StorageException
77
     * @throws \ReflectionException
78
     */
79
    public function testReadThrowsException(): void
80
    {
81
        $localFs = $this->createMock(FilesystemOperator::class);
82
        $localFs->expects($this->once())
83
            ->method('read')
84
            ->willThrowException(
85
                UnableToReadFile::fromLocation('file.txt')
86
            );
87
88
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
89
90
        $this->expectException(FileOperationException::class);
91
        $this->expectExceptionMessage('Unable to read file from location: file.txt.');
92
93
        $storage->read('local://file.txt');
94
    }
95
96
    /**
97
     * @throws StorageException
98
     * @throws \ReflectionException
99
     */
100
    public function testReadStream(): void
101
    {
102
        $localFs = $this->createMock(FilesystemOperator::class);
103
        $localFs->expects($this->once())
104
            ->method('readStream')
105
            ->with('file.txt');
106
107
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
108
109
        $storage->readStream('local://file.txt');
110
    }
111
112
    /**
113
     * @throws StorageException
114
     * @throws \ReflectionException
115
     */
116
    public function testReadStreamThrowsException(): void
117
    {
118
        $localFs = $this->createMock(FilesystemOperator::class);
119
        $localFs->expects($this->once())
120
            ->method('readStream')
121
            ->willThrowException(
122
                UnableToReadFile::fromLocation('file.txt')
123
            );
124
125
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
126
127
        $this->expectException(FileOperationException::class);
128
        $this->expectExceptionMessage('Unable to read file from location: file.txt.');
129
130
        $storage->readStream('local://file.txt');
131
    }
132
133
    /**
134
     * @throws StorageException
135
     * @throws \ReflectionException
136
     */
137
    public function testLastModified(): void
138
    {
139
        $localFs = $this->createMock(FilesystemOperator::class);
140
        $localFs->expects($this->once())
141
            ->method('lastModified')
142
            ->with('file.txt');
143
144
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
145
146
        $storage->lastModified('local://file.txt');
147
    }
148
149
    /**
150
     * @throws StorageException
151
     * @throws \ReflectionException
152
     */
153
    public function testLastModifiedThrowsException(): void
154
    {
155
        $localFs = $this->createMock(FilesystemOperator::class);
156
        $localFs->expects($this->once())
157
            ->method('lastModified')
158
            ->willThrowException(
159
                UnableToRetrieveMetadata::lastModified('file.txt')
160
            );
161
162
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
163
164
        $this->expectException(FileOperationException::class);
165
        $this->expectExceptionMessage('Unable to retrieve the last_modified for file at location: file.txt.');
166
167
        $storage->lastModified('local://file.txt');
168
    }
169
170
    /**
171
     * @throws StorageException
172
     * @throws \ReflectionException
173
     */
174
    public function testFileSize(): void
175
    {
176
        $localFs = $this->createMock(FilesystemOperator::class);
177
        $localFs->expects($this->once())
178
            ->method('fileSize')
179
            ->with('file.txt');
180
181
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
182
183
        $storage->fileSize('local://file.txt');
184
    }
185
186
    /**
187
     * @throws StorageException
188
     * @throws \ReflectionException
189
     */
190
    public function testFileSizeThrowsException(): void
191
    {
192
        $localFs = $this->createMock(FilesystemOperator::class);
193
        $localFs->expects($this->once())
194
            ->method('fileSize')
195
            ->willThrowException(
196
                UnableToRetrieveMetadata::fileSize('file.txt')
197
            );
198
199
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
200
201
        $this->expectException(FileOperationException::class);
202
        $this->expectExceptionMessage('Unable to retrieve the file_size for file at location: file.txt.');
203
204
        $storage->fileSize('local://file.txt');
205
    }
206
207
    /**
208
     * @throws StorageException
209
     * @throws \ReflectionException
210
     */
211
    public function testMimeType(): void
212
    {
213
        $localFs = $this->createMock(FilesystemOperator::class);
214
        $localFs->expects($this->once())
215
            ->method('mimeType')
216
            ->with('file.txt');
217
218
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
219
220
        $storage->mimeType('local://file.txt');
221
    }
222
223
    /**
224
     * @throws StorageException
225
     * @throws \ReflectionException
226
     */
227
    public function testMimeTypeThrowsException(): void
228
    {
229
        $localFs = $this->createMock(FilesystemOperator::class);
230
        $localFs->expects($this->once())
231
            ->method('mimeType')
232
            ->willThrowException(
233
                UnableToRetrieveMetadata::mimeType('file.txt')
234
            );
235
236
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
237
238
        $this->expectException(FileOperationException::class);
239
        $this->expectExceptionMessage('Unable to retrieve the mime_type for file at location: file.txt.');
240
241
        $storage->mimeType('local://file.txt');
242
    }
243
244
    /**
245
     * @throws StorageException
246
     * @throws \ReflectionException
247
     */
248
    public function testVisibility(): void
249
    {
250
        $localFs = $this->createMock(FilesystemOperator::class);
251
        $localFs->expects($this->once())
252
            ->method('visibility')
253
            ->with('file.txt');
254
255
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
256
257
        $storage->visibility('local://file.txt');
258
    }
259
260
    /**
261
     * @throws StorageException
262
     * @throws \ReflectionException
263
     */
264
    public function testVisibilityThrowsException(): void
265
    {
266
        $localFs = $this->createMock(FilesystemOperator::class);
267
        $localFs->expects($this->once())
268
            ->method('visibility')
269
            ->willThrowException(
270
                UnableToRetrieveMetadata::visibility('file.txt')
271
            );
272
273
        $storage = $this->buildSimpleStorageEngine(static::LOCAL_FS, $localFs);
274
275
        $this->expectException(FileOperationException::class);
276
        $this->expectExceptionMessage('Unable to retrieve the visibility for file at location: file.txt.');
277
278
        $storage->visibility('local://file.txt');
279
    }
280
}
281