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