1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use stdClass; |
8
|
|
|
use Yiisoft\Validator\DataSet\SingleValueDataSet; |
9
|
|
|
use Yiisoft\Validator\Rule\HasLength; |
10
|
|
|
use Yiisoft\Validator\Rule\HasLengthHandler; |
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\LimitTestTrait; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
16
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
17
|
|
|
|
18
|
|
|
final class HasLengthTest extends RuleTestCase |
19
|
|
|
{ |
20
|
|
|
use DifferentRuleInHandlerTestTrait; |
21
|
|
|
use LimitTestTrait; |
22
|
|
|
use RuleWithOptionsTestTrait; |
23
|
|
|
use SkipOnErrorTestTrait; |
24
|
|
|
use WhenTestTrait; |
25
|
|
|
|
26
|
|
|
public function testGetName(): void |
27
|
|
|
{ |
28
|
|
|
$rule = new HasLength(min: 3); |
29
|
|
|
$this->assertSame('hasLength', $rule->getName()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function dataOptions(): array |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
[ |
36
|
|
|
new HasLength(min: 3), |
37
|
|
|
[ |
38
|
|
|
'min' => 3, |
39
|
|
|
'max' => null, |
40
|
|
|
'exactly' => null, |
41
|
|
|
'lessThanMinMessage' => [ |
42
|
|
|
'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.', |
43
|
|
|
'parameters' => ['min' => 3], |
44
|
|
|
], |
45
|
|
|
'greaterThanMaxMessage' => [ |
46
|
|
|
'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.', |
47
|
|
|
'parameters' => ['max' => null], |
48
|
|
|
], |
49
|
|
|
'notExactlyMessage' => [ |
50
|
|
|
'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.', |
51
|
|
|
'parameters' => ['exactly' => null], |
52
|
|
|
], |
53
|
|
|
'incorrectInputMessage' => [ |
54
|
|
|
'template' => 'This value must be a string.', |
55
|
|
|
'parameters' => [], |
56
|
|
|
], |
57
|
|
|
'encoding' => 'UTF-8', |
58
|
|
|
'skipOnEmpty' => false, |
59
|
|
|
'skipOnError' => false, |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
new HasLength(max: 3), |
64
|
|
|
[ |
65
|
|
|
'min' => null, |
66
|
|
|
'max' => 3, |
67
|
|
|
'exactly' => null, |
68
|
|
|
'lessThanMinMessage' => [ |
69
|
|
|
'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.', |
70
|
|
|
'parameters' => ['min' => null], |
71
|
|
|
], |
72
|
|
|
'greaterThanMaxMessage' => [ |
73
|
|
|
'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.', |
74
|
|
|
'parameters' => ['max' => 3], |
75
|
|
|
], |
76
|
|
|
'notExactlyMessage' => [ |
77
|
|
|
'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.', |
78
|
|
|
'parameters' => ['exactly' => null], |
79
|
|
|
], |
80
|
|
|
'incorrectInputMessage' => [ |
81
|
|
|
'template' => 'This value must be a string.', |
82
|
|
|
'parameters' => [], |
83
|
|
|
], |
84
|
|
|
'encoding' => 'UTF-8', |
85
|
|
|
'skipOnEmpty' => false, |
86
|
|
|
'skipOnError' => false, |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
new HasLength(min: 3, max: 4, encoding: 'windows-1251'), |
91
|
|
|
[ |
92
|
|
|
'min' => 3, |
93
|
|
|
'max' => 4, |
94
|
|
|
'exactly' => null, |
95
|
|
|
'lessThanMinMessage' => [ |
96
|
|
|
'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.', |
97
|
|
|
'parameters' => ['min' => 3], |
98
|
|
|
], |
99
|
|
|
'greaterThanMaxMessage' => [ |
100
|
|
|
'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.', |
101
|
|
|
'parameters' => ['max' => 4], |
102
|
|
|
], |
103
|
|
|
'notExactlyMessage' => [ |
104
|
|
|
'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.', |
105
|
|
|
'parameters' => ['exactly' => null], |
106
|
|
|
], |
107
|
|
|
'incorrectInputMessage' => [ |
108
|
|
|
'template' => 'This value must be a string.', |
109
|
|
|
'parameters' => [], |
110
|
|
|
], |
111
|
|
|
'encoding' => 'windows-1251', |
112
|
|
|
'skipOnEmpty' => false, |
113
|
|
|
'skipOnError' => false, |
114
|
|
|
], |
115
|
|
|
], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function dataValidationPassed(): array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
[str_repeat('x', 25), [new HasLength(exactly: 25)]], |
123
|
|
|
[str_repeat('€', 25), [new HasLength(exactly: 25)]], |
124
|
|
|
|
125
|
|
|
[str_repeat('x', 125), [new HasLength(min: 25)]], |
126
|
|
|
[str_repeat('€', 25), [new HasLength(min: 25)]], |
127
|
|
|
|
128
|
|
|
[str_repeat('x', 25), [new HasLength(max: 25)]], |
129
|
|
|
[str_repeat('Ä', 24), [new HasLength(max: 25)]], |
130
|
|
|
['', [new HasLength(max: 25)]], |
131
|
|
|
|
132
|
|
|
[str_repeat('x', 15), [new HasLength(min: 10, max: 25)]], |
133
|
|
|
[str_repeat('x', 10), [new HasLength(min: 10, max: 25)]], |
134
|
|
|
[str_repeat('x', 20), [new HasLength(min: 10, max: 25)]], |
135
|
|
|
[str_repeat('x', 25), [new HasLength(min: 10, max: 25)]], |
136
|
|
|
|
137
|
|
|
[str_repeat('x', 5), [new HasLength(min: 1)]], |
138
|
|
|
[str_repeat('x', 5), [new HasLength(max: 100)]], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function dataValidationFailed(): array |
143
|
|
|
{ |
144
|
|
|
$incorrectInputMessage = 'This value must be a string.'; |
145
|
|
|
$greaterThanMaxMessage = 'This value must contain at most 25 characters.'; |
146
|
|
|
$notExactlyMessage = 'This value must contain exactly 25 characters.'; |
147
|
|
|
$lessThanMinMessage = 'This value must contain at least 25 characters.'; |
148
|
|
|
|
149
|
|
|
return [ |
150
|
|
|
'incorrect input, array' => [['not a string'], [new HasLength(min: 25)], ['' => [$incorrectInputMessage]]], |
151
|
|
|
'incorrect input, boolean (true)' => [true, [new HasLength(min: 25)], ['' => [$incorrectInputMessage]]], |
152
|
|
|
'incorrect input, boolean (false)' => [false, [new HasLength(min: 25)], ['' => [$incorrectInputMessage]]], |
153
|
|
|
'custom incorrect input message' => [ |
154
|
|
|
false, |
155
|
|
|
[new HasLength(min: 25, incorrectInputMessage: 'Custom incorrect input message.')], |
156
|
|
|
['' => ['Custom incorrect input message.']], |
157
|
|
|
], |
158
|
|
|
'custom incorrect input message with parameters' => [ |
159
|
|
|
false, |
160
|
|
|
[new HasLength(min: 25, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
161
|
|
|
['' => ['Attribute - , type - bool.']], |
162
|
|
|
], |
163
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
164
|
|
|
['data' => false], |
165
|
|
|
['data' => new HasLength(min: 25, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
166
|
|
|
['data' => ['Attribute - data, type - bool.']], |
167
|
|
|
], |
168
|
|
|
|
169
|
|
|
[new SingleValueDataSet(new stdClass()), [new HasLength(min: 25)], ['' => [$incorrectInputMessage]]], |
170
|
|
|
|
171
|
|
|
[str_repeat('x', 1250), [new HasLength(max: 25)], ['' => [$greaterThanMaxMessage]]], |
172
|
|
|
[str_repeat('x', 125), [new HasLength(exactly: 25)], ['' => [$notExactlyMessage]]], |
173
|
|
|
|
174
|
|
|
['', [new HasLength(exactly: 25)], ['' => [$notExactlyMessage]]], |
175
|
|
|
[ |
176
|
|
|
str_repeat('x', 5), |
177
|
|
|
[new HasLength(min: 10, max: 25)], |
178
|
|
|
['' => ['This value must contain at least 10 characters.']], |
179
|
|
|
], |
180
|
|
|
[str_repeat('x', 13), [new HasLength(min: 25)], ['' => [$lessThanMinMessage]]], |
181
|
|
|
['', [new HasLength(min: 25)], ['' => [$lessThanMinMessage]]], |
182
|
|
|
|
183
|
|
|
'custom less than min message' => [ |
184
|
|
|
'ab', |
185
|
|
|
[new HasLength(min: 3, lessThanMinMessage: 'Custom less than min message.')], |
186
|
|
|
['' => ['Custom less than min message.']], |
187
|
|
|
], |
188
|
|
|
'custom less than min message with parameters' => [ |
189
|
|
|
'ab', |
190
|
|
|
[new HasLength(min: 3, lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.')], |
191
|
|
|
['' => ['Min - 3, attribute - , number - 2.']], |
192
|
|
|
], |
193
|
|
|
'custom less than min message with parameters, attribute set' => [ |
194
|
|
|
['data' => 'ab'], |
195
|
|
|
[ |
196
|
|
|
'data' => new HasLength( |
197
|
|
|
min: 3, |
198
|
|
|
lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.', |
199
|
|
|
), |
200
|
|
|
], |
201
|
|
|
['data' => ['Min - 3, attribute - data, number - 2.']], |
202
|
|
|
], |
203
|
|
|
|
204
|
|
|
'custom greater than max message' => [ |
205
|
|
|
'abcd', |
206
|
|
|
[new HasLength(max: 3, greaterThanMaxMessage: 'Custom greater than max message.')], |
207
|
|
|
['' => ['Custom greater than max message.']], |
208
|
|
|
], |
209
|
|
|
'custom greater than max message with parameters' => [ |
210
|
|
|
'abcd', |
211
|
|
|
[ |
212
|
|
|
new HasLength( |
213
|
|
|
max: 3, |
214
|
|
|
greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.', |
215
|
|
|
), |
216
|
|
|
], |
217
|
|
|
['' => ['Max - 3, attribute - , number - 4.']], |
218
|
|
|
], |
219
|
|
|
'custom greater than max message with parameters, attribute set' => [ |
220
|
|
|
['data' => 'abcd'], |
221
|
|
|
[ |
222
|
|
|
'data' => new HasLength( |
223
|
|
|
max: 3, |
224
|
|
|
greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.', |
225
|
|
|
), |
226
|
|
|
], |
227
|
|
|
['data' => ['Max - 3, attribute - data, number - 4.']], |
228
|
|
|
], |
229
|
|
|
|
230
|
|
|
'custom not exactly message' => [ |
231
|
|
|
'abcd', |
232
|
|
|
[new HasLength(exactly: 3, notExactlyMessage: 'Custom not exactly message.')], |
233
|
|
|
['' => ['Custom not exactly message.']], |
234
|
|
|
], |
235
|
|
|
'custom not exactly message with parameters' => [ |
236
|
|
|
'abcd', |
237
|
|
|
[ |
238
|
|
|
new HasLength( |
239
|
|
|
exactly: 3, |
240
|
|
|
notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
241
|
|
|
), |
242
|
|
|
], |
243
|
|
|
['' => ['Exactly - 3, attribute - , number - 4.']], |
244
|
|
|
], |
245
|
|
|
'custom not exactly message with parameters, attribute set' => [ |
246
|
|
|
['data' => 'abcd'], |
247
|
|
|
[ |
248
|
|
|
'data' => new HasLength( |
249
|
|
|
exactly: 3, |
250
|
|
|
notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
251
|
|
|
), |
252
|
|
|
], |
253
|
|
|
['data' => ['Exactly - 3, attribute - data, number - 4.']], |
254
|
|
|
], |
255
|
|
|
]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testSkipOnError(): void |
259
|
|
|
{ |
260
|
|
|
$this->testSkipOnErrorInternal(new HasLength(min: 3), new HasLength(min: 3, skipOnError: true)); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testWhen(): void |
264
|
|
|
{ |
265
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
266
|
|
|
$this->testWhenInternal(new HasLength(min: 3), new HasLength(min: 3, when: $when)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
270
|
|
|
{ |
271
|
|
|
return [HasLength::class, HasLengthHandler::class]; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
protected function getRuleClass(): string |
275
|
|
|
{ |
276
|
|
|
return HasLength::class; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|