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