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