Passed
Pull Request — master (#407)
by Kirill
05:29
created

StorageEngineForLocalTestCase::testFileExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Feature;
6
7
use org\bovigo\vfs\vfsStream as VfsStream;
8
use Spiral\Storage\Exception\FileOperationException;
9
use Spiral\Storage\Exception\StorageException;
10
use Spiral\Storage\Storage;
11
use Spiral\Tests\Storage\Traits\LocalFsBuilderTrait;
12
use Spiral\Tests\Storage\Traits\StorageConfigTrait;
13
14
class StorageEngineForLocalTestCase extends FeatureTestCase
15
{
16
    use LocalFsBuilderTrait;
17
    use StorageConfigTrait;
18
19
    private const ROOT_FILE_NAME = 'file.txt';
20
    private const ROOT_FILE_CONTENT = 'file text';
21
22
    private $rootDir;
23
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->rootDir = VfsStream::setup(self::ROOT_DIR, 777);
29
    }
30
31
    /**
32
     * @throws StorageException
33
     */
34
    public function testTempFilenameNoUri(): void
35
    {
36
        $this->buildSimpleVfsStructure();
37
38
        $engine = new Storage(
39
            $this->buildStorageConfig(['local' => $this->buildLocalInfoDescription(true)]),
40
            $this->getUriParser()
41
        );
42
43
        $this->assertMatchesRegularExpression('/^\/tmp\/tmpStorageFile_[\w]*$/', $engine->tempFilename());
0 ignored issues
show
Bug introduced by
The method assertMatchesRegularExpression() does not exist on Spiral\Tests\Storage\Fea...eEngineForLocalTestCase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               assertMatchesRegularExpression('/^\/tmp\/tmpStorageFile_[\w]*$/', $engine->tempFilename());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
    }
45
46
    /**
47
     * @throws StorageException
48
     */
49
    public function testTempFilenameUri(): void
50
    {
51
        $this->buildSimpleVfsStructure();
52
53
        $engine = new Storage(
54
            $this->buildStorageConfig(['local' => $this->buildLocalInfoDescription(true)]),
55
            $this->getUriParser()
56
        );
57
58
        $tmpFilePath = $engine->tempFilename('localBucket://' . static::ROOT_FILE_NAME);
59
60
        $this->assertMatchesRegularExpression(
61
            \sprintf('/^\/tmp\/%s_[\w]*$/', static::ROOT_FILE_NAME),
62
            $tmpFilePath
63
        );
64
65
        $this->assertEquals(static::ROOT_FILE_CONTENT, file_get_contents($tmpFilePath));
66
    }
67
68
    /**
69
     * @throws StorageException
70
     */
71
    public function testFileExists(): void
72
    {
73
        $this->buildSimpleVfsStructure();
74
75
        $storageEngine = $this->buildStorageForFs('local');
76
77
        $this->assertTrue(
78
            $storageEngine->fileExists('localBucket://' . self::ROOT_FILE_NAME)
79
        );
80
81
        $this->assertFalse(
82
            $storageEngine->fileExists('localBucket://file_missed.txt')
83
        );
84
    }
85
86
    /**
87
     * @throws StorageException
88
     */
89
    public function testFileExistsWrongFsThrowsException(): void
90
    {
91
        $this->buildSimpleVfsStructure();
92
93
        $this->expectException(StorageException::class);
94
        $this->expectExceptionMessage('Filesystem `other` was not identified');
95
96
        $this->buildStorageForFs('local')->fileExists('other://' . static::ROOT_FILE_NAME);
97
    }
98
99
    /**
100
     * @throws StorageException
101
     */
102
    public function testFileExistsWrongFormatThrowsException(): void
103
    {
104
        $this->buildSimpleVfsStructure();
105
106
        $uri = 'other-//file.txt';
107
108
        $this->expectException(StorageException::class);
109
        $this->expectExceptionMessage(\sprintf('No uri structure was detected in uri `%s`', $uri));
110
111
        $this->buildStorageForFs('local')->fileExists($uri);
112
    }
113
114
    /**
115
     * @throws StorageException
116
     */
117
    public function testReadFile(): void
118
    {
119
        $this->buildSimpleVfsStructure();
120
121
        $storageEngine = $this->buildStorageForFs('local');
122
123
        $this->assertEquals(
124
            static::ROOT_FILE_CONTENT,
125
            $storageEngine->read('localBucket://' . static::ROOT_FILE_NAME)
126
        );
127
    }
128
129
    /**
130
     * @throws StorageException
131
     */
132
    public function testReadNonExistingFileThrowsException(): void
133
    {
134
        $this->buildSimpleVfsStructure();
135
136
        $storageEngine = $this->buildStorageForFs('local');
137
138
        $this->expectException(FileOperationException::class);
139
        $this->expectExceptionMessageMatches('/^Unable to read file from location: file_missed.txt./');
140
141
        $storageEngine->read('localBucket://file_missed.txt');
142
    }
143
144
    /**
145
     * @throws StorageException
146
     */
147
    public function testReadStream(): void
148
    {
149
        $this->buildSimpleVfsStructure();
150
151
        $storageEngine = $this->buildStorageForFs('local');
152
153
        $this->assertIsResource($storageEngine->readStream('localBucket://' . static::ROOT_FILE_NAME));
154
    }
155
156
    /**
157
     * @throws StorageException
158
     */
159
    public function testReadStreamNonExistingFileThrowsException(): void
160
    {
161
        $this->buildSimpleVfsStructure();
162
163
        $storageEngine = $this->buildStorageForFs('local');
164
165
        $this->expectException(FileOperationException::class);
166
        $this->expectExceptionMessageMatches('/^Unable to read file from location: file_missed.txt./');
167
168
        $storageEngine->readStream('localBucket://file_missed.txt');
169
    }
170
171
    /**
172
     * @throws StorageException
173
     */
174
    public function testLastModified(): void
175
    {
176
        $today = new \DateTimeImmutable();
177
178
        $this->buildSimpleVfsStructure();
179
180
        $storageEngine = $this->buildStorageForFs('local');
181
182
        $fileLastModified = $storageEngine->lastModified('localBucket://' . static::ROOT_FILE_NAME);
183
        $dateLastModified = $today->setTimestamp($fileLastModified);
184
185
        $this->assertIsInt($fileLastModified);
186
        $this->assertNotEquals($today, $dateLastModified);
187
188
        $this->assertEquals($today->format('Y-m-d'), $dateLastModified->format('Y-m-d'));
189
    }
190
191
    /**
192
     * @throws StorageException
193
     */
194
    public function testLastModifiedNonExistingFileThrowsException(): void
195
    {
196
        $this->buildSimpleVfsStructure();
197
198
        $storageEngine = $this->buildStorageForFs('local');
199
200
        $this->expectException(FileOperationException::class);
201
        $this->expectExceptionMessageMatches(
202
            '/^Unable to retrieve the last_modified for file at location: file_missed.txt./'
203
        );
204
205
        $storageEngine->lastModified('localBucket://file_missed.txt');
206
    }
207
208
    /**
209
     * @throws StorageException
210
     */
211
    public function testFileSize(): void
212
    {
213
        $this->buildSimpleVfsStructure();
214
215
        $storageEngine = $this->buildStorageForFs('local');
216
217
        $fileSize = $storageEngine->fileSize('localBucket://' . static::ROOT_FILE_NAME);
218
219
        $this->assertIsInt($fileSize);
220
        $this->assertNotEmpty($fileSize);
221
    }
222
223
    /**
224
     * @throws StorageException
225
     */
226
    public function testFileSizeNonExistingFileThrowsException(): void
227
    {
228
        $this->buildSimpleVfsStructure();
229
230
        $storageEngine = $this->buildStorageForFs('local');
231
232
        $this->expectException(FileOperationException::class);
233
        $this->expectExceptionMessageMatches(
234
            '/^Unable to retrieve the file_size for file at location: file_missed.txt./'
235
        );
236
237
        $storageEngine->fileSize('localBucket://file_missed.txt');
238
    }
239
240
    /**
241
     * @throws StorageException
242
     */
243
    public function testMimeType(): void
244
    {
245
        $this->buildSimpleVfsStructure();
246
247
        $storageEngine = $this->buildStorageForFs('local');
248
249
        $this->assertEquals('text/plain', $storageEngine->mimeType('localBucket://' . static::ROOT_FILE_NAME));
250
    }
251
252
    /**
253
     * @throws StorageException
254
     */
255
    public function testMimeTypeNonExistingFileThrowsException(): void
256
    {
257
        $this->buildSimpleVfsStructure();
258
259
        $storageEngine = $this->buildStorageForFs('local');
260
261
        $this->expectException(FileOperationException::class);
262
        $this->expectExceptionMessageMatches(
263
            '/^Unable to retrieve the mime_type for file at location: file_missed.txt./'
264
        );
265
266
        $storageEngine->mimeType('localBucket://file_missed.txt');
267
    }
268
269
    /**
270
     * @throws StorageException
271
     */
272
    public function testVisibility(): void
273
    {
274
        $this->buildSimpleVfsStructure();
275
276
        $storageEngine = $this->buildStorageForFs('local');
277
278
        $this->assertEquals('public', $storageEngine->visibility('localBucket://' . static::ROOT_FILE_NAME));
279
    }
280
281
    /**
282
     * @throws StorageException
283
     */
284
    public function testVisibilityNonExistingFileThrowsException(): void
285
    {
286
        $this->buildSimpleVfsStructure();
287
288
        $storageEngine = $this->buildStorageForFs('local');
289
290
        $this->expectException(FileOperationException::class);
291
        $this->expectExceptionMessageMatches(
292
            '/^Unable to retrieve the visibility for file at location: file_missed.txt./'
293
        );
294
295
        $storageEngine->visibility('localBucket://file_missed.txt');
296
    }
297
298
    private function buildSimpleVfsStructure(): void
299
    {
300
        VfsStream::create(
301
            [
302
                static::ROOT_FILE_NAME => static::ROOT_FILE_CONTENT,
303
                static::ROOT_DIR_NAME => [
0 ignored issues
show
Bug introduced by
The constant Spiral\Tests\Storage\Fea...TestCase::ROOT_DIR_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
304
                    'dir_file.txt' => 'dir file content'
305
                ],
306
            ],
307
            $this->rootDir
308
        );
309
    }
310
311
    /**
312
     * @param string $name
313
     *
314
     * @return Storage
315
     *
316
     * @throws StorageException
317
     */
318
    private function buildStorageForFs(string $name): Storage
319
    {
320
        return new Storage(
321
            $this->buildStorageConfig([$name => $this->buildLocalInfoDescription(true)]),
322
            $this->getUriParser()
323
        );
324
    }
325
}
326