1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Rules; |
4
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class BasicRulesTest extends CommonTestClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $key |
11
|
|
|
* @param string $expectedValue |
12
|
|
|
* @param string $checkValue |
13
|
|
|
* @param bool $gotResult |
14
|
|
|
* @throws RuleException |
15
|
|
|
* @dataProvider equalsProvider |
16
|
|
|
*/ |
17
|
|
|
public function testEquals(string $key, string $expectedValue, string $checkValue, bool $gotResult): void |
18
|
|
|
{ |
19
|
|
|
$data = new Rules\Equals(); |
20
|
|
|
$data->setErrorText('Custom error'); |
21
|
|
|
$data->setAgainstValue($expectedValue); |
22
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
23
|
|
|
|
24
|
|
|
$mock = MockEntry::init($key, $checkValue); |
25
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
26
|
|
|
$data->validate($mock); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $key |
31
|
|
|
* @param string $expectedValue |
32
|
|
|
* @param string $checkValue |
33
|
|
|
* @param bool $gotResult |
34
|
|
|
* @throws RuleException |
35
|
|
|
* @dataProvider equalsProvider |
36
|
|
|
*/ |
37
|
|
|
public function testNotEquals(string $key, string $expectedValue, string $checkValue, bool $gotResult): void |
38
|
|
|
{ |
39
|
|
|
$data = new Rules\NotEquals(); |
40
|
|
|
$data->setAgainstValue($expectedValue); |
41
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
42
|
|
|
$mock = MockEntry::init($key, $checkValue); |
43
|
|
|
if ($gotResult) $this->expectException(RuleException::class); |
44
|
|
|
$data->validate($mock); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $expectedValues |
49
|
|
|
* @param string $checkValue |
50
|
|
|
* @param bool $gotError |
51
|
|
|
* @param bool $gotResult |
52
|
|
|
* @throws RuleException |
53
|
|
|
* @dataProvider arrayProvider |
54
|
|
|
*/ |
55
|
|
|
public function testInArray($expectedValues, $checkValue, bool $gotError, bool $gotResult): void |
56
|
|
|
{ |
57
|
|
|
$data = new Rules\IsInArray(); |
58
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
59
|
|
|
if ($gotError) $this->expectException(RuleException::class); |
60
|
|
|
$data->setAgainstValue($expectedValues); |
61
|
|
|
if (!$gotError) { |
62
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
63
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
64
|
|
|
$data->validate($mock); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $expectedValues |
70
|
|
|
* @param string $checkValue |
71
|
|
|
* @param bool $gotError |
72
|
|
|
* @param bool $gotResult |
73
|
|
|
* @throws RuleException |
74
|
|
|
* @dataProvider arrayProvider |
75
|
|
|
*/ |
76
|
|
|
public function testNotInArray($expectedValues, $checkValue, bool $gotError, bool $gotResult): void |
77
|
|
|
{ |
78
|
|
|
$data = new Rules\IsNotInArray(); |
79
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
80
|
|
|
if ($gotError) $this->expectException(RuleException::class); |
81
|
|
|
$data->setAgainstValue($expectedValues); |
82
|
|
|
if (!$gotError) { |
83
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
84
|
|
|
if ($gotResult) $this->expectException(RuleException::class); |
85
|
|
|
$data->validate($mock); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function arrayProvider(): array |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
['5', '7', true, false], |
93
|
|
|
[[new \stdClass()], '7', true, false], |
94
|
|
|
[[1,3,5,9], '7', false, false], |
95
|
|
|
[[1,3,5,9], '5', false, true], |
96
|
|
|
[['2','4','6','8'], 6, false, true], |
97
|
|
|
[['abc','def','ghi','jkl'], 'mno', false, false], |
98
|
|
|
[['abc','def','ghi','jkl'], 'def', false, true], |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $expectedValue |
104
|
|
|
* @param string $checkValue |
105
|
|
|
* @param bool $gotResult |
106
|
|
|
* @throws RuleException |
107
|
|
|
* @dataProvider compareGtProvider |
108
|
|
|
*/ |
109
|
|
|
public function testGreaterThan(string $expectedValue, string $checkValue, bool $gotResult): void |
110
|
|
|
{ |
111
|
|
|
$data = new Rules\GreaterThan(); |
112
|
|
|
$data->setAgainstValue($expectedValue); |
113
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
114
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
115
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
116
|
|
|
$data->validate($mock); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $expectedValue |
121
|
|
|
* @param string $checkValue |
122
|
|
|
* @param bool $gotResult |
123
|
|
|
* @throws RuleException |
124
|
|
|
* @dataProvider compareLqProvider |
125
|
|
|
*/ |
126
|
|
|
public function testGreaterEquals(string $expectedValue, string $checkValue, bool $gotResult): void |
127
|
|
|
{ |
128
|
|
|
$data = new Rules\GreaterEquals(); |
129
|
|
|
$data->setAgainstValue($expectedValue); |
130
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
131
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
132
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
133
|
|
|
$data->validate($mock); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $expectedValue |
138
|
|
|
* @param string $checkValue |
139
|
|
|
* @param bool $gotResult |
140
|
|
|
* @throws RuleException |
141
|
|
|
* @dataProvider compareLqProvider |
142
|
|
|
*/ |
143
|
|
|
public function testLesserThan(string $expectedValue, string $checkValue, bool $gotResult): void |
144
|
|
|
{ |
145
|
|
|
$data = new Rules\LesserThan(); |
146
|
|
|
$data->setAgainstValue($expectedValue); |
147
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
148
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
149
|
|
|
if ($gotResult) $this->expectException(RuleException::class); |
150
|
|
|
$data->validate($mock); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $expectedValue |
155
|
|
|
* @param string $checkValue |
156
|
|
|
* @param bool $gotResult |
157
|
|
|
* @throws RuleException |
158
|
|
|
* @dataProvider compareGtProvider |
159
|
|
|
*/ |
160
|
|
|
public function testLesserEquals(string $expectedValue, string $checkValue, bool $gotResult): void |
161
|
|
|
{ |
162
|
|
|
$data = new Rules\LesserEquals(); |
163
|
|
|
$data->setAgainstValue($expectedValue); |
164
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
165
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
166
|
|
|
if ($gotResult) $this->expectException(RuleException::class); |
167
|
|
|
$data->validate($mock); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function compareGtProvider(): array |
171
|
|
|
{ |
172
|
|
|
return [ |
173
|
|
|
['5', '7', true], |
174
|
|
|
['7', '5', false], |
175
|
|
|
['6', '6', false], |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function compareLqProvider(): array |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
['4', '8', true], |
183
|
|
|
['8', '4', false], |
184
|
|
|
['6', '6', true], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param mixed $checkValue |
190
|
|
|
* @param bool $gotResult |
191
|
|
|
* @throws RuleException |
192
|
|
|
* @dataProvider compareFillProvider |
193
|
|
|
*/ |
194
|
|
|
public function testAlways($checkValue, bool $gotResult): void |
195
|
|
|
{ |
196
|
|
|
$data = new Rules\Always(); |
197
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
198
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
199
|
|
|
$this->expectException(RuleException::class); |
200
|
|
|
$data->validate($mock); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed $checkValue |
205
|
|
|
* @param bool $gotResult |
206
|
|
|
* @throws RuleException |
207
|
|
|
* @dataProvider compareFillProvider |
208
|
|
|
*/ |
209
|
|
|
public function testEmpty($checkValue, bool $gotResult): void |
210
|
|
|
{ |
211
|
|
|
$data = new Rules\IsEmpty(); |
212
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
213
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
214
|
|
|
if ($gotResult) $this->expectException(RuleException::class); |
215
|
|
|
$data->validate($mock); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param mixed $checkValue |
220
|
|
|
* @param bool $gotResult |
221
|
|
|
* @throws RuleException |
222
|
|
|
* @dataProvider compareFillProvider |
223
|
|
|
*/ |
224
|
|
|
public function testFilled($checkValue, bool $gotResult): void |
225
|
|
|
{ |
226
|
|
|
$data = new Rules\IsFilled(); |
227
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
228
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
229
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
230
|
|
|
$data->validate($mock); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function compareFillProvider(): array |
234
|
|
|
{ |
235
|
|
|
return [ |
236
|
|
|
[false, false], |
237
|
|
|
['', false], |
238
|
|
|
[123, true], |
239
|
|
|
['asdf', true], |
240
|
|
|
[MockEntry::init('foo', 'bar'), true], |
241
|
|
|
]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param mixed $checkValue |
246
|
|
|
* @param bool $gotResult |
247
|
|
|
* @throws RuleException |
248
|
|
|
* @dataProvider compareJsonProvider |
249
|
|
|
*/ |
250
|
|
|
public function testJson($checkValue, bool $gotResult): void |
251
|
|
|
{ |
252
|
|
|
$data = new Rules\IsJsonString(); |
253
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
254
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
255
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
256
|
|
|
$data->validate($mock); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function compareJsonProvider(): array |
260
|
|
|
{ |
261
|
|
|
return [ |
262
|
|
|
[false, false], |
263
|
|
|
['', false], |
264
|
|
|
[123, true], |
265
|
|
|
['asdf', false], |
266
|
|
|
[MockEntry::init('foo', 'bar'), false], |
267
|
|
|
['{}', true], |
268
|
|
|
['{"key": "value is more"}', true], |
269
|
|
|
]; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|