| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use kalanis\kw_rules\Rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use kalanis\kw_rules\Interfaces\IRules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use kalanis\kw_rules\Exceptions\RuleException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class MatchRulesTest extends CommonTestClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function testNoMatchInitial(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $data = new Rules\MatchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $data->setAgainstValue('abc'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function testNoMatchStr(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $data = new Rules\MatchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $data->setAgainstValue(['abc', 'def', 'ghi', 'jkl']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function testNoMatchObj(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $data = new Rules\MatchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $data->setAgainstValue([MockEntry::init('abc', 'def'), MockEntry::init('ghi', 'jkl'), ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function testProcessErrors(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $data = new Rules\MatchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $data->setAgainstValue([$this->initDeny(), $this->initDeny()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $mock = MockEntry::init('foo', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $data->validate($mock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } catch (RuleException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             // no catch - no errors - be risky and that's okay here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $got = $ex->getPrev(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->assertNotEmpty($got); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $this->assertInstanceOf(RuleException::class, $got); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param IRules[] $expectedValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param bool $matchAll | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param bool $matchAny | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @dataProvider matchProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function testMatchAll(array $expectedValue, bool $matchAll, bool $matchAny): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $data = new Rules\MatchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $data->setAgainstValue($expectedValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $mock = MockEntry::init('foo', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (!$matchAll) $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $data->validate($mock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param IRules[] $expectedValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param bool $matchAll | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param bool $matchAny | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @throws RuleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @dataProvider matchProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function testMatchAny(array $expectedValue, bool $matchAll, bool $matchAny): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $data = new Rules\MatchAny(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $data->setAgainstValue($expectedValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $mock = MockEntry::init('foo', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (!$matchAny) $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $data->validate($mock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function matchProvider(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             [[$this->initPass(), $this->initPass()], true, true], // pass all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             [[$this->initPass(), $this->initDeny()], false, true], // pass partial | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             [[$this->initDeny(), $this->initDeny()], false, false], // pass nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function testMatchEntryFail1(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $data = new Rules\MatchByEntry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $data->setAgainstValue('just string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function testMatchEntryFail2(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $data = new Rules\MatchByEntry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $data->setAgainstValue(new \stdClass()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function testMatchEntryPass(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $data = new Rules\MatchByEntry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $mock1 = MockEntry::init('foo', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $mock1->addRule(IRules::IS_NOT_EMPTY, 'problems: none'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $data->setAgainstValue($mock1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $mock2 = MockEntry::init('baz', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $mock2->addRule(IRules::EQUALS, 'pass, because it is okay'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $data->validate($mock2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     public function testMatchEntrySub(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         $data = new Rules\MatchByEntry(); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         $this->assertInstanceOf(Rules\ARule::class, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         $mock1 = MockEntry::init('foo', 'bar'); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $mock1->addRule(IRules::IS_NOT_EMPTY, 'problems: none'); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         $data->setAgainstValue($mock1); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         $mock2 = MockEntry::init('baz', 'bar'); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         $mock2->addRule(IRules::EQUALS, 'pass, because it is okay'); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         $mock1->addRule(IRules::IS_NUMERIC, 'add something which make it fail'); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         $this->expectException(RuleException::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         $data->validate($mock2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     protected function initPass(): Rules\ProcessCallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $callback = new Rules\ProcessCallback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $callback->setAgainstValue('MatchRulesTest::callMePass'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public static function callMePass(...$args): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     protected function initDeny(): Rules\ProcessCallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $callback = new Rules\ProcessCallback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $callback->setAgainstValue('MatchRulesTest::callMeDeny'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public static function callMeDeny(...$args): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  |