|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Validator\EmptyCondition\NeverEmpty; |
|
8
|
|
|
use Yiisoft\Validator\Rule\OneOf; |
|
9
|
|
|
use Yiisoft\Validator\Rule\OneOfHandler; |
|
10
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
|
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
|
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
|
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
|
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
|
15
|
|
|
use Yiisoft\Validator\Tests\Support\Data\OneOfDto; |
|
16
|
|
|
|
|
17
|
|
|
final class OneOfTest 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 OneOf([]); |
|
27
|
|
|
$this->assertSame('oneOf', $rule->getName()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function dataOptions(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
[ |
|
34
|
|
|
new OneOf(['attr1', 'attr2']), |
|
35
|
|
|
[ |
|
36
|
|
|
'attributes' => [ |
|
37
|
|
|
'attr1', |
|
38
|
|
|
'attr2', |
|
39
|
|
|
], |
|
40
|
|
|
'incorrectInputMessage' => [ |
|
41
|
|
|
'template' => 'The value must be an array or an object.', |
|
42
|
|
|
'parameters' => [], |
|
43
|
|
|
], |
|
44
|
|
|
'message' => [ |
|
45
|
|
|
'template' => 'Exactly 1 attribute from this list must be filled: {attributes}.', |
|
46
|
|
|
'parameters' => [], |
|
47
|
|
|
], |
|
48
|
|
|
'skipOnEmpty' => false, |
|
49
|
|
|
'skipOnError' => false, |
|
50
|
|
|
], |
|
51
|
|
|
], |
|
52
|
|
|
'callable skip on empty' => [ |
|
53
|
|
|
new OneOf(['attr1', 'attr2'], skipOnEmpty: new NeverEmpty()), |
|
54
|
|
|
[ |
|
55
|
|
|
'attributes' => [ |
|
56
|
|
|
'attr1', |
|
57
|
|
|
'attr2', |
|
58
|
|
|
], |
|
59
|
|
|
'incorrectInputMessage' => [ |
|
60
|
|
|
'template' => 'The value must be an array or an object.', |
|
61
|
|
|
'parameters' => [], |
|
62
|
|
|
], |
|
63
|
|
|
'message' => [ |
|
64
|
|
|
'template' => 'Exactly 1 attribute from this list must be filled: {attributes}.', |
|
65
|
|
|
'parameters' => [], |
|
66
|
|
|
], |
|
67
|
|
|
'skipOnEmpty' => null, |
|
68
|
|
|
'skipOnError' => false, |
|
69
|
|
|
], |
|
70
|
|
|
], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function dataValidationPassed(): array |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
[ |
|
78
|
|
|
new class () { |
|
79
|
|
|
public $attr1 = 1; |
|
80
|
|
|
public $attr2 = null; |
|
81
|
|
|
}, |
|
82
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
83
|
|
|
], |
|
84
|
|
|
[ |
|
85
|
|
|
new class () { |
|
86
|
|
|
public $attr1 = null; |
|
87
|
|
|
public $attr2 = 1; |
|
88
|
|
|
}, |
|
89
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
90
|
|
|
], |
|
91
|
|
|
[ |
|
92
|
|
|
new class () { |
|
93
|
|
|
private int $attr1 = 1; |
|
|
|
|
|
|
94
|
|
|
private $attr2 = null; |
|
|
|
|
|
|
95
|
|
|
}, |
|
96
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
97
|
|
|
], |
|
98
|
|
|
[ |
|
99
|
|
|
['attr1' => 1, 'attr2' => null], |
|
100
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
['attr1' => null, 'attr2' => 1], |
|
104
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
105
|
|
|
], |
|
106
|
|
|
[ |
|
107
|
|
|
new class () { |
|
108
|
|
|
public $obj; |
|
109
|
|
|
|
|
110
|
|
|
public function __construct() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->obj = new class () { |
|
113
|
|
|
public $attr1 = 1; |
|
114
|
|
|
public $attr2 = null; |
|
115
|
|
|
}; |
|
116
|
|
|
} |
|
117
|
|
|
}, |
|
118
|
|
|
['obj' => new OneOf(['attr1', 'attr2'])], |
|
119
|
|
|
], |
|
120
|
|
|
[ |
|
121
|
|
|
new class () { |
|
122
|
|
|
public $obj; |
|
123
|
|
|
|
|
124
|
|
|
public function __construct() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->obj = new class () { |
|
127
|
|
|
public $attr1 = null; |
|
128
|
|
|
public $attr2 = 1; |
|
129
|
|
|
}; |
|
130
|
|
|
} |
|
131
|
|
|
}, |
|
132
|
|
|
['obj' => new OneOf(['attr1', 'attr2'])], |
|
133
|
|
|
], |
|
134
|
|
|
[ |
|
135
|
|
|
['obj' => ['attr1' => 1, 'attr2' => null]], |
|
136
|
|
|
['obj' => new OneOf(['attr1', 'attr2'])], |
|
137
|
|
|
], |
|
138
|
|
|
[ |
|
139
|
|
|
['obj' => ['attr1' => null, 'attr2' => 1]], |
|
140
|
|
|
['obj' => new OneOf(['attr1', 'attr2'])], |
|
141
|
|
|
], |
|
142
|
|
|
'class attribute' => [ |
|
143
|
|
|
new OneOfDto(1), |
|
144
|
|
|
], |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function dataValidationFailed(): array |
|
149
|
|
|
{ |
|
150
|
|
|
$object = new class () { |
|
151
|
|
|
public $attr1 = null; |
|
152
|
|
|
public $attr2 = null; |
|
153
|
|
|
}; |
|
154
|
|
|
$array = ['attr1' => null, 'attr2' => null]; |
|
155
|
|
|
|
|
156
|
|
|
return [ |
|
157
|
|
|
'incorrect input' => [ |
|
158
|
|
|
1, |
|
159
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
160
|
|
|
['' => ['The value must be an array or an object.']], |
|
161
|
|
|
], |
|
162
|
|
|
'custom incorrect input message' => [ |
|
163
|
|
|
1, |
|
164
|
|
|
[new OneOf(['attr1', 'attr2'], incorrectInputMessage: 'Custom incorrect input message.')], |
|
165
|
|
|
['' => ['Custom incorrect input message.']], |
|
166
|
|
|
], |
|
167
|
|
|
'custom incorrect input message with parameters' => [ |
|
168
|
|
|
1, |
|
169
|
|
|
[new OneOf(['attr1', 'attr2'], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
|
170
|
|
|
['' => ['Attribute - , type - int.']], |
|
171
|
|
|
], |
|
172
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
|
173
|
|
|
['attribute' => 1], |
|
174
|
|
|
[ |
|
175
|
|
|
'attribute' => new OneOf( |
|
176
|
|
|
['attr1', 'attr2'], |
|
177
|
|
|
incorrectInputMessage: 'Attribute - {attribute}, type - {type}.', |
|
178
|
|
|
), |
|
179
|
|
|
], |
|
180
|
|
|
['attribute' => ['Attribute - attribute, type - int.']], |
|
181
|
|
|
], |
|
182
|
|
|
'object' => [ |
|
183
|
|
|
$object, |
|
184
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
185
|
|
|
['' => ['Exactly 1 attribute from this list must be filled: "attr1", "attr2".']], |
|
186
|
|
|
], |
|
187
|
|
|
'array' => [ |
|
188
|
|
|
$array, |
|
189
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
190
|
|
|
['' => ['Exactly 1 attribute from this list must be filled: "attr1", "attr2".']], |
|
191
|
|
|
], |
|
192
|
|
|
'more than 1 attribute is filled' => [ |
|
193
|
|
|
['attr1' => 1, 'attr2' => 2], |
|
194
|
|
|
[new OneOf(['attr1', 'attr2'])], |
|
195
|
|
|
['' => ['Exactly 1 attribute from this list must be filled: "attr1", "attr2".']], |
|
196
|
|
|
], |
|
197
|
|
|
'custom message' => [ |
|
198
|
|
|
$object, |
|
199
|
|
|
[new OneOf(['attr1', 'attr2'], message: 'Custom message.')], |
|
200
|
|
|
['' => ['Custom message.']], |
|
201
|
|
|
], |
|
202
|
|
|
'custom message with parameters' => [ |
|
203
|
|
|
$object, |
|
204
|
|
|
[new OneOf(['attr1', 'attr2'], message: 'Attributes - {attributes}.')], |
|
205
|
|
|
['' => ['Attributes - "attr1", "attr2".']], |
|
206
|
|
|
], |
|
207
|
|
|
'custom message with parameters, attribute set' => [ |
|
208
|
|
|
['data' => $object], |
|
209
|
|
|
['data' => new OneOf(['attr1', 'attr2'], message: 'Attributes - {attributes}.')], |
|
210
|
|
|
['data' => ['Attributes - "attr1", "attr2".']], |
|
211
|
|
|
], |
|
212
|
|
|
'class attribute' => [ |
|
213
|
|
|
new OneOfDto(), |
|
214
|
|
|
null, |
|
215
|
|
|
['' => ['Exactly 1 attribute from this list must be filled: "A", "B", "C".']], |
|
216
|
|
|
], |
|
217
|
|
|
]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function testSkipOnError(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$this->testSkipOnErrorInternal(new OneOf([]), new OneOf([], skipOnError: true)); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function testWhen(): void |
|
226
|
|
|
{ |
|
227
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
|
228
|
|
|
$this->testWhenInternal(new OneOf([]), new OneOf([], when: $when)); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
|
232
|
|
|
{ |
|
233
|
|
|
return [OneOf::class, OneOfHandler::class]; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|