Test Failed
Push — master ( b976f5...8958fb )
by Alex
02:01
created

SecurityRulesUnitTest::getFileValueProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 10
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
     * Method returns path to storage
16
     *
17
     * @return string path to storage
18
     */
19
    protected function getPathToStorage(): string
20
    {
21
        return SecurityRulesUnitTest::PATH_TO_FILE_STORAGE . date('Y/m/d/');
22
    }
23
24
    /**
25
     * Testing edge cases of getFileValue
26
     */
27
    public function testGetEmptyFileValue(): void
28
    {
29
        // setup
30
        $_FILES = [
31
            'test-file' => [
32
                'size' => 0
33
            ]
34
        ];
35
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
36
            ->setMethods([
37
            '_prepareFs',
38
            'filePutContents',
39
            'moveUploadedFile'
40
        ])
41
            ->setConstructorArgs([])
42
            ->getMock();
43
44
        // test body
45
        $result = $securityRules->getFileValue('test-file', false);
46
47
        // assertions
48
        $this->assertEquals('', $result);
49
    }
50
51
    /**
52
     * Method constructs element of the $_FILES array
53
     *
54
     * @param int $size
55
     *            size of the file
56
     * @param string $file
57
     *            file path
58
     * @param string $name
59
     *            file name
60
     * @return array element of the $_FILES array
61
     */
62
    protected function constructUploadedFile(int $size, string $file, string $name, string $tmpName = ''): array
63
    {
64
        $return = [
65
            'size' => $size,
66
            'name' => $name
67
        ];
68
69
        if ($file !== '') {
70
            $return['file'] = $file;
71
        }
72
73
        if ($tmpName !== '') {
74
            $return['tmp_name'] = $tmpName;
75
        }
76
77
        return $return;
78
    }
79
80
    /**
81
     * Data provider for the testGetFileValue test
82
     *
83
     * @return array data for test testGetFileValue
84
     */
85
    public function getFileValueProvider(): array
86
    {
87
        return [
88
            [
89
                true,
90
                [
91
                    'test-file' => $this->constructUploadedFile(2000, '1', '1')
92
                ]
93
            ],
94
            [
95
                false,
96
                [
97
                    'test-file' => $this->constructUploadedFile(1, '1', '1')
98
                ]
99
            ],
100
            [
101
                true,
102
                [
103
                    'test-file' => $this->constructUploadedFile(1, '', '1', '1')
104
                ]
105
            ]
106
        ];
107
    }
108
109
    /**
110
     * Testing edge cases of getFileValue
111
     *
112
     * @param bool $storeFile
113
     *            do we need to store file
114
     * @param array $files
115
     *            file ddescription
116
     * @dataProvider getFileValueProvider
117
     */
118
    public function testGetFileValue(bool $storeFile, array $files): void
119
    {
120
        // setup
121
        $_FILES = $files;
122
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
123
            ->setMethods([
124
            '_prepareFs',
125
            'filePutContents',
126
            'moveUploadedFile'
127
        ])
128
            ->setConstructorArgs([])
129
            ->getMock();
130
131
        if ($storeFile) {
132
            if (isset($files['test-file']['tmp_name'])) {
133
                $securityRules->expects($this->once())
134
                    ->method('moveUploadedFile');
135
            } else {
136
                $securityRules->expects($this->once())
137
                    ->method('filePutContents');
138
            }
139
        }
140
141
        // test body
142
        $result = $securityRules->getFileValue('test-file', $storeFile);
143
144
        // assertions
145
        if ($storeFile) {
146
            $this->assertStringContainsString($this->getPathToStorage(), $result);
147
        } else {
148
            $this->assertEquals(1, $result['size']);
149
150
            $this->assertEquals('1', $result['name']);
151
            if (isset($files['test-file']['tmp_name'])) {
152
                $this->assertEquals('1', $result['tmp_name']);
153
            } else {
154
                $this->assertEquals('1', $result['file']);
155
            }
156
        }
157
    }
158
159
    /**
160
     * Data provider for the testStoreFileContent
161
     *
162
     * @return array data for the testStoreFileContent test
163
     */
164
    public function storeFileContentProvider(): array
165
    {
166
        return [
167
            [
168
                true
169
            ],
170
            [
171
                false
172
            ]
173
        ];
174
    }
175
176
    /**
177
     * Testing storeFileContent method
178
     *
179
     * @dataProvider storeFileContentProvider
180
     */
181
    public function testStoreFileContent(bool $decoded): void
182
    {
183
        // setup
184
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
185
            ->setMethods([
186
            '_prepareFs',
187
            'filePutContents',
188
            'moveUploadedFile'
189
        ])
190
            ->setConstructorArgs([])
191
            ->getMock();
192
        $securityRules->method('_prepareFs')->willReturn('prepared');
193
        $securityRules->expects($this->once())
194
            ->method('filePutContents');
195
196
        // test body
197
        $result = $securityRules->storeFileContent('content', 'file-prefix', $decoded);
198
199
        // assertions
200
        $this->assertStringContainsString($this->getPathToStorage(), $result);
201
    }
202
203
    /**
204
     * Mock creation function
205
     *
206
     * @param mixed $returnValue
207
     *            return value of the fileGetContents method
208
     * @return object mock
209
     */
210
    protected function getStoreFileMock($returnValue): object
211
    {
212
        $securityRules = $this->getMockBuilder(\Mezon\Security\SecurityRules::class)
213
            ->setMethods([
214
            'fileGetContents',
215
            '_prepareFs',
216
            'filePutContents',
217
        ])
218
            ->setConstructorArgs([])
219
            ->getMock();
220
        $securityRules->expects($this->once())
221
            ->method('fileGetContents')
222
            ->willReturn($returnValue);
223
        $securityRules->method('_prepareFs')->willReturn('prepared');
224
225
        return $securityRules;
226
    }
227
228
    /**
229
     * Testing 'storeFile' method
230
     */
231
    public function testStoreFile(): void
232
    {
233
        // setup
234
        $securityRules = $this->getStoreFileMock('content');
235
236
        // test body
237
        $result = $securityRules->storeFile('c://file', 'prefix');
238
239
        // assertions
240
        $this->assertStringContainsString($this->getPathToStorage(), $result);
241
    }
242
243
    /**
244
     * Testing 'storeFile' method for unexisting file
245
     */
246
    public function testStoreUnexistingFile(): void
247
    {
248
        // setup
249
        $securityRules = $this->getStoreFileMock(false);
250
251
        // test body
252
        $result = $securityRules->storeFile('c://file', 'prefix');
253
254
        // assertions
255
        $this->assertNull($result);
256
    }
257
258
    /**
259
     * Data provider for the test testIsUploadedFileValid
260
     *
261
     * @return array testing data
262
     */
263
    public function isUploadFileValidProvider(): array
264
    {
265
        return [
266
            [
267
                $this->constructUploadedFile(2000, '1', '1'),
268
                [],
269
                true
270
            ],
271
            [
272
                $this->constructUploadedFile(2000, '1', '1'),
273
                [
274
                    new \Mezon\Security\Validators\File\Size(2000)
275
                ],
276
                true
277
            ],
278
            [
279
                $this->constructUploadedFile(2000, '1', '1'),
280
                [
281
                    new \Mezon\Security\Validators\File\Size(1500)
282
                ],
283
                false
284
            ]
285
        ];
286
    }
287
288
    /**
289
     * Testing that uploaded file is valid
290
     *
291
     * @param array $file
292
     *            uploaded file
293
     * @param array $validators
294
     *            validators
295
     * @param bool $requiredResult
296
     *            required result
297
     * @dataProvider isUPloadFileValidProvider
298
     */
299
    public function testIsUploadedFileValid(array $file, array $validators, bool $requiredResult): void
300
    {
301
        // setup
302
        $security = new \Mezon\Security\SecurityRules();
303
        $_FILES['is-valid-file'] = $file;
304
305
        // test body
306
        $result = $security->isUploadedFileValid('is-valid-file', $validators);
307
308
        // assertions
309
        $this->assertEquals($requiredResult, $result);
310
    }
311
312
    /**
313
     * Trying to validate size of the unexisting file
314
     */
315
    public function testValidatingSizeOfUnexistingFile(): void
316
    {
317
        // assertions
318
        $this->expectException(\Exception::class);
319
320
        // setup
321
        $security = new \Mezon\Security\SecurityRules();
322
        $validators = [
323
            new \Mezon\Security\Validators\File\Size(2000)
324
        ];
325
326
        // test body
327
        $security->isUploadedFileValid('unexisting-file', $validators);
328
    }
329
}
330