Test Failed
Push — master ( 0e97a6...1bbd27 )
by Alex
01:54
created

SecurityRulesUnitTest::testGetIntValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Mezon\Security\Tests;
3
4
class SecurityRulesUnitTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
{
6
7
    /**
8
     * Path to the directory where all files are stored
9
     *
10
     * @var string
11
     */
12
    public const PATH_TO_FILE_STORAGE = '/data/files/';
13
14
    /**
15
     * Field name of the testing file
16
     *
17
     * @var string
18
     */
19
    public const TEST_FILE_FIELD_NAME = 'test-file';
20
21
    /**
22
     * Testing image
23
     *
24
     * @var string
25
     */
26
    public const TEST_PNG_IMAGE_PATH = __DIR__ . '/res/test.png';
27
28
    /**
29
     * List of methods wich provides file system access and need to be mocked
30
     *
31
     * @var array
32
     */
33
    public const FILE_SYSTEM_ACCESS_METHODS = [
34
        '_prepareFs',
35
        'filePutContents',
36
        'moveUploadedFile',
37
        'fileGetContents'
38
    ];
39
40
    /**
41
     * Method returns path to storage
42
     *
43
     * @return string path to storage
44
     */
45
    protected function getPathToStorage(): string
46
    {
47
        return SecurityRulesUnitTest::PATH_TO_FILE_STORAGE . date('Y/m/d/');
48
    }
49
50
    /**
51
     * Testing edge cases of getFileValue
52
     */
53
    public function testGetEmptyFileValue(): void
54
    {
55
        // setup
56
        $_FILES = [
57
            'empty-file' => [
58
                'size' => 0
59
            ]
60
        ];
61
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
62
            ->setMethods(SecurityRulesUnitTest::FILE_SYSTEM_ACCESS_METHODS)
63
            ->setConstructorArgs([])
64
            ->getMock();
65
66
        // test body
67
        $result = $securityRules->getFileValue('empty-file', false);
68
69
        // assertions
70
        $this->assertEquals('', $result);
71
    }
72
73
    /**
74
     * Method constructs element of the $_FILES array
75
     *
76
     * @param int $size
77
     *            size of the file
78
     * @param string $file
79
     *            file path
80
     * @param string $name
81
     *            file name
82
     * @return array element of the $_FILES array
83
     */
84
    protected function constructUploadedFile(int $size, string $file, string $name, string $tmpName = ''): array
85
    {
86
        $return = [
87
            'size' => $size,
88
            'name' => $name
89
        ];
90
91
        if ($file !== '') {
92
            $return['file'] = $file;
93
        }
94
95
        if ($tmpName !== '') {
96
            $return['tmp_' . 'name'] = $tmpName;
97
        }
98
99
        return $return;
100
    }
101
102
    /**
103
     * Method constructs $_FILES array with one element
104
     *
105
     * @param int $size
106
     *            size of the file
107
     * @param string $file
108
     *            file path
109
     * @param string $name
110
     *            file name
111
     * @return array element of the $_FILES array
112
     */
113
    protected function constructTestFiles(int $size, string $file, string $name, string $tmpName = ''): array
114
    {
115
        return [
116
            SecurityRulesUnitTest::TEST_FILE_FIELD_NAME => $this->constructUploadedFile($size, $file, $name, $tmpName)
117
        ];
118
    }
119
120
    /**
121
     * Data provider for the testGetFileValue test
122
     *
123
     * @return array data for test testGetFileValue
124
     */
125
    public function getFileValueProvider(): array
126
    {
127
        return [
128
            [
129
                true,
130
                $this->constructTestFiles(2000, '1', '1')
131
            ],
132
            [
133
                false,
134
                $this->constructTestFiles(1, '1', '1')
135
            ],
136
            [
137
                true,
138
                $this->constructTestFiles(1, '', '1', '1')
139
            ]
140
        ];
141
    }
142
143
    /**
144
     * Method returns true if the field tmp_name is set
145
     *
146
     * @param array $file
147
     *            validating file description
148
     * @return bool true if the field tmp_name is set, false otherwise
149
     */
150
    protected function tmpNameSet(array $file): bool
151
    {
152
        return isset($file['tmp_name']);
153
    }
154
155
    /**
156
     * Testing edge cases of getFileValue
157
     *
158
     * @param bool $storeFile
159
     *            do we need to store file
160
     * @param array $files
161
     *            file ddescription
162
     * @dataProvider getFileValueProvider
163
     */
164
    public function testGetFileValue(bool $storeFile, array $files): void
165
    {
166
        // setup
167
        $_FILES = $files;
168
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
169
            ->setMethods(SecurityRulesUnitTest::FILE_SYSTEM_ACCESS_METHODS)
170
            ->setConstructorArgs([])
171
            ->getMock();
172
173
        if ($storeFile) {
174
            if ($this->tmpNameSet($files[SecurityRulesUnitTest::TEST_FILE_FIELD_NAME])) {
175
                $securityRules->expects($this->once())
176
                    ->method('moveUploadedFile');
177
            } else {
178
                $securityRules->expects($this->once())
179
                    ->method('filePutContents');
180
            }
181
        }
182
183
        // test body
184
        $result = $securityRules->getFileValue(SecurityRulesUnitTest::TEST_FILE_FIELD_NAME, $storeFile);
185
186
        // assertions
187
        if ($storeFile) {
188
            $this->assertStringContainsString($this->getPathToStorage(), $result);
189
        } else {
190
            $this->assertEquals(1, $result['size']);
191
192
            $this->assertEquals('1', $result['name']);
193
            if ($this->tmpNameSet($files[SecurityRulesUnitTest::TEST_FILE_FIELD_NAME])) {
194
                $this->assertEquals('1', $result['tmp_name']);
195
            } else {
196
                $this->assertEquals('1', $result['file']);
197
            }
198
        }
199
    }
200
201
    /**
202
     * Data provider for the testStoreFileContent
203
     *
204
     * @return array data for the testStoreFileContent test
205
     */
206
    public function storeFileContentProvider(): array
207
    {
208
        return [
209
            [
210
                true
211
            ],
212
            [
213
                false
214
            ]
215
        ];
216
    }
217
218
    /**
219
     * Testing storeFileContent method
220
     *
221
     * @dataProvider storeFileContentProvider
222
     */
223
    public function testStoreFileContent(bool $decoded): void
224
    {
225
        // setup
226
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
227
            ->setMethods(SecurityRulesUnitTest::FILE_SYSTEM_ACCESS_METHODS)
228
            ->setConstructorArgs([])
229
            ->getMock();
230
        $securityRules->method('_prepareFs')->willReturn('prepared');
231
        $securityRules->expects($this->once())
232
            ->method('filePutContents');
233
234
        // test body
235
        $result = $securityRules->storeFileContent('content', 'file-prefix', $decoded);
236
237
        // assertions
238
        $this->assertStringContainsString($this->getPathToStorage(), $result);
239
    }
240
241
    /**
242
     * Mock creation function
243
     *
244
     * @param mixed $returnValue
245
     *            return value of the fileGetContents method
246
     * @return object mock
247
     */
248
    protected function getStoreFileMock($returnValue): object
249
    {
250
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
251
            ->setMethods(SecurityRulesUnitTest::FILE_SYSTEM_ACCESS_METHODS)
252
            ->setConstructorArgs([])
253
            ->getMock();
254
        $securityRules->expects($this->once())
255
            ->method('fileGetContents')
256
            ->willReturn($returnValue);
257
        $securityRules->method('_prepareFs')->willReturn('prepared');
258
259
        return $securityRules;
260
    }
261
262
    /**
263
     * Testing 'storeFile' method
264
     */
265
    public function testStoreFile(): void
266
    {
267
        // setup
268
        $securityRules = $this->getStoreFileMock('content');
269
270
        // test body
271
        $result = $securityRules->storeFile('c://file', 'prefix');
272
273
        // assertions
274
        $this->assertStringContainsString($this->getPathToStorage(), $result);
275
    }
276
277
    /**
278
     * Testing 'storeFile' method for unexisting file
279
     */
280
    public function testStoreUnexistingFile(): void
281
    {
282
        // setup
283
        $securityRules = $this->getStoreFileMock(false);
284
285
        // test body
286
        $result = $securityRules->storeFile('c://file', 'prefix');
287
288
        // assertions
289
        $this->assertNull($result);
290
    }
291
292
    /**
293
     * Data provider for the test testIsUploadedFileValid
294
     *
295
     * @return array testing data
296
     */
297
    public function isUploadFileValidProvider(): array
298
    {
299
        return [
300
            [
301
                $this->constructUploadedFile(2000, '1', '1'),
302
                [],
303
                true
304
            ],
305
            [
306
                $this->constructUploadedFile(2000, '1', '1'),
307
                [
308
                    new \Mezon\Security\Validators\File\Size(2000)
309
                ],
310
                true
311
            ],
312
            [
313
                $this->constructUploadedFile(2000, '1', '1'),
314
                [
315
                    new \Mezon\Security\Validators\File\Size(1500)
316
                ],
317
                false
318
            ],
319
            [
320
                $this->constructUploadedFile(2000, '1', '1', __DIR__ . '/SecurityRulesUnitTest.php'),
321
                [
322
                    new \Mezon\Security\Validators\File\MimeType([
323
                        'text/x-php'
324
                    ])
325
                ],
326
                true
327
            ],
328
            [
329
                $this->constructUploadedFile(2000, '1', '1', __DIR__ . '/SecurityRulesUnitTest.php'),
330
                [
331
                    new \Mezon\Security\Validators\File\MimeType([
332
                        'image/png'
333
                    ])
334
                ],
335
                false
336
            ],
337
            [
338
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
339
                [
340
                    new \Mezon\Security\Validators\File\MimeType([
341
                        'image/png'
342
                    ])
343
                ],
344
                true
345
            ],
346
            [
347
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
348
                [
349
                    new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 500)
350
                ],
351
                false
352
            ],
353
            [
354
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
355
                [
356
                    new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 600)
357
                ],
358
                false
359
            ],
360
            [
361
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
362
                [
363
                    new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(500, 500)
364
                ],
365
                false
366
            ],
367
            [
368
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
369
                [
370
                    new \Mezon\Security\Validators\File\ImageMaximumWidthHeight(600, 600)
371
                ],
372
                true
373
            ],
374
            [
375
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
376
                [
377
                    new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(500, 500)
378
                ],
379
                true
380
            ],
381
            [
382
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
383
                [
384
                    new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(600, 500)
385
                ],
386
                false
387
            ],
388
            [
389
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
390
                [
391
                    new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(500, 600)
392
                ],
393
                false
394
            ],
395
            [
396
                $this->constructUploadedFile(6912, '1', '1', SecurityRulesUnitTest::TEST_PNG_IMAGE_PATH),
397
                [
398
                    new \Mezon\Security\Validators\File\ImageMinimumWidthHeight(600, 600)
399
                ],
400
                false
401
            ]
402
        ];
403
    }
404
405
    /**
406
     * Testing that uploaded file is valid
407
     *
408
     * @param array $file
409
     *            uploaded file
410
     * @param array $validators
411
     *            validators
412
     * @param bool $requiredResult
413
     *            required result
414
     * @dataProvider isUploadFileValidProvider
415
     */
416
    public function testIsUploadedFileValid(array $file, array $validators, bool $requiredResult): void
417
    {
418
        // setup
419
        $security = new \Mezon\Security\SecurityRules();
420
        $_FILES['is-valid-file'] = $file;
421
422
        // test body
423
        $result = $security->isUploadedFileValid('is-valid-file', $validators);
424
425
        // assertions
426
        $this->assertEquals($requiredResult, $result);
427
    }
428
429
    /**
430
     * Data provider for the test testValidatingUnexistingFile
431
     *
432
     * @return array testing data
433
     */
434
    public function validatingUnexistingFileProvider(): array
435
    {
436
        return [
437
            [
438
                new \Mezon\Security\Validators\File\Size(2000)
439
            ],
440
            [
441
                new \Mezon\Security\Validators\File\MimeType([
442
                    'image/jpeg'
443
                ])
444
            ]
445
        ];
446
    }
447
448
    /**
449
     * Trying to validate size of the unexisting file
450
     *
451
     * @param object $validator
452
     *            validator
453
     * @dataProvider validatingUnexistingFileProvider
454
     */
455
    public function testValidatingUnexistingFile(object $validator): void
456
    {
457
        // assertions
458
        $this->expectException(\Exception::class);
459
460
        // setup
461
        $security = new \Mezon\Security\SecurityRules();
462
463
        // test body
464
        $security->isUploadedFileValid('unexisting-file', [
465
            $validator
466
        ]);
467
    }
468
469
    /**
470
     * Testing getIntValue method
471
     */
472
    public function testGetIntValue(): void
473
    {
474
        // setup
475
        $security = new \Mezon\Security\SecurityRules();
476
477
        // test body and assertions
478
        $this->assertEquals(1, $security->getIntValue('1'));
479
        $this->assertEquals(2, $security->getIntValue(2));
480
        $this->assertEquals(0, $security->getIntValue('abc'));
481
        $this->assertEquals(1, $security->getIntValue(1.1));
482
    }
483
484
    /**
485
     * Testing getStringValue
486
     */
487
    public function testGetStringValue(): void
488
    {
489
        // setup
490
        $security = new \Mezon\Security\SecurityRules();
491
492
        // test body and assertions
493
        $this->assertEquals('1', $security->getStringValue('1'));
494
        $this->assertEquals('1', $security->getStringValue(1));
495
        $this->assertEquals('2.1', $security->getStringValue(2.1));
496
        $this->assertEquals('&amp;', $security->getStringValue('&'));
497
        $this->assertEquals('&lt;', $security->getStringValue('<'));
498
        $this->assertEquals('&gt;', $security->getStringValue('>'));
499
    }
500
}
501