|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Yiisoft\Validator\Rule\InRange; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @group validators |
|
10
|
|
|
*/ |
|
11
|
|
|
class InRangeTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testInitException(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$this->expectException(\RuntimeException::class); |
|
16
|
|
|
$this->expectExceptionMessage('The "range" property must be set.'); |
|
17
|
|
|
new InRange('not an array'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testValidate(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$val = new InRange(range(1, 10)); |
|
23
|
|
|
$this->assertTrue($val->validate(1)->isValid()); |
|
24
|
|
|
$this->assertFalse($val->validate(0)->isValid()); |
|
25
|
|
|
$this->assertFalse($val->validate(11)->isValid()); |
|
26
|
|
|
$this->assertFalse($val->validate(5.5)->isValid()); |
|
27
|
|
|
$this->assertTrue($val->validate(10)->isValid()); |
|
28
|
|
|
$this->assertTrue($val->validate('10')->isValid()); |
|
29
|
|
|
$this->assertTrue($val->validate('5')->isValid()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testValidateEmpty(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$rule = (new InRange(range(10, 20)))->skipOnEmpty(false); |
|
35
|
|
|
$this->assertFalse($rule->validate(null)->isValid()); //row RangeValidatorTest.php:101 |
|
36
|
|
|
$this->assertFalse($rule->validate('0')->isValid()); |
|
37
|
|
|
$this->assertFalse($rule->validate(0)->isValid()); |
|
38
|
|
|
$this->assertFalse($rule->validate('')->isValid()); |
|
39
|
|
|
|
|
40
|
|
|
$rule = (new InRange(range(10, 20, 1))) |
|
41
|
|
|
->skipOnEmpty(false); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertTrue($rule->validate([])->isValid()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testValidateArrayValue(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$rule = (new InRange(range(1, 10))); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertTrue($rule->validate([1, 2, 3, 4, 5])->isValid()); |
|
51
|
|
|
$this->assertTrue($rule->validate([6, 7, 8, 9, 10])->isValid()); |
|
52
|
|
|
$this->assertFalse($rule->validate([0, 1, 2])->isValid()); |
|
53
|
|
|
$this->assertFalse($rule->validate([10, 11, 12])->isValid()); |
|
54
|
|
|
$this->assertTrue($rule->validate(['1', '2', '3', 4, 5, 6])->isValid()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testValidateStrict(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$rule = (new InRange(range(1, 10))) |
|
60
|
|
|
->strict(); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue($rule->validate(1)->isValid()); |
|
63
|
|
|
$this->assertTrue($rule->validate(5)->isValid()); |
|
64
|
|
|
$this->assertTrue($rule->validate(10)->isValid()); |
|
65
|
|
|
$this->assertFalse($rule->validate('1')->isValid()); |
|
66
|
|
|
$this->assertFalse($rule->validate('10')->isValid()); |
|
67
|
|
|
$this->assertFalse($rule->validate('5.5')->isValid()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testValidateArrayValueStrict(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$rule = (new InRange(range(1, 10, 1))) |
|
73
|
|
|
->strict(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertFalse($rule->validate(['1', '2', '3', '4', '5', '6'])->isValid()); |
|
76
|
|
|
$this->assertFalse($rule->validate(['1', '2', '3', 4, 5, 6])->isValid()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testValidateNot() |
|
80
|
|
|
{ |
|
81
|
|
|
$rule = (new InRange(range(1, 10, 1))) |
|
82
|
|
|
->not(); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertFalse($rule->validate(1)->isValid()); |
|
85
|
|
|
$this->assertTrue($rule->validate(0)->isValid()); |
|
86
|
|
|
$this->assertTrue($rule->validate(11)->isValid()); |
|
87
|
|
|
$this->assertTrue($rule->validate(5.5)->isValid()); |
|
88
|
|
|
$this->assertFalse($rule->validate(10)->isValid()); |
|
89
|
|
|
$this->assertFalse($rule->validate('10')->isValid()); |
|
90
|
|
|
$this->assertFalse($rule->validate('5')->isValid()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testValidateSubsetArrayable(): void |
|
94
|
|
|
{ |
|
95
|
|
|
// Test in array, values are arrays. IE: ['a'] in [['a'], ['b']] |
|
96
|
|
|
$rule = (new InRange([['a'], ['b']])); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertTrue($rule->validate(['a'])->isValid()); |
|
99
|
|
|
|
|
100
|
|
|
// Test in array, values are arrays. IE: ['a', 'b'] subset [['a', 'b', 'c'] |
|
101
|
|
|
$rule = (new InRange(['a', 'b', 'c'])); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertTrue($rule->validate(['a', 'b'])->isValid()); |
|
104
|
|
|
|
|
105
|
|
|
// Test in array, values are arrays. IE: ['a', 'b'] subset [['a', 'b', 'c'] |
|
106
|
|
|
$rule = (new InRange(['a', 'b', 'c'])); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertTrue($rule->validate(new \ArrayObject(['a', 'b']))->isValid()); |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// Test range as ArrayObject. |
|
112
|
|
|
$rule = (new InRange(new \ArrayObject(['a', 'b']))); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertTrue($rule->validate('a')->isValid()); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|