1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Rule\CompareType; |
8
|
|
|
use Yiisoft\Validator\Rule\GreaterThanOrEqual; |
9
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
10
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
13
|
|
|
|
14
|
|
|
final class GreaterThanOrEqualTest extends RuleTestCase |
15
|
|
|
{ |
16
|
|
|
use RuleWithOptionsTestTrait; |
17
|
|
|
use SkipOnErrorTestTrait; |
18
|
|
|
use WhenTestTrait; |
19
|
|
|
|
20
|
|
|
public function testGetName(): void |
21
|
|
|
{ |
22
|
|
|
$rule = new GreaterThanOrEqual(1); |
23
|
|
|
$this->assertSame('greaterThanOrEqual', $rule->getName()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function dataOptions(): array |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
[ |
30
|
|
|
new GreaterThanOrEqual(1), |
31
|
|
|
[ |
32
|
|
|
'targetValue' => 1, |
33
|
|
|
'targetAttribute' => null, |
34
|
|
|
'incorrectInputMessage' => [ |
35
|
|
|
'template' => 'The allowed types are integer, float, string, boolean and null.', |
36
|
|
|
'parameters' => [ |
37
|
|
|
'targetValue' => 1, |
38
|
|
|
'targetAttribute' => null, |
39
|
|
|
'targetValueOrAttribute' => 1, |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
'incorrectDataSetTypeMessage' => [ |
43
|
|
|
'template' => 'The attribute value returned from a custom data set must have a scalar type.', |
44
|
|
|
'parameters' => [ |
45
|
|
|
'targetValue' => 1, |
46
|
|
|
'targetAttribute' => null, |
47
|
|
|
'targetValueOrAttribute' => 1, |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
'message' => [ |
51
|
|
|
'template' => 'Value must be greater than or equal to "{targetValueOrAttribute}".', |
52
|
|
|
'parameters' => [ |
53
|
|
|
'targetValue' => 1, |
54
|
|
|
'targetAttribute' => null, |
55
|
|
|
'targetValueOrAttribute' => 1, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
'type' => 'string', |
59
|
|
|
'operator' => '>=', |
60
|
|
|
'skipOnEmpty' => false, |
61
|
|
|
'skipOnError' => false, |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
new GreaterThanOrEqual(1, type: CompareType::NUMBER), |
66
|
|
|
[ |
67
|
|
|
'targetValue' => 1, |
68
|
|
|
'targetAttribute' => null, |
69
|
|
|
'incorrectInputMessage' => [ |
70
|
|
|
'template' => 'The allowed types are integer, float, string, boolean and null.', |
71
|
|
|
'parameters' => [ |
72
|
|
|
'targetValue' => 1, |
73
|
|
|
'targetAttribute' => null, |
74
|
|
|
'targetValueOrAttribute' => 1, |
75
|
|
|
], |
76
|
|
|
], |
77
|
|
|
'incorrectDataSetTypeMessage' => [ |
78
|
|
|
'template' => 'The attribute value returned from a custom data set must have a scalar type.', |
79
|
|
|
'parameters' => [ |
80
|
|
|
'targetValue' => 1, |
81
|
|
|
'targetAttribute' => null, |
82
|
|
|
'targetValueOrAttribute' => 1, |
83
|
|
|
], |
84
|
|
|
], |
85
|
|
|
'message' => [ |
86
|
|
|
'template' => 'Value must be greater than or equal to "{targetValueOrAttribute}".', |
87
|
|
|
'parameters' => [ |
88
|
|
|
'targetValue' => 1, |
89
|
|
|
'targetAttribute' => null, |
90
|
|
|
'targetValueOrAttribute' => 1, |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
'type' => 'number', |
94
|
|
|
'operator' => '>=', |
95
|
|
|
'skipOnEmpty' => false, |
96
|
|
|
'skipOnError' => false, |
97
|
|
|
], |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
new GreaterThanOrEqual(null, 'attribute'), |
101
|
|
|
[ |
102
|
|
|
'targetValue' => null, |
103
|
|
|
'targetAttribute' => 'attribute', |
104
|
|
|
'incorrectInputMessage' => [ |
105
|
|
|
'template' => 'The allowed types are integer, float, string, boolean and null.', |
106
|
|
|
'parameters' => [ |
107
|
|
|
'targetValue' => null, |
108
|
|
|
'targetAttribute' => 'attribute', |
109
|
|
|
'targetValueOrAttribute' => 'attribute', |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'incorrectDataSetTypeMessage' => [ |
113
|
|
|
'template' => 'The attribute value returned from a custom data set must have a scalar type.', |
114
|
|
|
'parameters' => [ |
115
|
|
|
'targetValue' => null, |
116
|
|
|
'targetAttribute' => 'attribute', |
117
|
|
|
'targetValueOrAttribute' => 'attribute', |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
'message' => [ |
121
|
|
|
'template' => 'Value must be greater than or equal to "{targetValueOrAttribute}".', |
122
|
|
|
'parameters' => [ |
123
|
|
|
'targetValue' => null, |
124
|
|
|
'targetAttribute' => 'attribute', |
125
|
|
|
'targetValueOrAttribute' => 'attribute', |
126
|
|
|
], |
127
|
|
|
], |
128
|
|
|
'type' => 'string', |
129
|
|
|
'operator' => '>=', |
130
|
|
|
'skipOnEmpty' => false, |
131
|
|
|
'skipOnError' => false, |
132
|
|
|
], |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
new GreaterThanOrEqual( |
136
|
|
|
targetAttribute: 'test', |
137
|
|
|
incorrectInputMessage: 'Custom message 1.', |
138
|
|
|
incorrectDataSetTypeMessage: 'Custom message 2.', |
139
|
|
|
message: 'Custom message 3.', |
140
|
|
|
), |
141
|
|
|
[ |
142
|
|
|
'targetValue' => null, |
143
|
|
|
'targetAttribute' => 'test', |
144
|
|
|
'incorrectInputMessage' => [ |
145
|
|
|
'template' => 'Custom message 1.', |
146
|
|
|
'parameters' => [ |
147
|
|
|
'targetValue' => null, |
148
|
|
|
'targetAttribute' => 'test', |
149
|
|
|
'targetValueOrAttribute' => 'test', |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
'incorrectDataSetTypeMessage' => [ |
153
|
|
|
'template' => 'Custom message 2.', |
154
|
|
|
'parameters' => [ |
155
|
|
|
'targetValue' => null, |
156
|
|
|
'targetAttribute' => 'test', |
157
|
|
|
'targetValueOrAttribute' => 'test', |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
'message' => [ |
161
|
|
|
'template' => 'Custom message 3.', |
162
|
|
|
'parameters' => [ |
163
|
|
|
'targetValue' => null, |
164
|
|
|
'targetAttribute' => 'test', |
165
|
|
|
'targetValueOrAttribute' => 'test', |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
'type' => 'string', |
169
|
|
|
'operator' => '>=', |
170
|
|
|
'skipOnEmpty' => false, |
171
|
|
|
'skipOnError' => false, |
172
|
|
|
], |
173
|
|
|
], |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function dataValidationPassed(): array |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
|
|
[100, [new GreaterThanOrEqual(99)]], |
181
|
|
|
['100', [new GreaterThanOrEqual('100')]], |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function dataValidationFailed(): array |
186
|
|
|
{ |
187
|
|
|
$message = 'Value must be greater than or equal to "100".'; |
188
|
|
|
|
189
|
|
|
return [ |
190
|
|
|
[99, [new GreaterThanOrEqual(100)], ['' => [$message]]], |
191
|
|
|
['99', [new GreaterThanOrEqual(100)], ['' => [$message]]], |
192
|
|
|
'custom error' => [99, [new GreaterThanOrEqual(100, message: 'Custom error')], ['' => ['Custom error']]], |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testSkipOnError(): void |
197
|
|
|
{ |
198
|
|
|
$this->testSkipOnErrorInternal(new GreaterThanOrEqual(1), new GreaterThanOrEqual(1, skipOnError: true)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testWhen(): void |
202
|
|
|
{ |
203
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
204
|
|
|
$this->testWhenInternal(new GreaterThanOrEqual(1), new GreaterThanOrEqual(1, when: $when)); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|