Passed
Pull Request — master (#407)
by Kirill
08:54
created

ManagerTestCase   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 133
c 1
b 0
f 0
dl 0
loc 292
rs 10
wmc 25

24 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidUri() 0 11 1
A testMoveToSameStorage() 0 11 1
A testCreate() 0 7 1
A testIterator() 0 14 1
A testCopyToAnotherStorage() 0 18 1
A testUriWithInvalidStorage() 0 8 1
A setUp() 0 6 1
A testCount() 0 9 1
A testReadingAsStream() 0 15 2
A testMime() 0 5 1
A testResolverByName() 0 3 1
A testDefaultResolver() 0 3 1
A testSize() 0 6 1
A testWriteString() 0 9 1
A testDelete() 0 7 1
A testExisting() 0 7 1
A testLastModified() 0 14 1
A testUnknownResolver() 0 6 1
A testUriAutoResolvable() 0 7 1
A testWriteStream() 0 12 1
A testAddedResolver() 0 6 1
A testCopyToSameStorage() 0 13 1
A testMoveToAnotherStorage() 0 17 1
A testVisibility() 0 18 1
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Storage;
13
14
use League\Flysystem\Local\LocalFilesystemAdapter;
15
use Spiral\Storage\Exception\InvalidArgumentException;
16
use Spiral\Storage\Storage;
17
use Spiral\Storage\Bucket;
18
use Spiral\Storage\Visibility;
19
20
/**
21
 * @group unit
22
 */
23
class ManagerTestCase extends TestCase
24
{
25
    /**
26
     * @var Storage
27
     */
28
    private $manager;
29
30
    public function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->manager = new Storage();
35
        $this->manager->add(Storage::DEFAULT_STORAGE, $this->local);
36
    }
37
38
    public function testDefaultResolver(): void
39
    {
40
        $this->assertSame($this->local, $this->manager->bucket());
41
    }
42
43
    public function testResolverByName(): void
44
    {
45
        $this->assertSame($this->local, $this->manager->bucket('default'));
46
    }
47
48
    public function testUnknownResolver(): void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
        $this->expectExceptionMessage('Bucket `unknown` has not been defined');
52
53
        $this->manager->bucket('unknown');
54
    }
55
56
    public function testAddedResolver(): void
57
    {
58
        $this->manager->add('known', $this->second);
59
60
        $this->assertSame($this->local, $this->manager->bucket());
61
        $this->assertSame($this->second, $this->manager->bucket('known'));
62
    }
63
64
    public function testIterator(): void
65
    {
66
        $manager = clone $this->manager;
67
68
        $resolvers = \iterator_to_array($manager->getIterator());
69
        $this->assertSame([Storage::DEFAULT_STORAGE => $this->local], $resolvers);
70
71
        $manager->add('example', $this->second);
72
73
        $resolvers = \iterator_to_array($manager->getIterator());
74
        $this->assertSame([
75
            Storage::DEFAULT_STORAGE => $this->local,
76
            'example'                => $this->second
77
        ], $resolvers);
78
    }
79
80
    public function testCount(): void
81
    {
82
        $manager = clone $this->manager;
83
84
        $this->assertSame(1, $manager->count());
85
86
        $manager->add('example', $this->second);
87
88
        $this->assertSame(2, $manager->count());
89
    }
90
91
    public function testInvalidUri(): void
92
    {
93
        $this->expectException(InvalidArgumentException::class);
94
        $this->expectExceptionMessage(
95
            'URI argument must be a valid URI in ' .
96
            '"[STORAGE]://[PATH_TO_FILE]" format, but `test://` given'
97
        );
98
99
        $this->manager->create('test://');
100
101
        $this->cleanTempDirectory();
102
    }
103
104
    public function testUriAutoResolvable(): void
105
    {
106
        $this->manager->create('file.txt');
107
108
        $this->assertTrue($this->manager->exists('file.txt'));
109
110
        $this->cleanTempDirectory();
111
    }
112
113
    public function testUriWithInvalidStorage(): void
114
    {
115
        $this->expectException(InvalidArgumentException::class);
116
        $this->expectExceptionMessage('Bucket `invalid` has not been defined');
117
118
        $this->manager->create('invalid://file.txt');
119
120
        $this->cleanTempDirectory();
121
    }
122
123
    public function testCreate(): void
124
    {
125
        $this->manager->create('file.txt');
126
127
        $this->assertTrue($this->manager->exists('file.txt'));
128
129
        $this->cleanTempDirectory();
130
    }
131
132
    public function testWriteString(): void
133
    {
134
        $content = \random_bytes(64);
135
        $this->manager->write('file.txt', $content);
136
137
        $this->assertTrue($this->manager->exists('file.txt'));
138
        $this->assertSame($content, $this->manager->getContents('file.txt'));
139
140
        $this->cleanTempDirectory();
141
    }
142
143
    public function testWriteStream(): void
144
    {
145
        $content = \random_bytes(64);
146
        $stream = \fopen('php://memory', 'ab+');
147
        \fwrite($stream, $content);
148
149
        $this->manager->write('file.txt', $stream);
150
151
        $this->assertTrue($this->manager->exists('file.txt'));
152
        $this->assertSame($content, $this->manager->getContents('file.txt'));
153
154
        $this->cleanTempDirectory();
155
    }
156
157
    public function testVisibility(): void
158
    {
159
        $this->markTestSkipped(
160
            'This test [' . __FUNCTION__ . '] returns incorrect visibility ' .
161
                'of files on Windows OS. ' .
162
            'It is required to understand the situation'
163
        );
164
165
        $this->manager->create('file.txt');
166
167
        $public = Visibility::VISIBILITY_PUBLIC;
168
        $private = Visibility::VISIBILITY_PRIVATE;
169
170
        $this->manager->setVisibility('file.txt', $public);
171
        $this->assertSame($public, $this->manager->getVisibility('file.txt'));
172
173
        $this->manager->setVisibility('file.txt', $private);
174
        $this->assertSame($private, $this->manager->getVisibility('file.txt'));
175
    }
176
177
    public function testCopyToSameStorage(): void
178
    {
179
        $content = \random_bytes(64);
180
        $this->manager->write('source.txt', $content);
181
        $this->manager->copy('source.txt', 'copy.txt');
182
183
        $this->assertTrue($this->manager->exists('source.txt'));
184
        $this->assertSame($content, $this->manager->getContents('source.txt'));
185
186
        $this->assertTrue($this->manager->exists('copy.txt'));
187
        $this->assertSame($content, $this->manager->getContents('copy.txt'));
188
189
        $this->cleanTempDirectory();
190
    }
191
192
    public function testCopyToAnotherStorage(): void
193
    {
194
        $manager = clone $this->manager;
195
        $manager->add('copy', $this->second);
196
197
        $content = \random_bytes(64);
198
        $manager->write('source.txt', $content);
199
        $manager->copy('source.txt', 'copy://copy.txt');
200
201
        $this->assertTrue($manager->exists('source.txt'));
202
        $this->assertSame($content, $manager->getContents('source.txt'));
203
        $this->assertFalse($manager->exists('copy.txt'));
204
205
        $this->assertTrue($manager->exists('copy://copy.txt'));
206
        $this->assertSame($content, $manager->getContents('copy://copy.txt'));
207
        $this->assertFalse($manager->exists('copy://source.txt'));
208
209
        $this->cleanTempDirectory();
210
    }
211
212
    public function testMoveToSameStorage(): void
213
    {
214
        $content = \random_bytes(64);
215
        $this->manager->write('source.txt', $content);
216
        $this->manager->move('source.txt', 'moved.txt');
217
218
        $this->assertFalse($this->manager->exists('source.txt'));
219
        $this->assertTrue($this->manager->exists('moved.txt'));
220
        $this->assertSame($content, $this->manager->getContents('moved.txt'));
221
222
        $this->cleanTempDirectory();
223
    }
224
225
    public function testMoveToAnotherStorage(): void
226
    {
227
        $manager = clone $this->manager;
228
        $manager->add('move', $this->second);
229
230
        $content = \random_bytes(64);
231
        $manager->write('source.txt', $content);
232
        $manager->move('source.txt', 'move://moved.txt');
233
234
        $this->assertFalse($manager->exists('source.txt'));
235
        $this->assertFalse($manager->exists('moved.txt'));
236
237
        $this->assertTrue($manager->exists('move://moved.txt'));
238
        $this->assertSame($content, $manager->getContents('move://moved.txt'));
239
        $this->assertFalse($manager->exists('move://source.txt'));
240
241
        $this->cleanTempDirectory();
242
    }
243
244
    public function testDelete(): void
245
    {
246
        $this->manager->create('file.txt');
247
        $this->assertTrue($this->manager->exists('file.txt'));
248
249
        $this->manager->delete('file.txt');
250
        $this->assertFalse($this->manager->exists('file.txt'));
251
    }
252
253
    public function testReadingAsStream(): void
254
    {
255
        $content = \random_bytes(64);
256
        $this->manager->write('file.txt', $content);
257
258
        $actual = '';
259
        $stream = $this->manager->getStream('file.txt');
260
        while (!\feof($stream)) {
261
            $actual .= \fread($stream, 256);
262
        }
263
        \fclose($stream);
264
265
        $this->assertSame($actual, $content);
266
267
        $this->cleanTempDirectory();
268
    }
269
270
    public function testExisting(): void
271
    {
272
        $this->assertFalse($this->manager->exists('file.txt'));
273
        $this->manager->create('file.txt');
274
        $this->assertTrue($this->manager->exists('file.txt'));
275
276
        $this->cleanTempDirectory();
277
    }
278
279
    /**
280
     * Note: This test may fail since it focuses on a mutable value
281
     */
282
    public function testLastModified(): void
283
    {
284
        $now = \time();
285
286
        $this->manager->create('file.txt');
287
        $before = $this->manager->getLastModified('file.txt');
288
        $this->assertGreaterThanOrEqual($now, $before);
289
290
        // Wait 1.1 seconds and then again modify file
291
        \usleep(1100000);
292
293
        $this->manager->write('file.txt', 'content');
294
        $after = $this->manager->getLastModified('file.txt');
295
        $this->assertGreaterThan($before, $after);
296
    }
297
298
    public function testSize(): void
299
    {
300
        $content = \random_bytes(\random_int(32, 256));
301
        $this->manager->write('file.txt', $content);
302
303
        $this->assertSame(\strlen($content), $this->manager->getSize('file.txt'));
304
    }
305
306
    /**
307
     * Note: Checking for all existing mime types is not the goal of this
308
     *       test; just need to check the readability of the mime type.
309
     */
310
    public function testMime(): void
311
    {
312
        $this->manager->write('file.txt', 'content');
313
314
        $this->assertSame('text/plain', $this->manager->getMimeType('file.txt'));
315
    }
316
}
317