StopOnErrorTest::testGetOptionsWithNotRule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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