1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Rules; |
4
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CallbackRulesTest extends CommonTestClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param mixed $expectedCall |
11
|
|
|
* @param mixed $checkValue |
12
|
|
|
* @param bool $gotResult |
13
|
|
|
* @param bool $pass |
14
|
|
|
* @throws RuleException |
15
|
|
|
* @dataProvider callbackProvider |
16
|
|
|
*/ |
17
|
|
|
public function testCallback($expectedCall, $checkValue, bool $gotResult, bool $pass): void |
18
|
|
|
{ |
19
|
|
|
$data = new Rules\ProcessCallback(); |
20
|
|
|
$this->assertInstanceOf(Rules\ARule::class, $data); |
21
|
|
|
if (!$gotResult) $this->expectException(RuleException::class); |
22
|
|
|
$data->setAgainstValue($expectedCall); |
23
|
|
|
if ($gotResult) { |
24
|
|
|
$mock = MockEntry::init('foo', $checkValue); |
25
|
|
|
if (!$pass) $this->expectException(RuleException::class); |
26
|
|
|
$data->validate($mock); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function callbackProvider(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
[false, false, false, false], |
34
|
|
|
['', '', false, false], |
35
|
|
|
[123, '', false, false], |
36
|
|
|
['asdf', '', false, false], |
37
|
|
|
['CallbackRulesTest::callMeStatic', null, true, false], |
38
|
|
|
['CallbackRulesTest::callMeStatic', 1, true, true ], |
39
|
|
|
[[$this, 'callMeDynamic'], null, true, false], |
40
|
|
|
[[$this, 'callMeDynamic'], 1, true, true], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function callMeStatic($param): bool |
45
|
|
|
{ |
46
|
|
|
return !empty($param); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function callMeDynamic($param): bool |
50
|
|
|
{ |
51
|
|
|
return !empty($param); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|