1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Rules\File; |
4
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class FileRulesTest extends CommonTestClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @throws RuleException |
11
|
|
|
*/ |
12
|
|
|
public function testFileExists(): void |
13
|
|
|
{ |
14
|
|
|
$data = new File\FileExists(); |
15
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
16
|
|
|
$data->validate($this->getMockFile()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @throws RuleException |
21
|
|
|
*/ |
22
|
|
|
public function testFileNotExists(): void |
23
|
|
|
{ |
24
|
|
|
$data = new File\FileExists(); |
25
|
|
|
$this->expectException(RuleException::class); |
26
|
|
|
$data->validate($this->getMockNoFile()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws RuleException |
31
|
|
|
*/ |
32
|
|
|
public function testFileSent(): void |
33
|
|
|
{ |
34
|
|
|
$data = new File\FileSent(); |
35
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
36
|
|
|
$data->validate($this->getMockFile()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws RuleException |
41
|
|
|
*/ |
42
|
|
|
public function testFileNotSent(): void |
43
|
|
|
{ |
44
|
|
|
$data = new File\FileSent(); |
45
|
|
|
$this->expectException(RuleException::class); |
46
|
|
|
$data->validate($this->getMockNoFile()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws RuleException |
51
|
|
|
*/ |
52
|
|
|
public function testFileReceived(): void |
53
|
|
|
{ |
54
|
|
|
$data = new File\FileReceived(); |
55
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
56
|
|
|
$data->validate($this->getMockFile()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws RuleException |
61
|
|
|
*/ |
62
|
|
|
public function testFileNotReceived(): void |
63
|
|
|
{ |
64
|
|
|
$data = new File\FileReceived(); |
65
|
|
|
$this->expectException(RuleException::class); |
66
|
|
|
$data->validate($this->getMockNoFile()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $maxSize |
71
|
|
|
* @param int $fileSize |
72
|
|
|
* @param bool $match |
73
|
|
|
* @throws RuleException |
74
|
|
|
* @dataProvider sizeMatchProvider |
75
|
|
|
*/ |
76
|
|
|
public function testFileMaxSize(string $maxSize, int $fileSize, bool $match): void |
77
|
|
|
{ |
78
|
|
|
$data = new File\FileMaxSize(); |
79
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
80
|
|
|
$data->setAgainstValue($maxSize); |
81
|
|
|
$mock = MockFile::init('foo', 'text0.txt', 'text/plain', |
82
|
|
|
'', $fileSize, UPLOAD_ERR_OK ); |
83
|
|
|
if (!$match) $this->expectException(RuleException::class); |
84
|
|
|
$data->validate($mock); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function sizeMatchProvider(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['32', 128, false], |
91
|
|
|
['10g', 46843, true], |
92
|
|
|
['15m', 84641, true], |
93
|
|
|
['30k', 3534, true], |
94
|
|
|
['30k', 35534, false], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws RuleException |
100
|
|
|
*/ |
101
|
|
|
public function testFileMimeEquals(): void |
102
|
|
|
{ |
103
|
|
|
$data = new File\FileMimeEquals(); |
104
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
105
|
|
|
$data->setAgainstValue('text/plain'); |
106
|
|
|
$data->validate($this->getMockFile()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @throws RuleException |
111
|
|
|
*/ |
112
|
|
|
public function testFileMimeNotEquals(): void |
113
|
|
|
{ |
114
|
|
|
$data = new File\FileMimeEquals(); |
115
|
|
|
$data->setAgainstValue('octet/stream'); |
116
|
|
|
$this->expectException(RuleException::class); |
117
|
|
|
$data->validate($this->getMockFile()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws RuleException |
122
|
|
|
*/ |
123
|
|
|
public function testFileMimeListFailString(): void |
124
|
|
|
{ |
125
|
|
|
$data = new File\FileMimeList(); |
126
|
|
|
$this->expectException(RuleException::class); |
127
|
|
|
$data->setAgainstValue('text/plain'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @throws RuleException |
132
|
|
|
*/ |
133
|
|
|
public function testFileMimeListFailNumber(): void |
134
|
|
|
{ |
135
|
|
|
$data = new File\FileMimeList(); |
136
|
|
|
$this->expectException(RuleException::class); |
137
|
|
|
$data->setAgainstValue(123456); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @throws RuleException |
142
|
|
|
*/ |
143
|
|
|
public function testFileMimeListFailClass(): void |
144
|
|
|
{ |
145
|
|
|
$data = new File\FileMimeList(); |
146
|
|
|
$this->expectException(RuleException::class); |
147
|
|
|
$data->setAgainstValue(new \stdClass()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @throws RuleException |
152
|
|
|
*/ |
153
|
|
|
public function testFileMimeListFailArrayNumber(): void |
154
|
|
|
{ |
155
|
|
|
$data = new File\FileMimeList(); |
156
|
|
|
$this->expectException(RuleException::class); |
157
|
|
|
$data->setAgainstValue([123456]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @throws RuleException |
162
|
|
|
*/ |
163
|
|
|
public function testFileMimeListFailArrayClass(): void |
164
|
|
|
{ |
165
|
|
|
$data = new File\FileMimeList(); |
166
|
|
|
$this->expectException(RuleException::class); |
167
|
|
|
$data->setAgainstValue([new \stdClass()]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @throws RuleException |
172
|
|
|
*/ |
173
|
|
|
public function testFileMimeList(): void |
174
|
|
|
{ |
175
|
|
|
$data = new File\FileMimeList(); |
176
|
|
|
$data->setAgainstValue(['text/plain']); |
177
|
|
|
$this->assertInstanceOf(File\AFileRule::class, $data); |
178
|
|
|
$data->validate($this->getMockFile()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @throws RuleException |
183
|
|
|
*/ |
184
|
|
|
public function testFileMimeNotList(): void |
185
|
|
|
{ |
186
|
|
|
$data = new File\FileMimeList(); |
187
|
|
|
$data->setAgainstValue(['octet/stream']); |
188
|
|
|
$this->expectException(RuleException::class); |
189
|
|
|
$data->validate($this->getMockFile()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|