Passed
Push — master ( b35780...9a3615 )
by
unknown
02:44
created

StopOnErrorTest::testWhen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use Yiisoft\Validator\Rule\Length;
8
use Yiisoft\Validator\Rule\Number;
9
use Yiisoft\Validator\Rule\Required;
10
use Yiisoft\Validator\Rule\StopOnError;
11
use Yiisoft\Validator\Rule\StopOnErrorHandler;
12
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
14
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
15
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
16
17
final class StopOnErrorTest extends RuleTestCase
18
{
19
    use DifferentRuleInHandlerTestTrait;
20
    use RuleWithOptionsTestTrait;
21
    use WhenTestTrait;
22
23
    public function testGetName(): void
24
    {
25
        $rule = new StopOnError([new Length(min: 10)]);
26
        $this->assertSame('stopOnError', $rule->getName());
27
    }
28
29
    public function dataOptions(): array
30
    {
31
        return [
32
            [
33
                new StopOnError([new Length(min: 10)]),
34
                [
35
                    'skipOnEmpty' => false,
36
                    'skipOnError' => false,
37
                    'rules' => [
38
                        [
39
                            'length',
40
                            'min' => 10,
41
                            'max' => null,
42
                            'exactly' => null,
43
                            'lessThanMinMessage' => [
44
                                'template' => 'This value must contain at least {min, number} {min, plural, ' .
45
                                    'one{character} other{characters}}.',
46
                                'parameters' => [
47
                                    'min' => 10,
48
                                ],
49
                            ],
50
                            'greaterThanMaxMessage' => [
51
                                'template' => 'This value must contain at most {max, number} {max, plural, ' .
52
                                    'one{character} other{characters}}.',
53
                                'parameters' => [
54
                                    'max' => null,
55
                                ],
56
                            ],
57
                            'notExactlyMessage' => [
58
                                'template' => 'This value must contain exactly {exactly, number} {exactly, plural, ' .
59
                                    'one{character} other{characters}}.',
60
                                'parameters' => [
61
                                    'exactly' => null,
62
                                ],
63
                            ],
64
                            'incorrectInputMessage' => [
65
                                'template' => 'The value must be a string.',
66
                                'parameters' => [],
67
                            ],
68
                            'encoding' => 'UTF-8',
69
                            'skipOnEmpty' => false,
70
                            'skipOnError' => false,
71
                        ],
72
                    ],
73
                ],
74
            ],
75
        ];
76
    }
77
78
    public function dataValidationPassed(): array
79
    {
80
        return [
81
            'at least one succeed property' => [
82
                'hello',
83
                [
84
                    new StopOnError([
85
                        new Length(min: 1),
86
                        new Length(max: 10),
87
                    ]),
88
                ],
89
            ],
90
        ];
91
    }
92
93
    public function dataValidationFailed(): array
94
    {
95
        return [
96
            'basic' => [
97
                'hello',
98
                [
99
                    new StopOnError([
100
                        new Length(min: 10),
101
                        new Length(max: 1),
102
                    ]),
103
                ],
104
                ['' => ['This value must contain at least 10 characters.']],
105
            ],
106
            'basic, different order' => [
107
                'hello',
108
                [
109
                    new StopOnError([
110
                        new Length(max: 1),
111
                        new Length(min: 10),
112
                    ]),
113
                ],
114
                ['' => ['This value must contain at most 1 character.']],
115
            ],
116
            'combined with other top level rules' => [
117
                'hello',
118
                [
119
                    new Number(),
120
                    new StopOnError([
121
                        new Length(max: 1),
122
                        new Length(min: 10),
123
                    ]),
124
                    new Length(min: 7),
125
                ],
126
                [
127
                    '' => [
128
                        'Value must be a number.',
129
                        'This value must contain at most 1 character.',
130
                        'This value must contain at least 7 characters.',
131
                    ],
132
                ],
133
            ],
134
            'combined with other top level rules, skipOnError: true' => [
135
                'hello',
136
                [
137
                    new Number(),
138
                    new StopOnError(
139
                        [
140
                            new Length(max: 1),
141
                            new Length(min: 10),
142
                        ],
143
                        skipOnError: true,
144
                    ),
145
                    new Length(min: 7),
146
                ],
147
                [
148
                    '' => [
149
                        'Value must be a number.',
150
                        'This value must contain at least 7 characters.',
151
                    ],
152
                ],
153
            ],
154
            'attributes, multiple StopOnError rules combined with other top level rules' => [
155
                [],
156
                [
157
                    'a' => new Required(),
158
                    'b' => new StopOnError([
159
                        new Required(),
160
                        new Number(min: 7),
161
                    ]),
162
                    'c' => new StopOnError([
163
                        new Required(),
164
                        new Number(min: 42),
165
                    ]),
166
                    'd' => new Required(),
167
                ],
168
                [
169
                    'a' => ['Value not passed.'],
170
                    'b' => ['Value not passed.'],
171
                    'c' => ['Value not passed.'],
172
                    'd' => ['Value not passed.'],
173
                ],
174
            ],
175
            'attributes, multiple StopOnError rules combined with other top level rules, skipOnError: true' => [
176
                [],
177
                [
178
                    'a' => new Required(),
179
                    'b' => new StopOnError(
180
                        [
181
                            new Required(),
182
                            new Number(min: 7),
183
                        ],
184
                        skipOnError: true,
185
                    ),
186
                    'c' => new StopOnError(
187
                        [
188
                            new Required(),
189
                            new Number(min: 42),
190
                        ],
191
                        skipOnError: true,
192
                    ),
193
                    'd' => new Required(),
194
                ],
195
                [
196
                    'a' => ['Value not passed.'],
197
                    'd' => ['Value not passed.'],
198
                ],
199
            ],
200
            'check for missing data set' => [
201
                ['b' => null],
202
                [
203
                    'a' => new StopOnError([
204
                        new Required(),
205
                    ]),
206
                    'b' => new Required(),
207
                ],
208
                [
209
                    'a' => ['Value not passed.'],
210
                    'b' => ['Value cannot be blank.'],
211
                ],
212
            ],
213
        ];
214
    }
215
216
    public function testWhen(): void
217
    {
218
        $when = static fn (mixed $value): bool => $value !== null;
219
        $this->testWhenInternal(
220
            new StopOnError([new Length(min: 10)]),
221
            new StopOnError([new Length(min: 10)], when: $when),
222
        );
223
    }
224
225
    protected function getDifferentRuleInHandlerItems(): array
226
    {
227
        return [StopOnError::class, StopOnErrorHandler::class];
228
    }
229
}
230