Passed
Pull Request — master (#653)
by Aleksei
07:43
created

FileTest::testSizeStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 69
rs 9.232
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Validation\Checkers;
13
14
use Spiral\Files\Files;
15
use Spiral\Files\FilesInterface;
16
use Spiral\Tests\Validation\BaseTest;
17
use Laminas\Diactoros\UploadedFile;
18
19
class FileTest extends BaseTest
20
{
21
    private $files;
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->files = new Files();
28
        $this->container->bind(FilesInterface::class, $this->files);
29
    }
30
31
    public function testExists(): void
32
    {
33
        $this->assertNotValid('a', [], [
34
            'a' => ['file:exists']
35
        ]);
36
37
        $this->assertNotValid('a', [
38
            'a' => null
39
        ], [
40
            'a' => ['file:exists']
41
        ]);
42
43
        $this->assertNotValid('a', [
44
            'a' => []
45
        ], [
46
            'a' => ['file:exists']
47
        ]);
48
49
        $this->assertValid([
50
            'a' => __FILE__
51
        ], [
52
            'a' => ['file:exists']
53
        ]);
54
    }
55
56
    public function testFakeUpload(): void
57
    {
58
        $this->assertValid([
59
            'a' => ['tmp_name' => __FILE__]
60
        ], [
61
            'a' => ['file:exists']
62
        ]);
63
64
        $this->assertNotValid('a', [
65
            'a' => ['tmp_name' => __FILE__]
66
        ], [
67
            'a' => ['file:uploaded']
68
        ]);
69
70
        $this->assertValid([
71
            'a' => ['tmp_name' => __FILE__, 'uploaded' => true]
72
        ], [
73
            'a' => ['file:uploaded']
74
        ]);
75
    }
76
77
    public function testExistsStream(): void
78
    {
79
        $uploaded = new UploadedFile(__FILE__, filesize(__FILE__), 0);
80
81
        $this->assertValid([
82
            'a' => $uploaded
83
        ], [
84
            'a' => ['file:exists']
85
        ]);
86
87
        $uploaded = new UploadedFile(__FILE__, filesize(__FILE__), 1);
88
89
        $this->assertNotValid('a', [
90
            'a' => $uploaded
91
        ], [
92
            'a' => ['file:exists']
93
        ]);
94
    }
95
96
    public function testUploaded(): void
97
    {
98
        $this->assertNotValid('a', [], [
99
            'a' => ['file:uploaded']
100
        ]);
101
102
        $this->assertNotValid('a', [
103
            'a' => null
104
        ], [
105
            'a' => ['file:uploaded']
106
        ]);
107
108
        $this->assertNotValid('a', [
109
            'a' => []
110
        ], [
111
            'a' => ['file:uploaded']
112
        ]);
113
114
        $this->assertNotValid('a', [
115
            'a' => __FILE__
116
        ], [
117
            'a' => ['file:uploaded']
118
        ]);
119
    }
120
121
    public function testUploadedSteam(): void
122
    {
123
        $uploaded = new UploadedFile(__FILE__, filesize(__FILE__), 0);
124
125
        $this->assertValid([
126
            'a' => $uploaded
127
        ], [
128
            'a' => ['file:uploaded']
129
        ]);
130
131
        $uploaded = new UploadedFile(__FILE__, filesize(__FILE__), 1);
132
133
        $this->assertNotValid('a', [
134
            'a' => $uploaded
135
        ], [
136
            'a' => ['file:uploaded']
137
        ]);
138
    }
139
140
    public function testSize(): void
141
    {
142
        $this->assertNotValid('a', [], [
143
            'a' => [
144
                'file:exists',
145
                ['file:size', 1] //1Kb
146
            ]
147
        ]);
148
149
        $tmpFile = $this->files->tempFilename();
150
        $this->files->write(
151
            $tmpFile,
152
            str_repeat('0', 1023)
153
        );
154
155
        clearstatcache();
156
        $this->assertValid([
157
            'a' => $tmpFile
158
        ], [
159
            'a' => [
160
                'file:exists',
161
                ['file:size', 1] //1Kb
162
            ]
163
        ]);
164
165
        $tmpFile = $this->files->tempFilename();
166
        $this->files->write(
167
            $tmpFile,
168
            str_repeat('0', 1024)
169
        );
170
171
        clearstatcache();
172
        $this->assertValid([
173
            'a' => $tmpFile
174
        ], [
175
            'a' => [
176
                'file:exists',
177
                ['file:size', 1] //1Kb
178
            ]
179
        ]);
180
181
        $tmpFile = $this->files->tempFilename();
182
        $this->files->write(
183
            $tmpFile,
184
            str_repeat('0', 1025)
185
        );
186
187
        clearstatcache();
188
        $this->assertNotValid('a', [
189
            'a' => $tmpFile
190
        ], [
191
            'a' => [
192
                'file:exists',
193
                ['file:size', 1] //1Kb
194
            ]
195
        ]);
196
    }
197
198
    public function testSizeStream(): void
199
    {
200
        $this->assertNotValid('a', [], [
201
            'a' => [
202
                'file:exists',
203
                ['file:size', 1] //1Kb
204
            ]
205
        ]);
206
207
        $tmpFile = $this->files->tempFilename();
208
        $this->files->write(
209
            $tmpFile,
210
            str_repeat('0', 1023)
211
        );
212
213
        clearstatcache();
214
        $this->assertValid([
215
            'a' => new UploadedFile($tmpFile, filesize($tmpFile), 0)
216
        ], [
217
            'a' => [
218
                'file:exists',
219
                ['file:size', 1] //1Kb
220
            ]
221
        ]);
222
223
        $tmpFile = $this->files->tempFilename();
224
        $this->files->write(
225
            $tmpFile,
226
            str_repeat('0', 1024)
227
        );
228
229
        clearstatcache();
230
        $this->assertValid([
231
            'a' => new UploadedFile($tmpFile, filesize($tmpFile), 0)
232
        ], [
233
            'a' => [
234
                'file:exists',
235
                ['file:size', 1] //1Kb
236
            ]
237
        ]);
238
239
        $tmpFile = $this->files->tempFilename();
240
        $this->files->write(
241
            $tmpFile,
242
            str_repeat('0', 1025)
243
        );
244
245
        clearstatcache();
246
        $this->assertNotValid('a', [
247
            'a' => new UploadedFile($tmpFile, filesize($tmpFile), 0)
248
        ], [
249
            'a' => [
250
                'file:exists',
251
                ['file:size', 1] //1Kb
252
            ]
253
        ]);
254
255
        $tmpFile = $this->files->tempFilename();
256
        $this->files->write(
257
            $tmpFile,
258
            str_repeat('0', 1023)
259
        );
260
261
        clearstatcache();
262
        $this->assertNotValid('a', [
263
            'a' => new UploadedFile($tmpFile, filesize($tmpFile), 1)
264
        ], [
265
            'a' => [
266
                ['file:size', 1] //1Kb
267
            ]
268
        ]);
269
    }
270
271
    public function testExtension(): void
272
    {
273
        $this->assertNotValid('a', [], [
274
            'a' => [
275
                'file:exists',
276
                ['file:extension', 1] //1Kb
277
            ]
278
        ]);
279
280
        $this->assertValid([
281
            'a' => __FILE__
282
        ], [
283
            'a' => [
284
                'file:exists',
285
                ['file:extension', 'php']
286
            ]
287
        ]);
288
289
        $this->assertNotValid('a', [
290
            'a' => __FILE__
291
        ], [
292
            'a' => [
293
                'file:exists',
294
                ['file:extension', 'jpg']
295
            ]
296
        ]);
297
    }
298
299
    public function testExtensionUploaded(): void
300
    {
301
        $this->assertNotValid('a', [], [
302
            'a' => [
303
                'file:exists',
304
                ['file:extension', 1] //1Kb
305
            ]
306
        ]);
307
308
        $uploaded = new UploadedFile(__FILE__, filesize(__FILE__), 0, 'file.php');
309
310
        $this->assertValid([
311
            'a' => $uploaded
312
        ], [
313
            'a' => [
314
                'file:exists',
315
                ['file:extension', 'php']
316
            ]
317
        ]);
318
319
        $this->assertNotValid('a', [
320
            'a' => $uploaded
321
        ], [
322
            'a' => [
323
                'file:exists',
324
                ['file:extension', 'jpg']
325
            ]
326
        ]);
327
    }
328
}
329