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\Attribute\FileUpload; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Validator\Validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FileUploadAttributeMultipleFileTest |
17
|
|
|
* @package Tests\NilPortugues\Validator\Attribute\FileUploadAttribute |
18
|
|
|
*/ |
19
|
|
|
class FileUploadAttributeMultipleFileTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
protected function setUp() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$_FILES = [ |
27
|
|
|
'image' => [ |
28
|
|
|
'name' => ['sample.png', 'sample.png', 'sample.png'], |
29
|
|
|
'type' => ['image/png', 'image/png', 'image/png'], |
30
|
|
|
'tmp_name' => [ |
31
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
32
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
33
|
|
|
\realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', |
34
|
|
|
], |
35
|
|
|
'error' => [0, 0, 0], |
36
|
|
|
'size' => [203868, 203868, 203868], |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \NilPortugues\Validator\Attribute\FileUpload\FileUploadAttribute |
43
|
|
|
*/ |
44
|
|
|
private function getValidator() |
45
|
|
|
{ |
46
|
|
|
$validator = Validator::create(); |
47
|
|
|
|
48
|
|
|
return $validator->isFileUpload('image'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function itShouldCheckIfHasLength() |
55
|
|
|
{ |
56
|
|
|
$this->assertTrue($this->getValidator()->hasLength(3)->validate('image')); |
57
|
|
|
$this->assertFalse($this->getValidator()->hasLength(2)->validate('image')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function itShouldCheckIfIsBetween() |
64
|
|
|
{ |
65
|
|
|
$this->assertTrue($this->getValidator()->isBetween(0, 2, 'MB', true)->validate('image')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function itShouldCheckIfIsMimeType() |
72
|
|
|
{ |
73
|
|
|
$this->assertTrue( |
74
|
|
|
$this->getValidator() |
75
|
|
|
->isMimeType(['image/png', 'image/gif', 'image/jpg']) |
76
|
|
|
->validate('image') |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertFalse( |
80
|
|
|
$this->getValidator() |
81
|
|
|
->isMimeType(['image/bmp']) |
82
|
|
|
->validate('image') |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function itShouldCheckIfHasFileNameFormat() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$validator = Validator::create(); |
92
|
|
|
$stringValidator = $validator->isString('image')->isLowercase(); |
93
|
|
|
|
94
|
|
|
$this->assertTrue($this->getValidator()->hasFileNameFormat($stringValidator)->validate('image')); |
95
|
|
|
|
96
|
|
|
$stringValidator = $validator->isString('image')->isAlphanumeric(); |
97
|
|
|
$_FILES['image']['name'] = '@sample.png'; |
98
|
|
|
$this->assertFalse($this->getValidator()->hasFileNameFormat($stringValidator)->validate('image')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
*/ |
104
|
|
|
public function itShouldCheckIfHasValidUploadDirectory() |
105
|
|
|
{ |
106
|
|
|
$this->assertTrue( |
107
|
|
|
$this->getValidator() |
108
|
|
|
->hasValidUploadDirectory(\realpath(\dirname(__FILE__)).'/resources/') |
109
|
|
|
->validate('image') |
110
|
|
|
); |
111
|
|
|
$this->assertFalse( |
112
|
|
|
$this->getValidator() |
113
|
|
|
->hasValidUploadDirectory(\realpath(\dirname(__FILE__)).'/not/') |
114
|
|
|
->validate('image') |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @test |
120
|
|
|
*/ |
121
|
|
|
public function itShouldCheckIfNotOverwritingExistingFile() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$this->assertFalse( |
124
|
|
|
$this->getValidator() |
125
|
|
|
->notOverwritingExistingFile(\realpath(\dirname(__FILE__)).'/resources') |
126
|
|
|
->validate('image') |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$_FILES['image']['name'] = 'a.png'; |
130
|
|
|
$this->assertTrue( |
131
|
|
|
$this->getValidator() |
132
|
|
|
->notOverwritingExistingFile(\realpath(\dirname(__FILE__)).'/resources') |
133
|
|
|
->validate('image') |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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: