-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUploadedFileTest.php
More file actions
208 lines (177 loc) · 5.63 KB
/
UploadedFileTest.php
File metadata and controls
208 lines (177 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace Rakit\Validation\Tests\Rules;
use Rakit\Validation\Rules\UploadedFile;
use PHPUnit\Framework\TestCase;
class UploadedFileTest extends TestCase
{
public function setUp()
{
$this->rule = new UploadedFile();
}
public function testValidUploadedFile()
{
$file = [
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => filesize(__FILE__),
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK
];
$uploadedFileRule = $this->getMockBuilder(UploadedFile::class)
->setMethods(['isUploadedFile'])
->getMock();
$uploadedFileRule->expects($this->once())
->method('isUploadedFile')
->willReturn(true);
$this->assertTrue($uploadedFileRule->check($file));
}
/**
* Make sure we can't just passing array like valid $_FILES['key']
*/
public function testValidateWithoutMockShouldBeInvalid()
{
$this->assertFalse($this->rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => filesize(__FILE__),
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK
]));
}
/**
* Missing UPLOAD_ERR_NO_FILE should be valid because it is job for required rule
*/
public function testEmptyUploadedFileShouldBeValid()
{
$this->assertTrue($this->rule->check([
'name' => '',
'type' => '',
'size' => '',
'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE
]));
}
public function testUploadError()
{
$this->assertFalse($this->rule->check([
'name' => '',
'type' => '',
'size' => '',
'tmp_name' => '',
'error' => 5
]));
}
public function testMaxSize()
{
$rule = $this->getMockBuilder(UploadedFile::class)
->setMethods(['isUploadedFile'])
->getMock();
$rule->expects($this->exactly(2))
->method('isUploadedFile')
->willReturn(true);
$rule->maxSize("1MB");
$this->assertFalse($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => 1024 * 1024 * 1.1,
'tmp_name' => __FILE__,
'error' => 0
]));
$this->assertTrue($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => 1000000,
'tmp_name' => __FILE__,
'error' => 0
]));
}
public function testMinSize()
{
$rule = $this->getMockBuilder(UploadedFile::class)
->setMethods(['isUploadedFile'])
->getMock();
$rule->expects($this->exactly(2))
->method('isUploadedFile')
->willReturn(true);
$rule->minSize('10K');
$this->assertFalse($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => 1024, // 1K
'tmp_name' => __FILE__,
'error' => 0
]));
$this->assertTrue($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => 10 * 1024,
'tmp_name' => __FILE__,
'error' => 0
]));
}
public function testFileTypes()
{
$rule = $this->getMockBuilder(UploadedFile::class)
->setMethods(['isUploadedFile'])
->getMock();
$rule->expects($this->exactly(3))
->method('isUploadedFile')
->willReturn(true);
$rule->fileTypes('png|jpeg');
$this->assertFalse($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => 1024, // 1K
'tmp_name' => __FILE__,
'error' => 0
]));
$this->assertTrue($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'image/png',
'size' => 10 * 1024,
'tmp_name' => __FILE__,
'error' => 0
]));
$this->assertTrue($rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'image/jpeg',
'size' => 10 * 1024,
'tmp_name' => __FILE__,
'error' => 0
]));
}
/**
* Missing array key(s) should be valid because it is job for required rule
*/
public function testMissingAKeyShouldBeValid()
{
// missing name
$this->assertTrue($this->rule->check([
'type' => 'text/plain',
'size' => filesize(__FILE__),
'tmp_name' => __FILE__,
'error' => 0
]));
// missing type
$this->assertTrue($this->rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'size' => filesize(__FILE__),
'tmp_name' => __FILE__,
'error' => 0
]));
// missing size
$this->assertTrue($this->rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'tmp_name' => __FILE__,
'error' => 0
]));
// missing tmp_name
$this->assertTrue($this->rule->check([
'name' => pathinfo(__FILE__, PATHINFO_BASENAME),
'type' => 'text/plain',
'size' => filesize(__FILE__),
'error' => 0
]));
}
}