1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Rule\IsTrue; |
8
|
|
|
use Yiisoft\Validator\Rule\IsTrueHandler; |
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 IsTrueTest 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 IsTrue(); |
25
|
|
|
$this->assertSame('isTrue', $rule->getName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function dataOptions(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
[ |
32
|
|
|
new IsTrue(), |
33
|
|
|
[ |
34
|
|
|
'trueValue' => '1', |
35
|
|
|
'strict' => false, |
36
|
|
|
'message' => [ |
37
|
|
|
'template' => 'The value must be "{true}".', |
38
|
|
|
'parameters' => [ |
39
|
|
|
'true' => '1', |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
'skipOnEmpty' => false, |
43
|
|
|
'skipOnError' => false, |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
[ |
47
|
|
|
new IsTrue(trueValue: true, strict: true), |
48
|
|
|
[ |
49
|
|
|
'trueValue' => true, |
50
|
|
|
'strict' => true, |
51
|
|
|
'message' => [ |
52
|
|
|
'template' => 'The value must be "{true}".', |
53
|
|
|
'parameters' => [ |
54
|
|
|
'true' => 'true', |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
'skipOnEmpty' => false, |
58
|
|
|
'skipOnError' => false, |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
new IsTrue( |
63
|
|
|
trueValue: 'YES', |
64
|
|
|
strict: true, |
65
|
|
|
message: 'Custom message.', |
66
|
|
|
skipOnEmpty: true, |
67
|
|
|
skipOnError: true |
68
|
|
|
), |
69
|
|
|
[ |
70
|
|
|
'trueValue' => 'YES', |
71
|
|
|
'strict' => true, |
72
|
|
|
'message' => [ |
73
|
|
|
'template' => 'Custom message.', |
74
|
|
|
'parameters' => [ |
75
|
|
|
'true' => 'YES', |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
'skipOnEmpty' => true, |
79
|
|
|
'skipOnError' => true, |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function dataValidationPassed(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
[true, [new IsTrue()]], |
89
|
|
|
['1', [new IsTrue()]], |
90
|
|
|
['1', [new IsTrue(strict: true)]], |
91
|
|
|
[true, [new IsTrue(trueValue: true, strict: true)]], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function dataValidationFailed(): array |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
['5', [new IsTrue()], ['' => ['The value must be "1".']]], |
99
|
|
|
[null, [new IsTrue()], ['' => ['The value must be "1".']]], |
100
|
|
|
[[], [new IsTrue()], ['' => ['The value must be "1".']]], |
101
|
|
|
[true, [new IsTrue(strict: true)], ['' => ['The value must be "1".']]], |
102
|
|
|
['1', [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]], |
103
|
|
|
[[], [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]], |
104
|
|
|
|
105
|
|
|
[false, [new IsTrue()], ['' => ['The value must be "1".']]], |
106
|
|
|
['0', [new IsTrue()], ['' => ['The value must be "1".']]], |
107
|
|
|
['0', [new IsTrue(strict: true)], ['' => ['The value must be "1".']]], |
108
|
|
|
[false, [new IsTrue(trueValue: true, strict: true)], ['' => ['The value must be "true".']]], |
109
|
|
|
'custom error' => [5, [new IsTrue(message: 'Custom error.')], ['' => ['Custom error.']]], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testSkipOnError(): void |
114
|
|
|
{ |
115
|
|
|
$this->testSkipOnErrorInternal(new IsTrue(), new IsTrue(skipOnError: true)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testWhen(): void |
119
|
|
|
{ |
120
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
121
|
|
|
$this->testWhenInternal(new IsTrue(), new IsTrue(when: $when)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
125
|
|
|
{ |
126
|
|
|
return [IsTrue::class, IsTrueHandler::class]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|