Passed
Branch dev (f9b2a9)
by Alan
02:42
created

RuleFactoryTest::testRuleFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace FigTree\Validation\Tests;
4
5
use const FILTER_SANITIZE_FULL_SPECIAL_CHARS;
1 ignored issue
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
6
7
use Closure;
8
use FigTree\Validation\Contracts\RuleFactoryInterface;
9
use FigTree\Validation\Contracts\RuleInterface;
10
use FigTree\Validation\AbstractRuleFactory;
11
use FigTree\Validation\RuleFactory;
12
13
class RuleFactoryTest extends AbstractTestCase
14
{
15
	/**
16
	 * @small
17
	 */
18
	public function testRuleFactory()
19
	{
20
		$ruleFactory = new RuleFactory();
21
22
		$this->assertInstanceOf(RuleFactoryInterface::class, $ruleFactory);
23
		$this->assertInstanceOf(AbstractRuleFactory::class, $ruleFactory);
24
	}
25
26
	/**
27
	 * @small
28
	 */
29
	public function testAddSlashesRule()
30
	{
31
		$ruleFactory = new RuleFactory();
32
33
		$rule = $ruleFactory->addSlashes();
34
35
		$this->assertInstanceOf(RuleInterface::class, $rule);
36
37
		$this->assertEquals(FILTER_SANITIZE_ADD_SLASHES, $rule->getFilterType());
38
		$this->assertEquals(0, $rule->getFlags());
39
		$this->assertIsArray($rule->getOptions());
40
		$this->assertEmpty($rule->getOptions());
41
		$this->assertNull($rule->getCallback());
42
43
		$array = $rule->toArray();
44
45
		$this->assertIsArray($array);
46
47
		$this->assertArrayHasKey('filter', $array);
48
		$this->assertEquals(FILTER_SANITIZE_ADD_SLASHES, $array['filter']);
49
	}
50
51
	/**
52
	 * @small
53
	 */
54
	public function testCleanEmailRule()
55
	{
56
		$ruleFactory = new RuleFactory();
57
58
		$rule = $ruleFactory->cleanEmail();
59
60
		$this->assertInstanceOf(RuleInterface::class, $rule);
61
62
		$this->assertEquals(FILTER_SANITIZE_EMAIL, $rule->getFilterType());
63
		$this->assertEquals(0, $rule->getFlags());
64
		$this->assertIsArray($rule->getOptions());
65
		$this->assertEmpty($rule->getOptions());
66
		$this->assertNull($rule->getCallback());
67
68
		$array = $rule->toArray();
69
70
		$this->assertIsArray($array);
71
72
		$this->assertArrayHasKey('filter', $array);
73
		$this->assertEquals(FILTER_SANITIZE_EMAIL, $array['filter']);
74
	}
75
76
	/**
77
	 * @small
78
	 */
79
	public function testCleanEncodedStringRule()
80
	{
81
		$ruleFactory = new RuleFactory();
82
83
		$rule = $ruleFactory->cleanEncodedString();
84
85
		$this->assertInstanceOf(RuleInterface::class, $rule);
86
87
		$this->assertEquals(FILTER_SANITIZE_ENCODED, $rule->getFilterType());
88
		$this->assertEquals(0, $rule->getFlags());
89
		$this->assertIsArray($rule->getOptions());
90
		$this->assertEmpty($rule->getOptions());
91
		$this->assertNull($rule->getCallback());
92
93
		$array = $rule->toArray();
94
95
		$this->assertIsArray($array);
96
97
		$this->assertArrayHasKey('filter', $array);
98
		$this->assertEquals(FILTER_SANITIZE_ENCODED, $array['filter']);
99
	}
100
101
	/**
102
	 * @small
103
	 */
104
	public function testCleanFloatRule()
105
	{
106
		$ruleFactory = new RuleFactory();
107
108
		$rule = $ruleFactory->cleanFloat();
109
110
		$this->assertInstanceOf(RuleInterface::class, $rule);
111
112
		$this->assertEquals(FILTER_SANITIZE_NUMBER_FLOAT, $rule->getFilterType());
113
		$this->assertEquals(0, $rule->getFlags());
114
		$this->assertIsArray($rule->getOptions());
115
		$this->assertEmpty($rule->getOptions());
116
		$this->assertNull($rule->getCallback());
117
118
		$array = $rule->toArray();
119
120
		$this->assertIsArray($array);
121
122
		$this->assertArrayHasKey('filter', $array);
123
		$this->assertEquals(FILTER_SANITIZE_NUMBER_FLOAT, $array['filter']);
124
	}
125
126
	/**
127
	 * @small
128
	 */
129
	public function testCleanFullSpecialChars()
130
	{
131
		$ruleFactory = new RuleFactory();
132
133
		$rule = $ruleFactory->cleanFullSpecialChars();
134
135
		$this->assertInstanceOf(RuleInterface::class, $rule);
136
137
		$this->assertEquals(FILTER_SANITIZE_FULL_SPECIAL_CHARS, $rule->getFilterType());
1 ignored issue
show
Bug introduced by
The constant FILTER_SANITIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
138
		$this->assertEquals(0, $rule->getFlags());
139
		$this->assertIsArray($rule->getOptions());
140
		$this->assertEmpty($rule->getOptions());
141
		$this->assertNull($rule->getCallback());
142
143
		$array = $rule->toArray();
144
145
		$this->assertIsArray($array);
146
147
		$this->assertArrayHasKey('filter', $array);
148
		$this->assertEquals(FILTER_SANITIZE_FULL_SPECIAL_CHARS, $array['filter']);
149
	}
150
151
	/**
152
	 * @small
153
	 */
154
	public function testCleanInt()
155
	{
156
		$ruleFactory = new RuleFactory();
157
158
		$rule = $ruleFactory->cleanInt();
159
160
		$this->assertInstanceOf(RuleInterface::class, $rule);
161
162
		$this->assertEquals(FILTER_SANITIZE_NUMBER_INT, $rule->getFilterType());
163
		$this->assertEquals(0, $rule->getFlags());
164
		$this->assertIsArray($rule->getOptions());
165
		$this->assertEmpty($rule->getOptions());
166
		$this->assertNull($rule->getCallback());
167
168
		$array = $rule->toArray();
169
170
		$this->assertIsArray($array);
171
172
		$this->assertArrayHasKey('filter', $array);
173
		$this->assertEquals(FILTER_SANITIZE_NUMBER_INT, $array['filter']);
174
	}
175
176
	/**
177
	 * @small
178
	 */
179
	public function testCleanSpecialChars()
180
	{
181
		$ruleFactory = new RuleFactory();
182
183
		$rule = $ruleFactory->cleanSpecialChars();
184
185
		$this->assertInstanceOf(RuleInterface::class, $rule);
186
187
		$this->assertEquals(FILTER_SANITIZE_SPECIAL_CHARS, $rule->getFilterType());
188
		$this->assertEquals(0, $rule->getFlags());
189
		$this->assertIsArray($rule->getOptions());
190
		$this->assertEmpty($rule->getOptions());
191
		$this->assertNull($rule->getCallback());
192
193
		$array = $rule->toArray();
194
195
		$this->assertIsArray($array);
196
197
		$this->assertArrayHasKey('filter', $array);
198
		$this->assertEquals(FILTER_SANITIZE_SPECIAL_CHARS, $array['filter']);
199
	}
200
201
	/**
202
	 * @small
203
	 */
204
	public function testCleanString()
205
	{
206
		$ruleFactory = new RuleFactory();
207
208
		$rule = $ruleFactory->cleanString();
209
210
		$this->assertInstanceOf(RuleInterface::class, $rule);
211
212
		$this->assertEquals(FILTER_SANITIZE_STRING, $rule->getFilterType());
213
		$this->assertEquals(0, $rule->getFlags());
214
		$this->assertIsArray($rule->getOptions());
215
		$this->assertEmpty($rule->getOptions());
216
		$this->assertNull($rule->getCallback());
217
218
		$array = $rule->toArray();
219
220
		$this->assertIsArray($array);
221
222
		$this->assertArrayHasKey('filter', $array);
223
		$this->assertEquals(FILTER_SANITIZE_STRING, $array['filter']);
224
	}
225
226
	/**
227
	 * @small
228
	 */
229
	public function testCleanUnsafe()
230
	{
231
		$ruleFactory = new RuleFactory();
232
233
		$rule = $ruleFactory->cleanUnsafe();
234
235
		$this->assertInstanceOf(RuleInterface::class, $rule);
236
237
		$this->assertEquals(FILTER_UNSAFE_RAW, $rule->getFilterType());
238
		$this->assertEquals(0, $rule->getFlags());
239
		$this->assertIsArray($rule->getOptions());
240
		$this->assertEmpty($rule->getOptions());
241
		$this->assertNull($rule->getCallback());
242
243
		$array = $rule->toArray();
244
245
		$this->assertIsArray($array);
246
247
		$this->assertArrayHasKey('filter', $array);
248
		$this->assertEquals(FILTER_UNSAFE_RAW, $array['filter']);
249
	}
250
251
	/**
252
	 * @small
253
	 */
254
	public function testCleanUrl()
255
	{
256
		$ruleFactory = new RuleFactory();
257
258
		$rule = $ruleFactory->cleanUrl();
259
260
		$this->assertInstanceOf(RuleInterface::class, $rule);
261
262
		$this->assertEquals(FILTER_SANITIZE_URL, $rule->getFilterType());
263
		$this->assertEquals(0, $rule->getFlags());
264
		$this->assertIsArray($rule->getOptions());
265
		$this->assertEmpty($rule->getOptions());
266
		$this->assertNull($rule->getCallback());
267
268
		$array = $rule->toArray();
269
270
		$this->assertIsArray($array);
271
272
		$this->assertArrayHasKey('filter', $array);
273
		$this->assertEquals(FILTER_SANITIZE_URL, $array['filter']);
274
	}
275
276
	/**
277
	 * @small
278
	 */
279
	public function testValidBool()
280
	{
281
		$ruleFactory = new RuleFactory();
282
283
		$rule = $ruleFactory->validBool();
284
285
		$this->assertInstanceOf(RuleInterface::class, $rule);
286
287
		$this->assertEquals(FILTER_VALIDATE_BOOLEAN, $rule->getFilterType());
288
		$this->assertEquals(FILTER_NULL_ON_FAILURE, $rule->getFlags());
289
		$this->assertIsArray($rule->getOptions());
290
		$this->assertEmpty($rule->getOptions());
291
		$this->assertNull($rule->getCallback());
292
293
		$array = $rule->toArray();
294
295
		$this->assertIsArray($array);
296
297
		$this->assertArrayHasKey('filter', $array);
298
		$this->assertEquals(FILTER_VALIDATE_BOOLEAN, $array['filter']);
299
300
		$this->assertArrayHasKey('flags', $array);
301
		$this->assertEquals(FILTER_NULL_ON_FAILURE, ($array['flags'] & FILTER_NULL_ON_FAILURE));
302
	}
303
304
	/**
305
	 * @small
306
	 */
307
	public function testValidDomain()
308
	{
309
		$ruleFactory = new RuleFactory();
310
311
		$rule = $ruleFactory->validDomain();
312
313
		$this->assertInstanceOf(RuleInterface::class, $rule);
314
315
		$this->assertEquals(FILTER_VALIDATE_DOMAIN, $rule->getFilterType());
316
		$this->assertEquals(0, $rule->getFlags());
317
		$this->assertIsArray($rule->getOptions());
318
		$this->assertEmpty($rule->getOptions());
319
		$this->assertNull($rule->getCallback());
320
321
		$array = $rule->toArray();
322
323
		$this->assertIsArray($array);
324
325
		$this->assertArrayHasKey('filter', $array);
326
		$this->assertEquals(FILTER_VALIDATE_DOMAIN, $array['filter']);
327
	}
328
329
	/**
330
	 * @small
331
	 */
332
	public function testValidEmail()
333
	{
334
		$ruleFactory = new RuleFactory();
335
336
		$rule = $ruleFactory->validEmail();
337
338
		$this->assertInstanceOf(RuleInterface::class, $rule);
339
340
		$this->assertEquals(FILTER_VALIDATE_EMAIL, $rule->getFilterType());
341
		$this->assertEquals(0, $rule->getFlags());
342
		$this->assertIsArray($rule->getOptions());
343
		$this->assertEmpty($rule->getOptions());
344
		$this->assertNull($rule->getCallback());
345
346
		$array = $rule->toArray();
347
348
		$this->assertIsArray($array);
349
350
		$this->assertArrayHasKey('filter', $array);
351
		$this->assertEquals(FILTER_VALIDATE_EMAIL, $array['filter']);
352
	}
353
354
	/**
355
	 * @small
356
	 */
357
	public function testValidFloat()
358
	{
359
		$ruleFactory = new RuleFactory();
360
361
		$rule = $ruleFactory->validFloat();
362
363
		$this->assertInstanceOf(RuleInterface::class, $rule);
364
365
		$this->assertEquals(FILTER_VALIDATE_FLOAT, $rule->getFilterType());
366
		$this->assertEquals(0, $rule->getFlags());
367
		$this->assertIsArray($rule->getOptions());
368
		$this->assertEmpty($rule->getOptions());
369
		$this->assertNull($rule->getCallback());
370
371
		$array = $rule->toArray();
372
373
		$this->assertIsArray($array);
374
375
		$this->assertArrayHasKey('filter', $array);
376
		$this->assertEquals(FILTER_VALIDATE_FLOAT, $array['filter']);
377
	}
378
379
	/**
380
	 * @small
381
	 */
382
	public function testValidInt()
383
	{
384
		$ruleFactory = new RuleFactory();
385
386
		$rule = $ruleFactory->validInt();
387
388
		$this->assertInstanceOf(RuleInterface::class, $rule);
389
390
		$this->assertEquals(FILTER_VALIDATE_INT, $rule->getFilterType());
391
		$this->assertEquals(0, $rule->getFlags());
392
		$this->assertIsArray($rule->getOptions());
393
		$this->assertEmpty($rule->getOptions());
394
		$this->assertNull($rule->getCallback());
395
396
		$array = $rule->toArray();
397
398
		$this->assertIsArray($array);
399
400
		$this->assertArrayHasKey('filter', $array);
401
		$this->assertEquals(FILTER_VALIDATE_INT, $array['filter']);
402
	}
403
404
	/**
405
	 * @small
406
	 */
407
	public function testValidIpAddress()
408
	{
409
		$ruleFactory = new RuleFactory();
410
411
		$rule = $ruleFactory->validIpAddress();
412
413
		$this->assertInstanceOf(RuleInterface::class, $rule);
414
415
		$this->assertEquals(FILTER_VALIDATE_IP, $rule->getFilterType());
416
		$this->assertEquals(0, $rule->getFlags());
417
		$this->assertIsArray($rule->getOptions());
418
		$this->assertEmpty($rule->getOptions());
419
		$this->assertNull($rule->getCallback());
420
421
		$array = $rule->toArray();
422
423
		$this->assertIsArray($array);
424
425
		$this->assertArrayHasKey('filter', $array);
426
		$this->assertEquals(FILTER_VALIDATE_IP, $array['filter']);
427
	}
428
429
	/**
430
	 * @small
431
	 */
432
	public function testValidMacAddress()
433
	{
434
		$ruleFactory = new RuleFactory();
435
436
		$rule = $ruleFactory->validMacAddress();
437
438
		$this->assertInstanceOf(RuleInterface::class, $rule);
439
440
		$this->assertEquals(FILTER_VALIDATE_MAC, $rule->getFilterType());
441
		$this->assertEquals(0, $rule->getFlags());
442
		$this->assertIsArray($rule->getOptions());
443
		$this->assertEmpty($rule->getOptions());
444
		$this->assertNull($rule->getCallback());
445
446
		$array = $rule->toArray();
447
448
		$this->assertIsArray($array);
449
450
		$this->assertArrayHasKey('filter', $array);
451
		$this->assertEquals(FILTER_VALIDATE_MAC, $array['filter']);
452
	}
453
454
	/**
455
	 * @small
456
	 */
457
	public function testValidRegExp()
458
	{
459
		$pattern = '/^[a-z]+$/i';
460
461
		$ruleFactory = new RuleFactory();
462
463
		$rule = $ruleFactory->validRegExp($pattern);
464
465
		$this->assertInstanceOf(RuleInterface::class, $rule);
466
467
		$this->assertEquals(FILTER_VALIDATE_REGEXP, $rule->getFilterType());
468
		$this->assertEquals(0, $rule->getFlags());
469
		$this->assertIsArray($rule->getOptions());
470
		$this->assertNull($rule->getCallback());
471
472
		$options = $rule->getOptions();
473
474
		$this->assertIsArray($options);
475
		$this->assertNotEmpty($options);
476
477
		$this->assertArrayHasKey('regexp', $options);
478
		$this->assertEquals($pattern, $options['regexp']);
479
480
		$array = $rule->toArray();
481
482
		$this->assertIsArray($array);
483
484
		$this->assertArrayHasKey('filter', $array);
485
		$this->assertEquals(FILTER_VALIDATE_REGEXP, $array['filter']);
486
487
		$this->assertArrayHasKey('options', $array);
488
		$this->assertIsArray($array['options']);
489
490
		$options = $array['options'];
491
492
		$this->assertArrayHasKey('regexp', $options);
493
		$this->assertEquals($pattern, $options['regexp']);
494
	}
495
496
	/**
497
	 * @small
498
	 */
499
	public function testWithCallable()
500
	{
501
		$callable = [$this, 'validatorMethod'];
502
503
		$ruleFactory = new RuleFactory();
504
505
		$rule = $ruleFactory->withCallable($callable);
506
507
		$this->assertInstanceOf(RuleInterface::class, $rule);
508
509
		$this->assertEquals(FILTER_CALLBACK, $rule->getFilterType());
510
		$this->assertEquals(0, $rule->getFlags());
511
		$this->assertIsArray($rule->getOptions());
512
		$this->assertEmpty($rule->getOptions());
513
514
		$this->assertNotNull($rule->getCallback());
515
		$this->assertInstanceOf(Closure::class, $rule->getCallback());
516
		$this->assertEquals(Closure::fromCallable($callable), $rule->getCallback());
517
518
		$array = $rule->toArray();
519
520
		$this->assertIsArray($array);
521
522
		$this->assertArrayHasKey('filter', $array);
523
		$this->assertEquals(FILTER_CALLBACK, $array['filter']);
524
525
		$this->assertArrayHasKey('options', $array);
526
		$this->assertInstanceOf(Closure::class, $array['options']);
527
		$this->assertEquals(Closure::fromCallable($callable), $array['options']);
528
	}
529
530
	/**
531
	 * @small
532
	 */
533
	public function testWithClosure()
534
	{
535
		$closure = Closure::fromCallable([$this, 'validatorMethod']);
536
537
		$ruleFactory = new RuleFactory();
538
539
		$rule = $ruleFactory->withClosure($closure);
540
541
		$this->assertInstanceOf(RuleInterface::class, $rule);
542
543
		$this->assertEquals(FILTER_CALLBACK, $rule->getFilterType());
544
		$this->assertEquals(0, $rule->getFlags());
545
		$this->assertIsArray($rule->getOptions());
546
		$this->assertEmpty($rule->getOptions());
547
548
		$this->assertNotNull($rule->getCallback());
549
		$this->assertInstanceOf(Closure::class, $rule->getCallback());
550
		$this->assertEquals($closure, $rule->getCallback());
551
552
		$array = $rule->toArray();
553
554
		$this->assertIsArray($array);
555
556
		$this->assertArrayHasKey('filter', $array);
557
		$this->assertEquals(FILTER_CALLBACK, $array['filter']);
558
559
		$this->assertArrayHasKey('options', $array);
560
		$this->assertInstanceOf(Closure::class, $array['options']);
561
		$this->assertEquals($closure, $array['options']);
562
	}
563
564
	public function validatorMethod($value)
565
	{
566
		return (is_string($value) && $value == 'foo');
567
	}
568
}
569