1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Yiisoft\Validator\DataSet\SingleValueDataSet; |
9
|
|
|
use Yiisoft\Validator\Rule\Subset; |
10
|
|
|
use Yiisoft\Validator\Rule\SubsetHandler; |
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 SubsetTest extends RuleTestCase |
18
|
|
|
{ |
19
|
|
|
use DifferentRuleInHandlerTestTrait; |
20
|
|
|
use RuleWithOptionsTestTrait; |
21
|
|
|
use SkipOnErrorTestTrait; |
22
|
|
|
use WhenTestTrait; |
23
|
|
|
|
24
|
|
|
public function testGetName(): void |
25
|
|
|
{ |
26
|
|
|
$rule = new Subset([]); |
27
|
|
|
$this->assertSame('subset', $rule->getName()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function dataOptions(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
[ |
34
|
|
|
new Subset([]), |
35
|
|
|
[ |
36
|
|
|
'values' => [], |
37
|
|
|
'strict' => false, |
38
|
|
|
'incorrectInputMessage' => [ |
39
|
|
|
'template' => 'Value must be iterable.', |
40
|
|
|
'parameters' => [], |
41
|
|
|
], |
42
|
|
|
'message' => [ |
43
|
|
|
'template' => 'This value is not a subset of acceptable values.', |
44
|
|
|
'parameters' => [], |
45
|
|
|
], |
46
|
|
|
'skipOnEmpty' => false, |
47
|
|
|
'skipOnError' => false, |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function dataValidationPassed(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
[[], [new Subset(range(1, 10))]], |
57
|
|
|
[[1, 2, 3, 4, 5], [new Subset(range(1, 10))]], |
58
|
|
|
[[6, 7, 8, 9, 10], [new Subset(range(1, 10))]], |
59
|
|
|
[['1', '2', '3', 4, 5, 6], [new Subset(range(1, 10))]], |
60
|
|
|
|
61
|
|
|
[['a', 'b'], [new Subset(['a', 'b', 'c'])]], |
62
|
|
|
[new SingleValueDataSet(new ArrayObject(['a', 'b'])), [new Subset(new ArrayObject(['a', 'b', 'c']))]], |
63
|
|
|
|
64
|
|
|
'arrays, simple case' => [[[1, 2], [3, 4]], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
65
|
|
|
'arrays, reversed order' => [[[3, 4], [1, 2]], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
66
|
|
|
'arrays, non-strict equality (partially), non-strict mode' => [ |
67
|
|
|
[['1', 2], ['3', 4]], |
68
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]])], |
69
|
|
|
], |
70
|
|
|
'arrays, non-strict equality (fully), non-strict mode' => [ |
71
|
|
|
[['1', '2'], ['3', '4']], |
72
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]])], |
73
|
|
|
], |
74
|
|
|
'arrays, nested' => [ |
75
|
|
|
[ |
76
|
|
|
['data' => ['value' => [1, 2]]], |
77
|
|
|
['data' => ['value' => [3, 4]]], |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
new Subset([ |
81
|
|
|
['data' => ['value' => [1, 2]]], |
82
|
|
|
['data' => ['value' => [3, 4]]], |
83
|
|
|
['data' => ['value' => [5, 6]]], |
84
|
|
|
]), |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
'arrays, nested, different keys order' => [ |
88
|
|
|
[ |
89
|
|
|
[ |
90
|
|
|
'data2' => ['value2' => [7, 8], 'value1' => [5, 6]], |
91
|
|
|
'data1' => ['value2' => [3, 4], 'value1' => [1, 2]], |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
'data2' => ['value2' => [15, 16], 'value1' => [13, 14]], |
95
|
|
|
'data1' => ['value2' => [11, 12], 'value1' => [9, 10]], |
96
|
|
|
], |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
new Subset([ |
100
|
|
|
[ |
101
|
|
|
'data1' => ['value1' => [1, 2], 'value2' => [3, 4]], |
102
|
|
|
'data2' => ['value1' => [5, 6], 'value2' => [7, 8]], |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
'data1' => ['value1' => [9, 10], 'value2' => [11, 12]], |
106
|
|
|
'data2' => ['value1' => [13, 14], 'value2' => [15, 16]], |
107
|
|
|
], |
108
|
|
|
]), |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function dataValidationFailed(): array |
115
|
|
|
{ |
116
|
|
|
$errors = ['' => ['This value is not a subset of acceptable values.']]; |
117
|
|
|
|
118
|
|
|
return [ |
119
|
|
|
'non-iterable' => [ |
120
|
|
|
1, |
121
|
|
|
[new Subset([1, 2, 3])], |
122
|
|
|
['' => ['Value must be iterable.']], |
123
|
|
|
], |
124
|
|
|
'custom incorrect input message' => [ |
125
|
|
|
1, |
126
|
|
|
[new Subset([1, 2, 3], incorrectInputMessage: 'Custom non-iterable message.')], |
127
|
|
|
['' => ['Custom non-iterable message.']], |
128
|
|
|
], |
129
|
|
|
'custom incorrect input message with parameters' => [ |
130
|
|
|
1, |
131
|
|
|
[new Subset([1, 2, 3], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
132
|
|
|
['' => ['Attribute - , type - int.']], |
133
|
|
|
], |
134
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
135
|
|
|
['data' => 1], |
136
|
|
|
['data' => new Subset([1, 2, 3], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
137
|
|
|
['data' => ['Attribute - data, type - int.']], |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
[0, 1, 2], |
141
|
|
|
[new Subset(range(1, 10))], |
142
|
|
|
$errors, |
143
|
|
|
], |
144
|
|
|
[ |
145
|
|
|
[10, 11, 12], |
146
|
|
|
[new Subset(range(1, 10))], |
147
|
|
|
$errors, |
148
|
|
|
], |
149
|
|
|
'iterator as a value' => [ |
150
|
|
|
new SingleValueDataSet(new ArrayObject(['c', 'd'])), |
151
|
|
|
[new Subset(new ArrayObject(['a', 'b', 'c']))], |
152
|
|
|
$errors, |
153
|
|
|
], |
154
|
|
|
|
155
|
|
|
'arrays, non-strict equality (partially), strict mode' => [ |
156
|
|
|
[['1', 2], ['3', 4]], |
157
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], |
158
|
|
|
$errors, |
159
|
|
|
], |
160
|
|
|
'arrays, non-strict equality (fully), strict mode' => [ |
161
|
|
|
[['1', '2'], ['3', '4']], |
162
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], |
163
|
|
|
$errors, |
164
|
|
|
], |
165
|
|
|
'arrays, items are not from acceptable list' => [ |
166
|
|
|
[[7, 8], [9, 10]], |
167
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], |
168
|
|
|
$errors, |
169
|
|
|
], |
170
|
|
|
'arrays, items from acceptable list but not as units' => [ |
171
|
|
|
[[2, 3], [4, 5]], |
172
|
|
|
[new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], |
173
|
|
|
$errors, |
174
|
|
|
], |
175
|
|
|
'arrays, nested, not all items are from acceptable list' => [ |
176
|
|
|
[ |
177
|
|
|
['data' => ['value' => [3, 4]]], |
178
|
|
|
['data' => ['value' => [5, 7]]], |
179
|
|
|
], |
180
|
|
|
[ |
181
|
|
|
new Subset([ |
182
|
|
|
['data' => ['value' => [1, 2]]], |
183
|
|
|
['data' => ['value' => [3, 4]]], |
184
|
|
|
['data' => ['value' => [5, 6]]], |
185
|
|
|
]), |
186
|
|
|
], |
187
|
|
|
$errors, |
188
|
|
|
], |
189
|
|
|
'arrays, nested, reversed order of values in lists' => [ |
190
|
|
|
[ |
191
|
|
|
[ |
192
|
|
|
'data2' => ['value2' => [8, 7], 'value1' => [6, 5]], |
193
|
|
|
'data1' => ['value2' => [4, 3], 'value1' => [2, 1]], |
194
|
|
|
], |
195
|
|
|
[ |
196
|
|
|
'data2' => ['value2' => [16, 15], 'value1' => [14, 13]], |
197
|
|
|
'data1' => ['value2' => [12, 11], 'value1' => [10, 9]], |
198
|
|
|
], |
199
|
|
|
], |
200
|
|
|
[ |
201
|
|
|
new Subset([ |
202
|
|
|
[ |
203
|
|
|
'data1' => ['value1' => [1, 2], 'value2' => [3, 4]], |
204
|
|
|
'data2' => ['value1' => [5, 6], 'value2' => [7, 8]], |
205
|
|
|
], |
206
|
|
|
[ |
207
|
|
|
'data1' => ['value1' => [9, 10], 'value2' => [11, 12]], |
208
|
|
|
'data2' => ['value1' => [13, 14], 'value2' => [15, 16]], |
209
|
|
|
], |
210
|
|
|
]), |
211
|
|
|
], |
212
|
|
|
$errors, |
213
|
|
|
], |
214
|
|
|
|
215
|
|
|
'custom message' => [ |
216
|
|
|
['' => ['c']], |
217
|
|
|
['' => new Subset(['a', 'b'], message: 'Custom message.')], |
218
|
|
|
['' => ['Custom message.']], |
219
|
|
|
], |
220
|
|
|
'custom message with parameters' => [ |
221
|
|
|
['' => ['c']], |
222
|
|
|
['' => new Subset(['a', 'b'], message: 'Attribute - {attribute}.')], |
223
|
|
|
['' => ['Attribute - .']], |
224
|
|
|
], |
225
|
|
|
'custom message with parameters, attribute set' => [ |
226
|
|
|
['data' => ['c']], |
227
|
|
|
['data' => new Subset(['a', 'b'], message: 'Attribute - {attribute}.')], |
228
|
|
|
['data' => ['Attribute - data.']], |
229
|
|
|
], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testSkipOnError(): void |
234
|
|
|
{ |
235
|
|
|
$this->testSkipOnErrorInternal(new Subset([]), new Subset([], skipOnError: true)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testWhen(): void |
239
|
|
|
{ |
240
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
241
|
|
|
$this->testWhenInternal(new Subset([]), new Subset([], when: $when)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
245
|
|
|
{ |
246
|
|
|
return [Subset::class, SubsetHandler::class]; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|