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

StorageTestCase::testLastModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
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\Storage;
16
use Spiral\Storage\Visibility;
17
18
/**
19
 * @group unit
20
 */
21
class StorageTestCase extends TestCase
22
{
23
    public function testCreate(): void
24
    {
25
        $this->local->create('file.txt');
26
27
        $this->assertTrue($this->local->exists('file.txt'));
28
29
        $this->cleanTempDirectory();
30
    }
31
32
    public function testWriteString(): void
33
    {
34
        $content = \random_bytes(64);
35
        $this->local->write('file.txt', $content);
36
37
        $this->assertTrue($this->local->exists('file.txt'));
38
        $this->assertSame($content, $this->local->getContents('file.txt'));
39
40
        $this->cleanTempDirectory();
41
    }
42
43
    public function testWriteStream(): void
44
    {
45
        $content = \random_bytes(64);
46
        $stream = \fopen('php://memory', 'ab+');
47
        \fwrite($stream, $content);
48
49
        $this->local->write('file.txt', $stream);
50
51
        $this->assertTrue($this->local->exists('file.txt'));
52
        $this->assertSame($content, $this->local->getContents('file.txt'));
53
54
        $this->cleanTempDirectory();
55
    }
56
57
    public function testVisibility(): void
58
    {
59
        $this->markTestSkipped(
60
            'This test [' . __FUNCTION__ . '] returns incorrect visibility ' .
61
                'of files on Windows OS. ' .
62
            'It is required to understand the situation'
63
        );
64
65
        $this->local->create('file.txt');
66
67
        $public = Visibility::VISIBILITY_PUBLIC;
68
        $private = Visibility::VISIBILITY_PRIVATE;
69
70
        $this->local->setVisibility('file.txt', $public);
71
        $this->assertSame($public, $this->local->getVisibility('file.txt'));
72
73
        $this->local->setVisibility('file.txt', $private);
74
        $this->assertSame($private, $this->local->getVisibility('file.txt'));
75
    }
76
77
    public function testCopyToSameStorage(): void
78
    {
79
        $content = \random_bytes(64);
80
        $this->local->write('source.txt', $content);
81
        $this->local->copy('source.txt', 'copy.txt');
82
83
        $this->assertTrue($this->local->exists('source.txt'));
84
        $this->assertSame($content, $this->local->getContents('source.txt'));
85
86
        $this->assertTrue($this->local->exists('copy.txt'));
87
        $this->assertSame($content, $this->local->getContents('copy.txt'));
88
89
        $this->cleanTempDirectory();
90
    }
91
92
    public function testCopyToAnotherStorage(): void
93
    {
94
        $content = \random_bytes(64);
95
        $this->local->write('source.txt', $content);
96
        $this->local->copy('source.txt', 'copy.txt', $this->second);
97
98
        $this->assertTrue($this->local->exists('source.txt'));
99
        $this->assertSame($content, $this->local->getContents('source.txt'));
100
        $this->assertFalse($this->local->exists('copy.txt'));
101
102
        $this->assertTrue($this->second->exists('copy.txt'));
103
        $this->assertSame($content, $this->second->getContents('copy.txt'));
104
        $this->assertFalse($this->second->exists('source.txt'));
105
106
        $this->cleanTempDirectory();
107
    }
108
109
    public function testMoveToSameStorage(): void
110
    {
111
        $content = \random_bytes(64);
112
        $this->local->write('source.txt', $content);
113
        $this->local->move('source.txt', 'moved.txt');
114
115
        $this->assertFalse($this->local->exists('source.txt'));
116
        $this->assertTrue($this->local->exists('moved.txt'));
117
        $this->assertSame($content, $this->local->getContents('moved.txt'));
118
119
        $this->cleanTempDirectory();
120
    }
121
122
    public function testMoveToAnotherStorage(): void
123
    {
124
        $content = \random_bytes(64);
125
        $this->local->write('source.txt', $content);
126
        $this->local->move('source.txt', 'moved.txt', $this->second);
127
128
        $this->assertFalse($this->local->exists('source.txt'));
129
        $this->assertFalse($this->local->exists('moved.txt'));
130
131
        $this->assertTrue($this->second->exists('moved.txt'));
132
        $this->assertSame($content, $this->second->getContents('moved.txt'));
133
        $this->assertFalse($this->second->exists('source.txt'));
134
135
        $this->cleanTempDirectory();
136
    }
137
138
    public function testDelete(): void
139
    {
140
        $this->local->create('file.txt');
141
        $this->assertTrue($this->local->exists('file.txt'));
142
143
        $this->local->delete('file.txt');
144
        $this->assertFalse($this->local->exists('file.txt'));
145
    }
146
147
    public function testReadingAsStream(): void
148
    {
149
        $content = \random_bytes(64);
150
        $this->local->write('file.txt', $content);
151
152
        $actual = '';
153
        $stream = $this->local->getStream('file.txt');
154
        while (!\feof($stream)) {
155
            $actual .= \fread($stream, 256);
156
        }
157
        \fclose($stream);
158
159
        $this->assertSame($actual, $content);
160
161
        $this->cleanTempDirectory();
162
    }
163
164
    public function testExisting(): void
165
    {
166
        $this->assertFalse($this->local->exists('file.txt'));
167
        $this->local->create('file.txt');
168
        $this->assertTrue($this->local->exists('file.txt'));
169
170
        $this->cleanTempDirectory();
171
    }
172
173
    /**
174
     * Note: This test may fail since it focuses on a mutable value
175
     */
176
    public function testLastModified(): void
177
    {
178
        $now = \time();
179
180
        $this->local->create('file.txt');
181
        $before = $this->local->getLastModified('file.txt');
182
        $this->assertGreaterThanOrEqual($now, $before);
183
184
        // Wait 1.1 seconds and then again modify file
185
        \usleep(1100000);
186
187
        $this->local->write('file.txt', 'content');
188
        $after = $this->local->getLastModified('file.txt');
189
        $this->assertGreaterThan($before, $after);
190
    }
191
192
    public function testSize(): void
193
    {
194
        $content = \random_bytes(\random_int(32, 256));
195
        $this->local->write('file.txt', $content);
196
197
        $this->assertSame(\strlen($content), $this->local->getSize('file.txt'));
198
    }
199
200
    /**
201
     * Note: Checking for all existing mime types is not the goal of this
202
     *       test; just need to check the readability of the mime type.
203
     */
204
    public function testMime(): void
205
    {
206
        $this->local->write('file.txt', 'content');
207
208
        $this->assertSame('text/plain', $this->local->getMimeType('file.txt'));
209
    }
210
}
211