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

FileTestCase   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 115
c 1
b 0
f 0
dl 0
loc 238
rs 10
wmc 17

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testSize() 0 8 1
A testWriteString() 0 11 1
A testCopyToSameStorage() 0 15 1
A testCopyToAnotherStorage() 0 17 1
A testCreating() 0 16 1
A testMime() 0 7 1
A testReadingAsStream() 0 17 2
A testDelete() 0 10 1
A testMoveToSameStorage() 0 13 1
A testStorage() 0 5 1
A testMoveToAnotherStorage() 0 16 1
A testVisibility() 0 20 1
A testLastModified() 0 17 1
A testPathname() 0 5 1
A testExisting() 0 9 1
A testWriteStream() 0 13 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 Spiral\Storage\Visibility;
15
16
/**
17
 * @group unit
18
 */
19
class FileTestCase extends TestCase
20
{
21
    public function testPathname(): void
22
    {
23
        $file = $this->local->file('path/to/file.txt');
24
25
        $this->assertSame('path/to/file.txt', $file->getPathname());
26
    }
27
28
    public function testStorage(): void
29
    {
30
        $file = $this->local->file('path/to/file.txt');
31
32
        $this->assertSame($this->local, $file->getStorage());
33
    }
34
35
    public function testCreating(): void
36
    {
37
        $file = $this->local->file('path/to/file.txt');
38
        $this->assertFalse($file->exists());
39
40
        $file->create();
41
        $this->assertTrue($file->exists());
42
43
        $content = \random_bytes(64);
44
        $file->write($content);
45
46
        // execute "create" method again
47
        $this->assertSame($content, $file->getContents());
48
        $file->create();
49
        // content must not be changed
50
        $this->assertSame($content, $file->getContents());
51
    }
52
53
    public function testWriteString(): void
54
    {
55
        $content = \random_bytes(64);
56
57
        $file = $this->local->file('file.txt');
58
        $file->write($content);
59
60
        $this->assertTrue($file->exists());
61
        $this->assertSame($content, $file->getContents());
62
63
        $this->cleanTempDirectory();
64
    }
65
66
    public function testWriteStream(): void
67
    {
68
        $content = \random_bytes(64);
69
        $stream = \fopen('php://memory', 'ab+');
70
        \fwrite($stream, $content);
71
72
        $file = $this->local->file('file.txt');
73
        $file->write($content);
74
75
        $this->assertTrue($file->exists());
76
        $this->assertSame($content, $file->getContents());
77
78
        $this->cleanTempDirectory();
79
    }
80
81
    public function testVisibility(): void
82
    {
83
        $this->markTestSkipped(
84
            'This test [' . __FUNCTION__ . '] returns incorrect visibility ' .
85
                'of files on Windows OS. ' .
86
            'It is required to understand the situation'
87
        );
88
89
        $file = $this->local->file('file.txt')
90
            ->create()
91
        ;
92
93
        $public = Visibility::VISIBILITY_PUBLIC;
94
        $private = Visibility::VISIBILITY_PRIVATE;
95
96
        $file->setVisibility($public);
97
        $this->assertSame($public, $file->getVisibility());
98
99
        $file->setVisibility($private);
100
        $this->assertSame($private, $file->getVisibility());
101
    }
102
103
    public function testCopyToSameStorage(): void
104
    {
105
        $content = \random_bytes(64);
106
        $source = $this->local->file('file.txt');
107
        $source->write($content);
108
109
        $copy = $source->copy('copy.txt');
110
111
        $this->assertTrue($source->exists());
112
        $this->assertSame($content, $source->getContents());
113
114
        $this->assertTrue($copy->exists());
115
        $this->assertSame($content, $copy->getContents());
116
117
        $this->cleanTempDirectory();
118
    }
119
120
    public function testCopyToAnotherStorage(): void
121
    {
122
        $content = \random_bytes(64);
123
        $source = $this->local->file('source.txt');
124
        $source->write($content);
125
126
        $copy = $source->copy('copy.txt', $this->second);
127
128
        $this->assertTrue($source->exists());
129
        $this->assertSame($content, $source->getContents());
130
        $this->assertFalse($this->local->exists('copy.txt'));
131
132
        $this->assertTrue($copy->exists());
133
        $this->assertSame($content, $copy->getContents());
134
        $this->assertFalse($this->second->exists('source.txt'));
135
136
        $this->cleanTempDirectory();
137
    }
138
139
    public function testMoveToSameStorage(): void
140
    {
141
        $content = \random_bytes(64);
142
        $source = $this->local->file('file.txt');
143
        $source->write($content);
144
145
        $moved = $source->move('moved.txt');
146
147
        $this->assertFalse($source->exists());
148
        $this->assertTrue($moved->exists());
149
        $this->assertSame($content, $moved->getContents());
150
151
        $this->cleanTempDirectory();
152
    }
153
154
    public function testMoveToAnotherStorage(): void
155
    {
156
        $content = \random_bytes(64);
157
        $source = $this->local->file('source.txt');
158
        $source->write($content);
159
160
        $moved = $source->move('moved.txt', $this->second);
161
162
        $this->assertFalse($source->exists());
163
        $this->assertTrue($moved->exists());
164
        $this->assertSame($content, $moved->getContents());
165
166
        $this->assertFalse($this->local->exists('moved.txt'));
167
        $this->assertFalse($this->second->exists('source.txt'));
168
169
        $this->cleanTempDirectory();
170
    }
171
172
    public function testDelete(): void
173
    {
174
        $source = $this->local->file('file.txt');
175
        $this->assertFalse($source->exists());
176
177
        $source->create();
178
        $this->assertTrue($source->exists());
179
180
        $source->delete();
181
        $this->assertFalse($source->exists());
182
    }
183
184
    public function testReadingAsStream(): void
185
    {
186
        $content = \random_bytes(64);
187
        $source = $this->local->file('file.txt')
188
            ->write($content)
189
        ;
190
191
        $actual = '';
192
        $stream = $source->getStream();
193
        while (!\feof($stream)) {
194
            $actual .= \fread($stream, 256);
195
        }
196
        \fclose($stream);
197
198
        $this->assertSame($actual, $content);
199
200
        $this->cleanTempDirectory();
201
    }
202
203
    public function testExisting(): void
204
    {
205
        $file = $this->local->file('file.txt');
206
207
        $this->assertFalse($file->exists());
208
        $this->local->create('file.txt');
209
        $this->assertTrue($file->exists());
210
211
        $this->cleanTempDirectory();
212
    }
213
214
    /**
215
     * Note: This test may fail since it focuses on a mutable value
216
     */
217
    public function testLastModified(): void
218
    {
219
        $now = \time();
220
221
        $file = $this->local->file('file.txt')
222
            ->create()
223
        ;
224
225
        $before = $file->getLastModified();
226
        $this->assertGreaterThanOrEqual($now, $before);
227
228
        // Wait 1.1 seconds and then again modify file
229
        \usleep(1100000);
230
231
        $file->write('content');
232
        $after = $file->getLastModified();
233
        $this->assertGreaterThan($before, $after);
234
    }
235
236
    public function testSize(): void
237
    {
238
        $content = \random_bytes(\random_int(32, 256));
239
        $file = $this->local->file('file.txt')
240
            ->write($content)
241
        ;
242
243
        $this->assertSame(\strlen($content), $file->getSize());
244
    }
245
246
    /**
247
     * Note: Checking for all existing mime types is not the goal of this
248
     *       test; just need to check the readability of the mime type.
249
     */
250
    public function testMime(): void
251
    {
252
        $file = $this->local->file('file.txt')
253
            ->write('content')
254
        ;
255
256
        $this->assertSame('text/plain', $file->getMimeType());
257
    }
258
}
259