1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
4
|
|
|
use kalanis\kw_rules\Interfaces; |
5
|
|
|
use kalanis\kw_rules\Validate; |
6
|
|
|
use kalanis\kw_rules\SubRules; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class ValidateTest extends CommonTestClass |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @throws RuleException |
13
|
|
|
*/ |
14
|
|
|
public function testSimple(): void |
15
|
|
|
{ |
16
|
|
|
$entry = MockEntry::init('foo', 'bar'); |
17
|
|
|
$this->assertEmpty($entry->getRules()); |
18
|
|
|
$entry->addRule(Interfaces\IRules::IS_FILLED, 'Not filled'); |
19
|
|
|
$entry->addRule(Interfaces\IRules::IS_STRING, 'Not string'); |
20
|
|
|
$validate = new Validate(); |
21
|
|
|
$this->assertTrue($validate->validate($entry)); |
22
|
|
|
$this->assertEmpty($validate->getErrors()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws RuleException |
27
|
|
|
*/ |
28
|
|
|
public function testFailed(): void |
29
|
|
|
{ |
30
|
|
|
$entry = MockEntry::init('baz', 0); |
31
|
|
|
$this->assertEmpty($entry->getRules()); |
32
|
|
|
$entry->addRule(Interfaces\IRules::IS_FILLED, 'Not filled'); |
33
|
|
|
$entry->addRule(Interfaces\IRules::IS_STRING, 'Not string'); |
34
|
|
|
$validate = new Validate(); |
35
|
|
|
$this->assertFalse($validate->validate($entry)); |
36
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws RuleException |
41
|
|
|
*/ |
42
|
|
|
public function testOr(): void |
43
|
|
|
{ |
44
|
|
|
$entry = MockEntry::init('vfr', 75); |
45
|
|
|
$subRules = new SubRules(); |
46
|
|
|
$subRules->addRule(Interfaces\IRules::IS_STRING, 'Not string'); |
47
|
|
|
$subRules->addRule(Interfaces\IRules::IS_NUMERIC, 'Not number'); |
48
|
|
|
$entry->addRule(Interfaces\IRules::IS_FILLED, 'Not filled'); |
49
|
|
|
$entry->addRule(Interfaces\IRules::MATCH_ANY, 'Must be following', $subRules->getRules()); |
50
|
|
|
$validate = new Validate(); |
51
|
|
|
$this->assertTrue($validate->validate($entry)); |
52
|
|
|
$this->assertEmpty($validate->getErrors()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws RuleException |
57
|
|
|
*/ |
58
|
|
|
public function testOrFail(): void |
59
|
|
|
{ |
60
|
|
|
$entry = MockEntry::init('vfr', 75); |
61
|
|
|
$this->assertEmpty($entry->getRules()); |
62
|
|
|
$entry->addRule(Interfaces\IRules::IS_STRING, 'Not string'); |
63
|
|
|
$entry->addRule(Interfaces\IRules::IS_BOOL, 'Not boolean'); |
64
|
|
|
$presetRules = $entry->getRules(); |
65
|
|
|
$entry->removeRules(); |
66
|
|
|
$entry->addRule(Interfaces\IRules::IS_FILLED, 'Not filled'); |
67
|
|
|
$entry->addRule(Interfaces\IRules::MATCH_ANY, 'Must be following', $presetRules); |
68
|
|
|
$presetRules = $entry->getRules(); |
69
|
|
|
$entry->removeRules(); |
70
|
|
|
$this->assertEmpty($entry->getRules()); |
71
|
|
|
$entry->addRules($presetRules); |
72
|
|
|
$validate = new Validate(); |
73
|
|
|
$this->assertFalse($validate->validate($entry)); |
74
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws RuleException |
79
|
|
|
*/ |
80
|
|
|
public function testAddFile(): void |
81
|
|
|
{ |
82
|
|
|
$entry = $this->getMockNoFile(); |
83
|
|
|
$this->assertEmpty($entry->getRules()); |
84
|
|
|
$entry->addRule(Interfaces\IRules::FILE_RECEIVED, 'Must be received'); |
85
|
|
|
$entry->addRule(Interfaces\IRules::FILE_SENT, 'Must be sent'); |
86
|
|
|
$presetRules = $entry->getRules(); |
87
|
|
|
$entry->removeRules(); |
88
|
|
|
$this->assertEmpty($entry->getRules()); |
89
|
|
|
$entry->addRules($presetRules); |
90
|
|
|
$validate = new Validate(); |
91
|
|
|
$this->assertFalse($validate->validate($entry)); |
92
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|