| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Tests\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Validator\Rule\Regex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Validator\Rule\RegexHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | final class RegexTest extends RuleTestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     use DifferentRuleInHandlerTestTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     use RuleWithOptionsTestTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     use SkipOnErrorTestTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     use WhenTestTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function testGetName(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $rule = new Regex('//'); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->assertSame('regex', $rule->getName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function dataOptions(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 new Regex('//'), | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                     'pattern' => '//', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                     'not' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                     'incorrectInputMessage' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                         'template' => 'Value should be string.', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                         'parameters' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     'message' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                         'template' => 'Value is invalid.', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                         'parameters' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                     'skipOnEmpty' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     'skipOnError' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 new Regex('//', not: true), | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                     'pattern' => '//', | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     'not' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     'incorrectInputMessage' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                         'template' => 'Value should be string.', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                         'parameters' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     'message' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                         'template' => 'Value is invalid.', | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                         'parameters' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     'skipOnEmpty' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     'skipOnError' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     public function dataValidationPassed(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             ['a', [new Regex('/a/')]], | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ['ab', [new Regex('/a/')]], | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ['b', [new Regex('/a/', not: true)]], | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     public function dataValidationFailed(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $incorrectInputMessage = 'Value should be string.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $message = 'Value is invalid.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             [['a', 'b'], [new Regex('/a/')], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             [['a', 'b'], [new Regex('/a/', not: true)], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             [null, [new Regex('/a/')], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             [null, [new Regex('/a/', not: true)], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             [new stdClass(), [new Regex('/a/')], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             [new stdClass(), [new Regex('/a/', not: true)], ['' => [$incorrectInputMessage]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             'custom incorrect input message' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 null, | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 [new Regex('/a/', incorrectInputMessage: 'Custom incorrect input message.')], | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 ['' => ['Custom incorrect input message.']], | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             'custom incorrect input message with parameters' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 null, | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 [new Regex('/a/', incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 ['' => ['Attribute - , type - null.']], | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             'custom incorrect input message with parameters, attribute set' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 ['data' => null], | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 ['data' => new Regex('/a/', incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 ['data' => ['Attribute - data, type - null.']], | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             ['b', [new Regex('/a/')], ['' => [$message]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             'custom message' => ['b', [new Regex('/a/', message: 'Custom message.')], ['' => ['Custom message.']]], | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             'custom message with parameters' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 'b', | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 [new Regex('/a/', message: 'Attribute - {attribute}, value - {value}.')], | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 ['' => ['Attribute - , value - b.']], | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             'custom message with parameters, attribute set' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 ['data' => 'b'], | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 ['data' => new Regex('/a/', message: 'Attribute - {attribute}, value - {value}.')], | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 ['data' => ['Attribute - data, value - b.']], | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function testSkipOnError(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |         $this->testSkipOnErrorInternal(new Regex('//'), new Regex('//', skipOnError: true)); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     public function testWhen(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         $when = static fn (mixed $value): bool => $value !== null; | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         $this->testWhenInternal(new Regex('//'), new Regex('//', when: $when)); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function getDifferentRuleInHandlerItems(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |         return [Regex::class, RegexHandler::class]; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 138 |  |  |  |