1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use stdClass; |
9
|
|
|
use Yiisoft\Validator\DataSet\SingleValueDataSet; |
10
|
|
|
use Yiisoft\Validator\Rule\Count; |
11
|
|
|
use Yiisoft\Validator\Rule\CountHandler; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\LimitTestTrait; |
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
16
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
17
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
18
|
|
|
|
19
|
|
|
final class CountTest extends RuleTestCase |
20
|
|
|
{ |
21
|
|
|
use DifferentRuleInHandlerTestTrait; |
22
|
|
|
use LimitTestTrait; |
23
|
|
|
use RuleWithOptionsTestTrait; |
24
|
|
|
use SkipOnErrorTestTrait; |
25
|
|
|
use WhenTestTrait; |
26
|
|
|
|
27
|
|
|
public function testGetName(): void |
28
|
|
|
{ |
29
|
|
|
$rule = new Count(min: 3); |
30
|
|
|
$this->assertSame('count', $rule->getName()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function dataOptions(): array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[ |
37
|
|
|
new Count(min: 3), |
38
|
|
|
[ |
39
|
|
|
'min' => 3, |
40
|
|
|
'max' => null, |
41
|
|
|
'exactly' => null, |
42
|
|
|
'lessThanMinMessage' => [ |
43
|
|
|
'template' => 'This value must contain at least {min, number} {min, plural, one{item} ' . |
44
|
|
|
'other{items}}.', |
45
|
|
|
'parameters' => ['min' => 3], |
46
|
|
|
], |
47
|
|
|
'greaterThanMaxMessage' => [ |
48
|
|
|
'template' => 'This value must contain at most {max, number} {max, plural, one{item} ' . |
49
|
|
|
'other{items}}.', |
50
|
|
|
'parameters' => ['max' => null], |
51
|
|
|
], |
52
|
|
|
'notExactlyMessage' => [ |
53
|
|
|
'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' . |
54
|
|
|
'other{items}}.', |
55
|
|
|
'parameters' => ['exactly' => null], |
56
|
|
|
], |
57
|
|
|
'incorrectInputMessage' => [ |
58
|
|
|
'template' => 'This value must be an array or implement \Countable interface.', |
59
|
|
|
'parameters' => [], |
60
|
|
|
], |
61
|
|
|
'skipOnEmpty' => false, |
62
|
|
|
'skipOnError' => false, |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function dataValidationPassed(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
[[0, 0, 0], [new Count(min: 3)]], |
72
|
|
|
[[0, 0, 0, 0], [new Count(min: 3)]], |
73
|
|
|
[[0, 0, 0], [new Count(exactly: 3)]], |
74
|
|
|
[[], [new Count(max: 3)]], |
75
|
|
|
[[0, 0], [new Count(max: 3)]], |
76
|
|
|
[[0, 0, 0], [new Count(max: 3)]], |
77
|
|
|
[ |
78
|
|
|
new SingleValueDataSet( |
79
|
|
|
new class () implements Countable { |
80
|
|
|
protected int $myCount = 3; |
81
|
|
|
|
82
|
|
|
public function count(): int |
83
|
|
|
{ |
84
|
|
|
return $this->myCount; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
), |
88
|
|
|
[new Count(min: 3)], |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function dataValidationFailed(): array |
94
|
|
|
{ |
95
|
|
|
$lessThanMinmessage = 'This value must contain at least 3 items.'; |
96
|
|
|
$greaterThanMaxMessage = 'This value must contain at most 3 items.'; |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
'incorrect input' => [ |
100
|
|
|
1, |
101
|
|
|
[new Count(min: 3)], |
102
|
|
|
['' => ['This value must be an array or implement \Countable interface.']], |
103
|
|
|
], |
104
|
|
|
'custom incorrect input message' => [ |
105
|
|
|
1, |
106
|
|
|
[new Count(min: 3, incorrectInputMessage: 'Custom incorrect input message.')], |
107
|
|
|
['' => ['Custom incorrect input message.']], |
108
|
|
|
], |
109
|
|
|
'custom incorrect input message with parameters' => [ |
110
|
|
|
1, |
111
|
|
|
[new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
112
|
|
|
['' => ['Attribute - , type - int.']], |
113
|
|
|
], |
114
|
|
|
'custom incorrect input message, attribute set' => [ |
115
|
|
|
['data' => 1], |
116
|
|
|
['data' => new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
117
|
|
|
['data' => ['Attribute - data, type - int.']], |
118
|
|
|
], |
119
|
|
|
|
120
|
|
|
[[1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
121
|
|
|
[[], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
122
|
|
|
[[0, 0], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
123
|
|
|
[[1.1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
124
|
|
|
[[''], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
125
|
|
|
[['some string'], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
126
|
|
|
[[new stdClass()], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
127
|
|
|
// https://www.php.net/manual/ru/class.countable.php |
128
|
|
|
[ |
129
|
|
|
[ |
130
|
|
|
new class () { |
131
|
|
|
protected int $myCount = 3; |
132
|
|
|
|
133
|
|
|
public function count(): int |
134
|
|
|
{ |
135
|
|
|
return $this->myCount; |
136
|
|
|
} |
137
|
|
|
}, |
138
|
|
|
], |
139
|
|
|
[new Count(min: 3)], |
140
|
|
|
['' => [$lessThanMinmessage]], |
141
|
|
|
], |
142
|
|
|
[[0, 0, 0, 0], [new Count(max: 3)], ['' => [$greaterThanMaxMessage]]], |
143
|
|
|
|
144
|
|
|
'custom less than min message' => [ |
145
|
|
|
[0, 0], |
146
|
|
|
[new Count(min: 3, lessThanMinMessage: 'Custom less than min message.')], |
147
|
|
|
['' => ['Custom less than min message.']], |
148
|
|
|
], |
149
|
|
|
'custom less than min message with parameters' => [ |
150
|
|
|
[0, 0], |
151
|
|
|
[new Count(min: 3, lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.')], |
152
|
|
|
['' => ['Min - 3, attribute - , number - 2.']], |
153
|
|
|
], |
154
|
|
|
'custom less than min message with parameters, attribute set' => [ |
155
|
|
|
['data' => [0, 0]], |
156
|
|
|
[ |
157
|
|
|
'data' => new Count( |
158
|
|
|
min: 3, |
159
|
|
|
lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.', |
160
|
|
|
), |
161
|
|
|
], |
162
|
|
|
['data' => ['Min - 3, attribute - data, number - 2.']], |
163
|
|
|
], |
164
|
|
|
|
165
|
|
|
'custom greater than max message' => [ |
166
|
|
|
[0, 0, 0, 0], |
167
|
|
|
[new Count(max: 3, greaterThanMaxMessage: 'Custom greater than max message.')], |
168
|
|
|
['' => ['Custom greater than max message.']], |
169
|
|
|
], |
170
|
|
|
'custom greater than max message with parameters' => [ |
171
|
|
|
[0, 0, 0, 0], |
172
|
|
|
[new Count(max: 3, greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.')], |
173
|
|
|
['' => ['Max - 3, attribute - , number - 4.']], |
174
|
|
|
], |
175
|
|
|
'custom greater than max message with parameters, attribute set' => [ |
176
|
|
|
['data' => [0, 0, 0, 0]], |
177
|
|
|
[ |
178
|
|
|
'data' => new Count( |
179
|
|
|
max: 3, |
180
|
|
|
greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.', |
181
|
|
|
), |
182
|
|
|
], |
183
|
|
|
['data' => ['Max - 3, attribute - data, number - 4.']], |
184
|
|
|
], |
185
|
|
|
|
186
|
|
|
'custom not exactly message' => [ |
187
|
|
|
[0, 0, 0, 0], |
188
|
|
|
[new Count(exactly: 3, notExactlyMessage: 'Custom not exactly message.')], |
189
|
|
|
['' => ['Custom not exactly message.']], |
190
|
|
|
], |
191
|
|
|
'custom not exactly message with parameters' => [ |
192
|
|
|
[0, 0, 0, 0], |
193
|
|
|
[ |
194
|
|
|
new Count( |
195
|
|
|
exactly: 3, |
196
|
|
|
notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
197
|
|
|
), |
198
|
|
|
], |
199
|
|
|
['' => ['Exactly - 3, attribute - , number - 4.']], |
200
|
|
|
], |
201
|
|
|
'custom not exactly message with parameters, attribute set' => [ |
202
|
|
|
['data' => [0, 0, 0, 0]], |
203
|
|
|
[ |
204
|
|
|
'data' => new Count( |
205
|
|
|
exactly: 3, |
206
|
|
|
notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
207
|
|
|
), |
208
|
|
|
], |
209
|
|
|
['data' => ['Exactly - 3, attribute - data, number - 4.']], |
210
|
|
|
], |
211
|
|
|
]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testSkipOnError(): void |
215
|
|
|
{ |
216
|
|
|
$this->testSkipOnErrorInternal(new Count(min: 3), new Count(min: 3, skipOnError: true)); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testWhen(): void |
220
|
|
|
{ |
221
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
222
|
|
|
$this->testWhenInternal(new Count(min: 3), new Count(min: 3, when: $when)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
protected function getRuleClass(): string |
226
|
|
|
{ |
227
|
|
|
return Count::class; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
231
|
|
|
{ |
232
|
|
|
return [Count::class, CountHandler::class]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|