|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FigTree\Validation\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use FigTree\Validation\Contracts\RuleInterface; |
|
7
|
|
|
use FigTree\Validation\RuleFactory; |
|
8
|
|
|
|
|
9
|
|
|
class RuleFactoryTest extends AbstractTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testAddSlashesRule() |
|
12
|
|
|
{ |
|
13
|
|
|
$rule = $this->ruleFactory()->addSlashes(); |
|
14
|
|
|
|
|
15
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
16
|
|
|
|
|
17
|
|
|
$array = $rule->toArray(); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertIsArray($array); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
22
|
|
|
$this->assertEquals(FILTER_SANITIZE_ADD_SLASHES, $array['filter']); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testCleanEmailRule() |
|
26
|
|
|
{ |
|
27
|
|
|
$rule = $this->ruleFactory()->cleanEmail(); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
30
|
|
|
|
|
31
|
|
|
$array = $rule->toArray(); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertIsArray($array); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
36
|
|
|
$this->assertEquals(FILTER_SANITIZE_EMAIL, $array['filter']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testCleanEncodedStringRule() |
|
40
|
|
|
{ |
|
41
|
|
|
$rule = $this->ruleFactory()->cleanEncodedString(); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
44
|
|
|
|
|
45
|
|
|
$array = $rule->toArray(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertIsArray($array); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
50
|
|
|
$this->assertEquals(FILTER_SANITIZE_ENCODED, $array['filter']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testCleanFloatRule() |
|
54
|
|
|
{ |
|
55
|
|
|
$rule = $this->ruleFactory()->cleanFloat(); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
58
|
|
|
|
|
59
|
|
|
$array = $rule->toArray(); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertIsArray($array); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
64
|
|
|
$this->assertEquals(FILTER_SANITIZE_NUMBER_FLOAT, $array['filter']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testCleanFullSpecialChars() |
|
68
|
|
|
{ |
|
69
|
|
|
$rule = $this->ruleFactory()->cleanFullSpecialChars(); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
72
|
|
|
|
|
73
|
|
|
$array = $rule->toArray(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertIsArray($array); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
78
|
|
|
$this->assertEquals(FILTER_SANITIZE_FULL_SPECIAL_CHARS, $array['filter']); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testCleanInt() |
|
82
|
|
|
{ |
|
83
|
|
|
$rule = $this->ruleFactory()->cleanInt(); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
86
|
|
|
|
|
87
|
|
|
$array = $rule->toArray(); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertIsArray($array); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
92
|
|
|
$this->assertEquals(FILTER_SANITIZE_NUMBER_INT, $array['filter']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testCleanSpecialChars() |
|
96
|
|
|
{ |
|
97
|
|
|
$rule = $this->ruleFactory()->cleanSpecialChars(); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
100
|
|
|
|
|
101
|
|
|
$array = $rule->toArray(); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertIsArray($array); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
106
|
|
|
$this->assertEquals(FILTER_SANITIZE_SPECIAL_CHARS, $array['filter']); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testCleanString() |
|
110
|
|
|
{ |
|
111
|
|
|
$rule = $this->ruleFactory()->cleanString(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
114
|
|
|
|
|
115
|
|
|
$array = $rule->toArray(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertIsArray($array); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
120
|
|
|
$this->assertEquals(FILTER_SANITIZE_STRING, $array['filter']); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testCleanUnsafe() |
|
124
|
|
|
{ |
|
125
|
|
|
$rule = $this->ruleFactory()->cleanUnsafe(); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
128
|
|
|
|
|
129
|
|
|
$array = $rule->toArray(); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertIsArray($array); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
134
|
|
|
$this->assertEquals(FILTER_UNSAFE_RAW, $array['filter']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testCleanUrl() |
|
138
|
|
|
{ |
|
139
|
|
|
$rule = $this->ruleFactory()->cleanUrl(); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
142
|
|
|
|
|
143
|
|
|
$array = $rule->toArray(); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertIsArray($array); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
148
|
|
|
$this->assertEquals(FILTER_SANITIZE_URL, $array['filter']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testValidBool() |
|
152
|
|
|
{ |
|
153
|
|
|
$rule = $this->ruleFactory()->validBool(); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
156
|
|
|
|
|
157
|
|
|
$array = $rule->toArray(); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertIsArray($array); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
162
|
|
|
$this->assertEquals(FILTER_VALIDATE_BOOLEAN, $array['filter']); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertArrayHasKey('flags', $array); |
|
165
|
|
|
$this->assertEquals(FILTER_NULL_ON_FAILURE, ($array['flags'] & FILTER_NULL_ON_FAILURE)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testValidDomain() |
|
169
|
|
|
{ |
|
170
|
|
|
$rule = $this->ruleFactory()->validDomain(); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
173
|
|
|
|
|
174
|
|
|
$array = $rule->toArray(); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertIsArray($array); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
179
|
|
|
$this->assertEquals(FILTER_VALIDATE_DOMAIN, $array['filter']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testValidEmail() |
|
183
|
|
|
{ |
|
184
|
|
|
$rule = $this->ruleFactory()->validEmail(); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
187
|
|
|
|
|
188
|
|
|
$array = $rule->toArray(); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertIsArray($array); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
193
|
|
|
$this->assertEquals(FILTER_VALIDATE_EMAIL, $array['filter']); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function testValidFloat() |
|
197
|
|
|
{ |
|
198
|
|
|
$rule = $this->ruleFactory()->validFloat(); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
201
|
|
|
|
|
202
|
|
|
$array = $rule->toArray(); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertIsArray($array); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
207
|
|
|
$this->assertEquals(FILTER_VALIDATE_FLOAT, $array['filter']); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testValidInt() |
|
211
|
|
|
{ |
|
212
|
|
|
$rule = $this->ruleFactory()->validInt(); |
|
213
|
|
|
|
|
214
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
215
|
|
|
|
|
216
|
|
|
$array = $rule->toArray(); |
|
217
|
|
|
|
|
218
|
|
|
$this->assertIsArray($array); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
221
|
|
|
$this->assertEquals(FILTER_VALIDATE_INT, $array['filter']); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testValidIpAddress() |
|
225
|
|
|
{ |
|
226
|
|
|
$rule = $this->ruleFactory()->validIpAddress(); |
|
227
|
|
|
|
|
228
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
229
|
|
|
|
|
230
|
|
|
$array = $rule->toArray(); |
|
231
|
|
|
|
|
232
|
|
|
$this->assertIsArray($array); |
|
233
|
|
|
|
|
234
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
235
|
|
|
$this->assertEquals(FILTER_VALIDATE_IP, $array['filter']); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function testValidMacAddress() |
|
239
|
|
|
{ |
|
240
|
|
|
$rule = $this->ruleFactory()->validMacAddress(); |
|
241
|
|
|
|
|
242
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
243
|
|
|
|
|
244
|
|
|
$array = $rule->toArray(); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertIsArray($array); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
249
|
|
|
$this->assertEquals(FILTER_VALIDATE_MAC, $array['filter']); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function testValidRegExp() |
|
253
|
|
|
{ |
|
254
|
|
|
$pattern = '/^[a-z]+$/i'; |
|
255
|
|
|
|
|
256
|
|
|
$rule = $this->ruleFactory()->validRegExp($pattern); |
|
257
|
|
|
|
|
258
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
259
|
|
|
|
|
260
|
|
|
$array = $rule->toArray(); |
|
261
|
|
|
|
|
262
|
|
|
$this->assertIsArray($array); |
|
263
|
|
|
|
|
264
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
265
|
|
|
$this->assertEquals(FILTER_VALIDATE_REGEXP, $array['filter']); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertArrayHasKey('options', $array); |
|
268
|
|
|
$this->assertIsArray($array['options']); |
|
269
|
|
|
|
|
270
|
|
|
$options = $array['options']; |
|
271
|
|
|
|
|
272
|
|
|
$this->assertArrayHasKey('regexp', $options); |
|
273
|
|
|
$this->assertEquals($pattern, $options['regexp']); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function testWithCallable() |
|
277
|
|
|
{ |
|
278
|
|
|
$callable = [$this, 'validatorMethod']; |
|
279
|
|
|
|
|
280
|
|
|
$rule = $this->ruleFactory()->withCallable($callable); |
|
281
|
|
|
|
|
282
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
283
|
|
|
|
|
284
|
|
|
$array = $rule->toArray(); |
|
285
|
|
|
|
|
286
|
|
|
$this->assertIsArray($array); |
|
287
|
|
|
|
|
288
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
289
|
|
|
$this->assertEquals(FILTER_CALLBACK, $array['filter']); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
public function testWithClosure() |
|
293
|
|
|
{ |
|
294
|
|
|
$closure = Closure::fromCallable([$this, 'validatorMethod']); |
|
295
|
|
|
|
|
296
|
|
|
$rule = $this->ruleFactory()->withClosure($closure); |
|
297
|
|
|
|
|
298
|
|
|
$this->assertInstanceOf(RuleInterface::class, $rule); |
|
299
|
|
|
|
|
300
|
|
|
$array = $rule->toArray(); |
|
301
|
|
|
|
|
302
|
|
|
$this->assertIsArray($array); |
|
303
|
|
|
|
|
304
|
|
|
$this->assertArrayHasKey('filter', $array); |
|
305
|
|
|
$this->assertEquals(FILTER_CALLBACK, $array['filter']); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
public function validatorMethod($value) |
|
309
|
|
|
{ |
|
310
|
|
|
return (is_string($value) && $value == 'foo'); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
protected function ruleFactory() |
|
314
|
|
|
{ |
|
315
|
|
|
return new RuleFactory(); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|