1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Rules; |
4
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class RangeRulesTest extends CommonTestClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param mixed $expectedValue |
11
|
|
|
* @param bool $gotException |
12
|
|
|
* @throws RuleException |
13
|
|
|
* @dataProvider inputRangeProvider |
14
|
|
|
*/ |
15
|
|
|
public function testRangeSet($expectedValue, bool $gotException): void |
16
|
|
|
{ |
17
|
|
|
$data = new Rules\InRange(); |
18
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
19
|
|
|
if ($gotException) $this->expectException(RuleException::class); |
20
|
|
|
$data->setAgainstValue($expectedValue); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function inputRangeProvider(): array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
[['8', '4'], false], |
27
|
|
|
['8', true], // no array |
28
|
|
|
[new \stdClass(), true], // class |
29
|
|
|
[['abc', ['8', '4']], true], // sub-array |
30
|
|
|
[[new \stdClass(), new \stdClass()], true], // classes |
31
|
|
|
[['8', 22, 2, 13, '4'], false], // choose your fun |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|int $checkValue |
37
|
|
|
* @param string|int $lowerLimit |
38
|
|
|
* @param string|int $upperLimit |
39
|
|
|
* @param bool $range |
40
|
|
|
* @param bool $rangeEq |
41
|
|
|
* @throws RuleException |
42
|
|
|
* @dataProvider compareRangesProvider |
43
|
|
|
*/ |
44
|
|
|
public function testRangeIn($checkValue, $lowerLimit, $upperLimit, bool $range, bool $rangeEq): void |
45
|
|
|
{ |
46
|
|
|
$data = new Rules\InRange(); |
47
|
|
|
$data->setAgainstValue([$lowerLimit, $upperLimit]); |
48
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
49
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
50
|
|
|
if (!$range) $this->expectException(RuleException::class); |
51
|
|
|
$data->validate($mock); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|int $checkValue |
56
|
|
|
* @param string|int $lowerLimit |
57
|
|
|
* @param string|int $upperLimit |
58
|
|
|
* @param bool $range |
59
|
|
|
* @param bool $rangeEq |
60
|
|
|
* @throws RuleException |
61
|
|
|
* @dataProvider compareRangesProvider |
62
|
|
|
*/ |
63
|
|
|
public function testRangeInEquals($checkValue, $lowerLimit, $upperLimit, bool $range, bool $rangeEq): void |
64
|
|
|
{ |
65
|
|
|
$data = new Rules\InRangeEquals(); |
66
|
|
|
$data->setAgainstValue([$lowerLimit, $upperLimit]); |
67
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
68
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
69
|
|
|
if (!$rangeEq) $this->expectException(RuleException::class); |
70
|
|
|
$data->validate($mock); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|int $checkValue |
75
|
|
|
* @param string|int $lowerLimit |
76
|
|
|
* @param string|int $upperLimit |
77
|
|
|
* @param bool $range |
78
|
|
|
* @param bool $rangeEq |
79
|
|
|
* @throws RuleException |
80
|
|
|
* @dataProvider compareRangesProvider |
81
|
|
|
*/ |
82
|
|
|
public function testRangeOut($checkValue, $lowerLimit, $upperLimit, bool $range, bool $rangeEq): void |
83
|
|
|
{ |
84
|
|
|
$data = new Rules\OutRange(); |
85
|
|
|
$data->setAgainstValue([$lowerLimit, $upperLimit]); |
86
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
87
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
88
|
|
|
if ($range) $this->expectException(RuleException::class); |
89
|
|
|
$data->validate($mock); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string|int $checkValue |
94
|
|
|
* @param string|int $lowerLimit |
95
|
|
|
* @param string|int $upperLimit |
96
|
|
|
* @param bool $range |
97
|
|
|
* @param bool $rangeEq |
98
|
|
|
* @throws RuleException |
99
|
|
|
* @dataProvider compareRangesProvider |
100
|
|
|
*/ |
101
|
|
|
public function testRangeOutEquals($checkValue, $lowerLimit, $upperLimit, bool $range, bool $rangeEq): void |
102
|
|
|
{ |
103
|
|
|
$data = new Rules\OutRangeEquals(); |
104
|
|
|
$data->setAgainstValue([$lowerLimit, $upperLimit]); |
105
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
106
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
107
|
|
|
if ($rangeEq) $this->expectException(RuleException::class); |
108
|
|
|
$data->validate($mock); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function compareRangesProvider(): array |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
[2, '8', '4', false, false, ], |
115
|
|
|
[2, '8', '2', false, true, ], |
116
|
|
|
[6, '4', '8', true, true, ], |
117
|
|
|
[10, '2', '10', false, true, ], |
118
|
|
|
[10, '2', '6', false, false, ], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|