|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/24/14 |
|
5
|
|
|
* Time: 5:01 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Tests\NilPortugues\Validator\Validation\FileUpload; |
|
12
|
|
|
|
|
13
|
|
|
use NilPortugues\Validator\Validation\FileUpload\FileUploadValidation; |
|
14
|
|
|
use NilPortugues\Validator\Validator; |
|
15
|
|
|
|
|
16
|
|
|
class FileUploadValidatorMultipleFileTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function setUp() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$_FILES = [ |
|
24
|
|
|
'image' => [ |
|
25
|
|
|
'name' => ['sample.png', 'sample.png', 'sample.png'], |
|
26
|
|
|
'type' => ['image/png', 'image/png', 'image/png'], |
|
27
|
|
|
'tmp_name' => [ |
|
28
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
|
29
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
|
30
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
|
31
|
|
|
], |
|
32
|
|
|
'error' => [], |
|
33
|
|
|
'size' => [203868, 203868, 203868], |
|
34
|
|
|
], |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function itShouldCheckIfHasLength() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->assertTrue(FileUploadValidation::hasLength('image', 3)); |
|
44
|
|
|
$this->assertFalse(FileUploadValidation::hasLength('image', 2)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
*/ |
|
50
|
|
|
public function itShouldCheckIfIsBetween() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertTrue(FileUploadValidation::isBetween('image', 0, 2, 'MB', true)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @test |
|
57
|
|
|
*/ |
|
58
|
|
|
public function itShouldCheckIfIsMimeType() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->assertTrue(FileUploadValidation::isMimeType('image', ['image/png', 'image/gif', 'image/jpg'])); |
|
61
|
|
|
$this->assertFalse(FileUploadValidation::isMimeType('image', ['image/bmp'])); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @test |
|
66
|
|
|
*/ |
|
67
|
|
|
public function itShouldCheckIfHasFileNameFormat() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$validator = Validator::create(); |
|
70
|
|
|
$stringValidator = $validator->isString('image')->isLowercase(); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertTrue(FileUploadValidation::hasFileNameFormat('image', $stringValidator)); |
|
73
|
|
|
|
|
74
|
|
|
$stringValidator = $validator->isString('image')->isAlphanumeric(); |
|
75
|
|
|
$_FILES['image']['name'] = '@sample.png'; |
|
76
|
|
|
$this->assertFalse(FileUploadValidation::hasFileNameFormat('image', $stringValidator)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function itShouldCheckIfHasValidUploadDirectory() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertTrue( |
|
85
|
|
|
FileUploadValidation::hasValidUploadDirectory('image', \realpath(\dirname(__FILE__)).'/resources/') |
|
86
|
|
|
); |
|
87
|
|
|
$this->assertFalse(FileUploadValidation::hasValidUploadDirectory('image', \realpath(\dirname(__FILE__)).'/not/')); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @test |
|
92
|
|
|
*/ |
|
93
|
|
|
public function itShouldCheckIfNotOverwritingExistingFile() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$this->assertFalse( |
|
96
|
|
|
FileUploadValidation::notOverwritingExistingFile('image', \realpath(\dirname(__FILE__)).'/resources') |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$_FILES['image']['name'] = 'a.png'; |
|
100
|
|
|
$this->assertTrue( |
|
101
|
|
|
FileUploadValidation::notOverwritingExistingFile('image', \realpath(\dirname(__FILE__)).'/resources') |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: