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
|
|
|
[[[1, 2], [3, 4]], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
65
|
|
|
[[[3, 4], [1, 2]], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
66
|
|
|
[[['1', 2], ['3', 4]], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
67
|
|
|
[[['1', '2'], ['3', '4']], [new Subset([[1, 2], [3, 4], [5, 6]])]], |
68
|
|
|
[ |
69
|
|
|
[ |
70
|
|
|
['data' => ['value' => [1, 2]]], |
71
|
|
|
['data' => ['value' => [3, 4]]], |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
new Subset([ |
75
|
|
|
['data' => ['value' => [1, 2]]], |
76
|
|
|
['data' => ['value' => [3, 4]]], |
77
|
|
|
['data' => ['value' => [5, 6]]], |
78
|
|
|
]), |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
[ |
82
|
|
|
[ |
83
|
|
|
[ |
84
|
|
|
'data2' => ['value2' => [7, 8], 'value1' => [5, 6]], |
85
|
|
|
'data1' => ['value2' => [3, 4], 'value1' => [1, 2]], |
86
|
|
|
], |
87
|
|
|
[ |
88
|
|
|
'data2' => ['value2' => [15, 16], 'value1' => [13, 14]], |
89
|
|
|
'data1' => ['value2' => [11, 12], 'value1' => [9, 10]], |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
[ |
93
|
|
|
new Subset([ |
94
|
|
|
[ |
95
|
|
|
'data1' => ['value1' => [1, 2], 'value2' => [3, 4]], |
96
|
|
|
'data2' => ['value1' => [5, 6], 'value2' => [7, 8]], |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
'data1' => ['value1' => [9, 10], 'value2' => [11, 12]], |
100
|
|
|
'data2' => ['value1' => [13, 14], 'value2' => [15, 16]], |
101
|
|
|
], |
102
|
|
|
]), |
103
|
|
|
], |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function dataValidationFailed(): array |
109
|
|
|
{ |
110
|
|
|
$errors = ['' => ['This value is not a subset of acceptable values.']]; |
111
|
|
|
|
112
|
|
|
return [ |
113
|
|
|
'non-iterable' => [ |
114
|
|
|
1, |
115
|
|
|
[new Subset([1, 2, 3])], |
116
|
|
|
['' => ['Value must be iterable.']], |
117
|
|
|
], |
118
|
|
|
'custom incorrect input message' => [ |
119
|
|
|
1, |
120
|
|
|
[new Subset([1, 2, 3], incorrectInputMessage: 'Custom non-iterable message.')], |
121
|
|
|
['' => ['Custom non-iterable message.']], |
122
|
|
|
], |
123
|
|
|
'custom incorrect input message with parameters' => [ |
124
|
|
|
1, |
125
|
|
|
[new Subset([1, 2, 3], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
126
|
|
|
['' => ['Attribute - , type - int.']], |
127
|
|
|
], |
128
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
129
|
|
|
['data' => 1], |
130
|
|
|
['data' => new Subset([1, 2, 3], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
131
|
|
|
['data' => ['Attribute - data, type - int.']], |
132
|
|
|
], |
133
|
|
|
[ |
134
|
|
|
[0, 1, 2], |
135
|
|
|
[new Subset(range(1, 10))], |
136
|
|
|
$errors, |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
[10, 11, 12], |
140
|
|
|
[new Subset(range(1, 10))], |
141
|
|
|
$errors, |
142
|
|
|
], |
143
|
|
|
'iterator as a value' => [ |
144
|
|
|
new SingleValueDataSet(new ArrayObject(['c', 'd'])), |
145
|
|
|
[new Subset(new ArrayObject(['a', 'b', 'c']))], |
146
|
|
|
$errors, |
147
|
|
|
], |
148
|
|
|
|
149
|
|
|
[[['1', 2], ['3', 4]], [new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], $errors], |
150
|
|
|
[[['1', '2'], ['3', '4']], [new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], $errors], |
151
|
|
|
[[[7, 8], [9, 10]], [new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], $errors], |
152
|
|
|
[[[2, 3], [4, 5]], [new Subset([[1, 2], [3, 4], [5, 6]], strict: true)], $errors], |
153
|
|
|
[ |
154
|
|
|
[ |
155
|
|
|
['data' => ['value' => [3, 4]]], |
156
|
|
|
['data' => ['value' => [5, 7]]], |
157
|
|
|
], |
158
|
|
|
[ |
159
|
|
|
new Subset([ |
160
|
|
|
['data' => ['value' => [1, 2]]], |
161
|
|
|
['data' => ['value' => [3, 4]]], |
162
|
|
|
['data' => ['value' => [5, 6]]], |
163
|
|
|
]), |
164
|
|
|
], |
165
|
|
|
$errors, |
166
|
|
|
], |
167
|
|
|
[ |
168
|
|
|
[ |
169
|
|
|
[ |
170
|
|
|
'data2' => ['value2' => [8, 7], 'value1' => [6, 5]], |
171
|
|
|
'data1' => ['value2' => [4, 3], 'value1' => [2, 1]], |
172
|
|
|
], |
173
|
|
|
[ |
174
|
|
|
'data2' => ['value2' => [16, 15], 'value1' => [14, 13]], |
175
|
|
|
'data1' => ['value2' => [12, 11], 'value1' => [10, 9]], |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
[ |
179
|
|
|
new Subset([ |
180
|
|
|
[ |
181
|
|
|
'data1' => ['value1' => [1, 2], 'value2' => [3, 4]], |
182
|
|
|
'data2' => ['value1' => [5, 6], 'value2' => [7, 8]], |
183
|
|
|
], |
184
|
|
|
[ |
185
|
|
|
'data1' => ['value1' => [9, 10], 'value2' => [11, 12]], |
186
|
|
|
'data2' => ['value1' => [13, 14], 'value2' => [15, 16]], |
187
|
|
|
], |
188
|
|
|
]), |
189
|
|
|
], |
190
|
|
|
$errors, |
191
|
|
|
], |
192
|
|
|
|
193
|
|
|
'custom message' => [ |
194
|
|
|
['' => ['c']], |
195
|
|
|
['' => new Subset(['a', 'b'], message: 'Custom message.')], |
196
|
|
|
['' => ['Custom message.']], |
197
|
|
|
], |
198
|
|
|
'custom message with parameters' => [ |
199
|
|
|
['' => ['c']], |
200
|
|
|
['' => new Subset(['a', 'b'], message: 'Attribute - {attribute}.')], |
201
|
|
|
['' => ['Attribute - .']], |
202
|
|
|
], |
203
|
|
|
'custom message with parameters, attribute set' => [ |
204
|
|
|
['data' => ['c']], |
205
|
|
|
['data' => new Subset(['a', 'b'], message: 'Attribute - {attribute}.')], |
206
|
|
|
['data' => ['Attribute - data.']], |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testSkipOnError(): void |
212
|
|
|
{ |
213
|
|
|
$this->testSkipOnErrorInternal(new Subset([]), new Subset([], skipOnError: true)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testWhen(): void |
217
|
|
|
{ |
218
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
219
|
|
|
$this->testWhenInternal(new Subset([]), new Subset([], when: $when)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
223
|
|
|
{ |
224
|
|
|
return [Subset::class, SubsetHandler::class]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|