-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRulesTest.php
363 lines (303 loc) · 9.5 KB
/
RulesTest.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework Validation Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Validation;
use Framework\Validation\FilesValidator;
use Framework\Validation\Rules;
use Framework\Validation\Validator;
use PHPUnit\Framework\TestCase;
final class RulesTest extends TestCase
{
protected Rules $rules;
protected function setUp() : void
{
$this->rules = new Rules();
}
public function testCreate() : void
{
$r1 = Rules::create();
$r2 = Rules::create();
self::assertInstanceOf(Rules::class, $r1);
self::assertInstanceOf(Rules::class, $r2);
self::assertNotSame($r1, $r2);
}
public function testToString() : void
{
self::assertSame('', (string) $this->rules);
self::assertSame('alpha', (string) $this->rules->alpha());
self::assertSame('alpha|less:3', (string) $this->rules->less(3));
self::assertSame('alpha|less:3|less:3', (string) $this->rules->less(3));
}
public function testGet() : void
{
self::assertSame([], $this->rules->rules);
self::assertSame(['alpha'], $this->rules->alpha()->rules);
self::assertSame(['alpha', 'less:3'], $this->rules->less(3)->rules);
self::assertSame(['alpha', 'less:3', 'less:3'], $this->rules->less(3)->rules);
$this->expectException(\Error::class);
$this->expectExceptionMessage('Cannot access property ' . $this->rules::class . '::$foo');
$this->rules->foo; // @phpstan-ignore-line
}
public function testMethodsCompatibility() : void
{
/**
* @var array<int,\ReflectionMethod> $methods
*/
$methods = [];
$classes = [
new \ReflectionClass(Validator::class),
new \ReflectionClass(FilesValidator::class),
];
foreach ($classes as $class) {
foreach ($class->getMethods(\ReflectionMethod::IS_STATIC) as $method) {
if ($method->isPublic()) {
$methods[] = $method;
}
}
}
$rules = new \ReflectionClass(Rules::class);
foreach ($methods as $method) {
$name = $method->getName();
self::assertTrue($rules->hasMethod($name));
self::assertTrue($rules->getMethod($name)->isPublic());
self::assertFalse($rules->getMethod($name)->isStatic());
self::assertSame(
$method->getNumberOfParameters(),
$rules->getMethod($name)->getNumberOfParameters() + 2
);
}
}
public function testRuleAlpha() : void
{
self::assertRule('alpha', $this->rules->alpha());
}
public function testRuleNumber() : void
{
self::assertRule('number', $this->rules->number());
}
public function testRuleAlphaNumber() : void
{
self::assertRule('alphaNumber', $this->rules->alphaNumber());
}
public function testRuleUuid() : void
{
self::assertRule('uuid', $this->rules->uuid());
}
public function testRuleTimezone() : void
{
self::assertRule('timezone', $this->rules->timezone());
}
public function testRuleBase64() : void
{
self::assertRule('base64', $this->rules->base64());
}
public function testRuleMd5() : void
{
self::assertRule('md5', $this->rules->md5());
}
public function testRuleHex() : void
{
self::assertRule('hex', $this->rules->hex());
}
public function testRuleHexColor() : void
{
self::assertRule('hexColor', $this->rules->hexColor());
}
public function testRuleJson() : void
{
self::assertRule('json', $this->rules->json());
}
public function testRuleRegex() : void
{
self::assertRule('regex:[a-z]', $this->rules->regex('[a-z]'));
}
public function testRuleNotRegex() : void
{
self::assertRule('notRegex:[a-z]', $this->rules->notRegex('[a-z]'));
}
public function testRuleEquals() : void
{
self::assertRule('equals:foo', $this->rules->equals('foo'));
}
public function testRuleNotEquals() : void
{
self::assertRule('notEquals:foo', $this->rules->notEquals('foo'));
}
public function testRuleBetween() : void
{
self::assertRule('between:1,5', $this->rules->between(1, 5));
}
public function testRuleNotBetween() : void
{
self::assertRule('notBetween:1,5', $this->rules->notBetween(1, 5));
}
public function testRuleIn() : void
{
self::assertRule('in:foo,bar', $this->rules->in('foo', 'bar'));
}
public function testRuleNotIn() : void
{
self::assertRule('notIn:foo,bar', $this->rules->notIn('foo', 'bar'));
}
public function testRuleIp() : void
{
self::assertRule('ip:0', $this->rules->ip());
$this->rules = new Rules();
self::assertRule('ip:4', $this->rules->ip(4));
}
public function testRuleUrl() : void
{
self::assertRule('url', $this->rules->url());
}
public function testRuleDatetime() : void
{
self::assertRule('datetime:Y-m-d H:i:s', $this->rules->datetime());
$this->rules = new Rules();
self::assertRule('datetime:r', $this->rules->datetime('r'));
}
public function testRuleEmail() : void
{
self::assertRule('email', $this->rules->email());
}
public function testRuleGreater() : void
{
self::assertRule('greater:8', $this->rules->greater(8));
}
public function testRuleGreaterOrEqual() : void
{
self::assertRule('greaterOrEqual:8', $this->rules->greaterOrEqual(8));
}
public function testRuleLess() : void
{
self::assertRule('less:8', $this->rules->less(8));
}
public function testRuleLessOrEqual() : void
{
self::assertRule('lessOrEqual:8', $this->rules->lessOrEqual(8));
}
public function testRuleLatin() : void
{
self::assertRule('latin', $this->rules->latin());
}
public function testRuleMaxLength() : void
{
self::assertRule('maxLength:32', $this->rules->maxLength(32));
}
public function testRuleMinLength() : void
{
self::assertRule('minLength:5', $this->rules->minLength(5));
}
public function testRuleLength() : void
{
self::assertRule('length:5', $this->rules->length(5));
}
public function testRuleRequired() : void
{
self::assertRule('required', $this->rules->required());
}
public function testRuleIsset() : void
{
self::assertRule('isset', $this->rules->isset());
}
public function testRuleSpecialChar() : void
{
self::assertRule(
'specialChar:1,!"#$%&\'()*+\,-./:;=<>?@[\]^_`{|}~',
$this->rules->specialChar()
);
$this->rules = new Rules();
self::assertRule(
'specialChar:3,@\,#',
$this->rules->specialChar(3, '@,#')
);
}
public function testRuleUploaded() : void
{
self::assertRule('uploaded', $this->rules->uploaded());
}
public function testRuleMaxSize() : void
{
self::assertRule('maxSize:10', $this->rules->maxSize(10));
}
public function testRuleMimes() : void
{
self::assertRule(
'mimes:image/png,image/jpg',
$this->rules->mimes('image/png', 'image/jpg')
);
}
public function testRuleExt() : void
{
self::assertRule('ext:png,jpeg', $this->rules->ext('png', 'jpeg'));
}
public function testRuleImage() : void
{
self::assertRule('image', $this->rules->image());
}
public function testRuleMaxDim() : void
{
self::assertRule('maxDim:100,120', $this->rules->maxDim(100, 120));
}
public function testRuleMinDim() : void
{
self::assertRule('minDim:100,120', $this->rules->minDim(100, 120));
}
public function testRuleDim() : void
{
self::assertRule('dim:100,120', $this->rules->dim(100, 120));
}
public function testRuleOptional() : void
{
self::assertRule('optional', $this->rules->optional());
}
public function testRuleBlank() : void
{
self::assertRule('blank', $this->rules->blank());
}
public function testRuleNull() : void
{
self::assertRule('null', $this->rules->null());
}
public function testRuleEmpty() : void
{
self::assertRule('empty', $this->rules->empty());
}
public function testRuleArray() : void
{
self::assertRule('array', $this->rules->array());
}
public function testRuleBool() : void
{
self::assertRule('bool', $this->rules->bool());
}
public function testRuleFloat() : void
{
self::assertRule('float', $this->rules->float());
}
public function testRuleInt() : void
{
self::assertRule('int', $this->rules->int());
}
public function testRuleObject() : void
{
self::assertRule('object', $this->rules->object());
}
public function testRuleString() : void
{
self::assertRule('string', $this->rules->string());
}
public function testRuleSlug() : void
{
self::assertRule('slug', $this->rules->slug());
}
protected static function assertRule(string $rule, Rules $rules) : void
{
self::assertSame($rule, (string) $rules);
}
}