1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Rule\HasLength; |
8
|
|
|
use Yiisoft\Validator\Rule\Number; |
9
|
|
|
use Yiisoft\Validator\Rule\StopOnError; |
10
|
|
|
use Yiisoft\Validator\Rule\StopOnErrorHandler; |
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\WhenTestTrait; |
15
|
|
|
|
16
|
|
|
final class StopOnErrorTest extends RuleTestCase |
17
|
|
|
{ |
18
|
|
|
use DifferentRuleInHandlerTestTrait; |
19
|
|
|
use RuleWithOptionsTestTrait; |
20
|
|
|
use WhenTestTrait; |
21
|
|
|
|
22
|
|
|
public function testGetName(): void |
23
|
|
|
{ |
24
|
|
|
$rule = new StopOnError([new HasLength(min: 10)]); |
25
|
|
|
$this->assertSame('stopOnError', $rule->getName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function dataOptions(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
[ |
32
|
|
|
new StopOnError([new HasLength(min: 10)]), |
33
|
|
|
[ |
34
|
|
|
'skipOnEmpty' => false, |
35
|
|
|
'rules' => [ |
36
|
|
|
[ |
37
|
|
|
'hasLength', |
38
|
|
|
'min' => 10, |
39
|
|
|
'max' => null, |
40
|
|
|
'exactly' => null, |
41
|
|
|
'lessThanMinMessage' => [ |
42
|
|
|
'template' => 'This value must contain at least {min, number} {min, plural, ' . |
43
|
|
|
'one{character} other{characters}}.', |
44
|
|
|
'parameters' => [ |
45
|
|
|
'min' => 10, |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
'greaterThanMaxMessage' => [ |
49
|
|
|
'template' => 'This value must contain at most {max, number} {max, plural, ' . |
50
|
|
|
'one{character} other{characters}}.', |
51
|
|
|
'parameters' => [ |
52
|
|
|
'max' => null, |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
'notExactlyMessage' => [ |
56
|
|
|
'template' => 'This value must contain exactly {exactly, number} {exactly, plural, ' . |
57
|
|
|
'one{character} other{characters}}.', |
58
|
|
|
'parameters' => [ |
59
|
|
|
'exactly' => null, |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
'incorrectInputMessage' => [ |
63
|
|
|
'template' => 'This value must be a string.', |
64
|
|
|
'parameters' => [], |
65
|
|
|
], |
66
|
|
|
'encoding' => 'UTF-8', |
67
|
|
|
'skipOnEmpty' => false, |
68
|
|
|
'skipOnError' => false, |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function dataValidationPassed(): array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'at least one succeed property' => [ |
80
|
|
|
'hello', |
81
|
|
|
[ |
82
|
|
|
new StopOnError([ |
83
|
|
|
new HasLength(min: 1), |
84
|
|
|
new HasLength(max: 10), |
85
|
|
|
]), |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function dataValidationFailed(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'case1' => [ |
95
|
|
|
'hello', |
96
|
|
|
[ |
97
|
|
|
new StopOnError([ |
98
|
|
|
new HasLength(min: 10), |
99
|
|
|
new HasLength(max: 1), |
100
|
|
|
]), |
101
|
|
|
], |
102
|
|
|
['' => ['This value must contain at least 10 characters.']], |
103
|
|
|
], |
104
|
|
|
'case2' => [ |
105
|
|
|
'hello', |
106
|
|
|
[ |
107
|
|
|
new StopOnError([ |
108
|
|
|
new HasLength(max: 1), |
109
|
|
|
new HasLength(min: 10), |
110
|
|
|
]), |
111
|
|
|
], |
112
|
|
|
['' => ['This value must contain at most 1 character.']], |
113
|
|
|
], |
114
|
|
|
'case3' => [ |
115
|
|
|
'hello', |
116
|
|
|
[ |
117
|
|
|
new Number(), |
118
|
|
|
new StopOnError([ |
119
|
|
|
new HasLength(max: 1), |
120
|
|
|
new HasLength(min: 10), |
121
|
|
|
]), |
122
|
|
|
new HasLength(min: 7), |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
'' => [ |
126
|
|
|
'Value must be a number.', |
127
|
|
|
'This value must contain at least 7 characters.', |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testWhen(): void |
135
|
|
|
{ |
136
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
137
|
|
|
$this->testWhenInternal( |
138
|
|
|
new StopOnError([new HasLength(min: 10)]), |
139
|
|
|
new StopOnError([new HasLength(min: 10)], when: $when), |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
144
|
|
|
{ |
145
|
|
|
return [StopOnError::class, StopOnErrorHandler::class]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|