1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Rule\TrueValue; |
8
|
|
|
use Yiisoft\Validator\Rule\TrueValueHandler; |
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 TrueValueTest 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 TrueValue(); |
25
|
|
|
$this->assertSame('isTrue', $rule->getName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function dataOptions(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
[ |
32
|
|
|
new TrueValue(), |
33
|
|
|
[ |
34
|
|
|
'trueValue' => '1', |
35
|
|
|
'strict' => false, |
36
|
|
|
'incorrectInputMessage' => [ |
37
|
|
|
'template' => 'The allowed types are integer, float, string, boolean. {type} given.', |
38
|
|
|
'parameters' => [ |
39
|
|
|
'true' => '1', |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
'message' => [ |
43
|
|
|
'template' => 'The value must be "{true}".', |
44
|
|
|
'parameters' => [ |
45
|
|
|
'true' => '1', |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
'skipOnEmpty' => false, |
49
|
|
|
'skipOnError' => false, |
50
|
|
|
], |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
new TrueValue(trueValue: true, strict: true), |
54
|
|
|
[ |
55
|
|
|
'trueValue' => true, |
56
|
|
|
'strict' => true, |
57
|
|
|
'incorrectInputMessage' => [ |
58
|
|
|
'template' => 'The allowed types are integer, float, string, boolean. {type} given.', |
59
|
|
|
'parameters' => [ |
60
|
|
|
'true' => 'true', |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
'message' => [ |
64
|
|
|
'template' => 'The value must be "{true}".', |
65
|
|
|
'parameters' => [ |
66
|
|
|
'true' => 'true', |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
'skipOnEmpty' => false, |
70
|
|
|
'skipOnError' => false, |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
new TrueValue( |
75
|
|
|
trueValue: 'YES', |
76
|
|
|
strict: true, |
77
|
|
|
incorrectInputMessage: 'Custom incorrect input message.', |
78
|
|
|
message: 'Custom message.', |
79
|
|
|
skipOnEmpty: true, |
80
|
|
|
skipOnError: true |
81
|
|
|
), |
82
|
|
|
[ |
83
|
|
|
'trueValue' => 'YES', |
84
|
|
|
'strict' => true, |
85
|
|
|
'incorrectInputMessage' => [ |
86
|
|
|
'template' => 'Custom incorrect input message.', |
87
|
|
|
'parameters' => [ |
88
|
|
|
'true' => 'YES', |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
'message' => [ |
92
|
|
|
'template' => 'Custom message.', |
93
|
|
|
'parameters' => [ |
94
|
|
|
'true' => 'YES', |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
'skipOnEmpty' => true, |
98
|
|
|
'skipOnError' => true, |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function dataValidationPassed(): array |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
[true, [new TrueValue()]], |
108
|
|
|
['1', [new TrueValue()]], |
109
|
|
|
['1', [new TrueValue(strict: true)]], |
110
|
|
|
[true, [new TrueValue(trueValue: true, strict: true)]], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function dataValidationFailed(): array |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
|
|
['5', [new TrueValue()], ['' => ['The value must be "1".']]], |
118
|
|
|
[null, [new TrueValue()], ['' => ['The allowed types are integer, float, string, boolean. null given.']]], |
119
|
|
|
[[], [new TrueValue()], ['' => ['The allowed types are integer, float, string, boolean. array given.']]], |
120
|
|
|
[true, [new TrueValue(strict: true)], ['' => ['The value must be "1".']]], |
121
|
|
|
['1', [new TrueValue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]], |
122
|
|
|
[ |
123
|
|
|
[], |
124
|
|
|
[new TrueValue(trueValue: true, strict: true)], |
125
|
|
|
['' => ['The allowed types are integer, float, string, boolean. array given.']], |
126
|
|
|
], |
127
|
|
|
|
128
|
|
|
[false, [new TrueValue()], ['' => ['The value must be "1".']]], |
129
|
|
|
['0', [new TrueValue()], ['' => ['The value must be "1".']]], |
130
|
|
|
['0', [new TrueValue(strict: true)], ['' => ['The value must be "1".']]], |
131
|
|
|
[false, [new TrueValue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]], |
132
|
|
|
|
133
|
|
|
'custom message' => [ |
134
|
|
|
5, |
135
|
|
|
[new TrueValue(message: 'Custom error.')], |
136
|
|
|
['' => ['Custom error.']], |
137
|
|
|
], |
138
|
|
|
'custom message with parameters' => [ |
139
|
|
|
5, |
140
|
|
|
[new TrueValue(message: 'Attribute - {attribute}, true - {true}, value - {value}.')], |
141
|
|
|
['' => ['Attribute - , true - 1, value - 5.']], |
142
|
|
|
], |
143
|
|
|
'custom message with parameters, custom true value, strict' => [ |
144
|
|
|
5, |
145
|
|
|
[ |
146
|
|
|
new TrueValue( |
147
|
|
|
trueValue: true, |
148
|
|
|
strict: true, |
149
|
|
|
message: 'Attribute - {attribute}, true - {true}, value - {value}.', |
150
|
|
|
), |
151
|
|
|
], |
152
|
|
|
['' => ['Attribute - , true - true, value - 5.']], |
153
|
|
|
], |
154
|
|
|
'custom message with parameters, attribute set' => [ |
155
|
|
|
['data' => 5], |
156
|
|
|
[ |
157
|
|
|
'data' => new TrueValue(message: 'Attribute - {attribute}, true - {true}, value - {value}.'), |
158
|
|
|
], |
159
|
|
|
['data' => ['Attribute - data, true - 1, value - 5.']], |
160
|
|
|
], |
161
|
|
|
'custom incorrect input message' => [ |
162
|
|
|
[], |
163
|
|
|
[new TrueValue(incorrectInputMessage: 'Custom error.')], |
164
|
|
|
['' => ['Custom error.']], |
165
|
|
|
], |
166
|
|
|
'custom incorrect input message with parameters' => [ |
167
|
|
|
[], |
168
|
|
|
[ |
169
|
|
|
new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'), |
170
|
|
|
], |
171
|
|
|
['' => ['Attribute - , true - 1, type - array.']], |
172
|
|
|
], |
173
|
|
|
'custom incorrect input message with parameters, custom true and false values, strict' => [ |
174
|
|
|
[], |
175
|
|
|
[ |
176
|
|
|
new TrueValue( |
177
|
|
|
trueValue: true, |
178
|
|
|
strict: true, |
179
|
|
|
incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.', |
180
|
|
|
), |
181
|
|
|
], |
182
|
|
|
['' => ['Attribute - , true - true, type - array.']], |
183
|
|
|
], |
184
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
185
|
|
|
['data' => []], |
186
|
|
|
[ |
187
|
|
|
'data' => new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'), |
188
|
|
|
], |
189
|
|
|
['data' => ['Attribute - data, true - 1, type - array.']], |
190
|
|
|
], |
191
|
|
|
'custom incorrect input message, null' => [ |
192
|
|
|
null, |
193
|
|
|
[new TrueValue(incorrectInputMessage: 'Attribute - {attribute}, true - {true}, type - {type}.'),], |
194
|
|
|
['' => ['Attribute - , true - 1, type - null.']], |
195
|
|
|
], |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testSkipOnError(): void |
200
|
|
|
{ |
201
|
|
|
$this->testSkipOnErrorInternal(new TrueValue(), new TrueValue(skipOnError: true)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testWhen(): void |
205
|
|
|
{ |
206
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
207
|
|
|
$this->testWhenInternal(new TrueValue(), new TrueValue(when: $when)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
211
|
|
|
{ |
212
|
|
|
return [TrueValue::class, TrueValueHandler::class]; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|