RuleFactoryTest::testCleanFloatRule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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