Passed
Pull Request — master (#550)
by Alexander
05:42 queued 02:37
created

StopOnErrorTest::testGetOptionsWithNotRule()   A

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', $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',
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
            'combined with other top level rules' => [
125
                'hello',
126
                [
127
                    new Number(),
128
                    new StopOnError([
129
                        new Length(max: 1),
130
                        new Length(min: 10),
131
                    ]),
132
                    new Length(min: 7),
133
                ],
134
                [
135
                    '' => [
136
                        'Value must be a number.',
137
                        'This value must contain at most 1 character.',
138
                        'This value must contain at least 7 characters.',
139
                    ],
140
                ],
141
            ],
142
            'combined with other top level rules, skipOnError: true' => [
143
                'hello',
144
                [
145
                    new Number(),
146
                    new StopOnError(
147
                        [
148
                            new Length(max: 1),
149
                            new Length(min: 10),
150
                        ],
151
                        skipOnError: true,
152
                    ),
153
                    new Length(min: 7),
154
                ],
155
                [
156
                    '' => [
157
                        'Value must be a number.',
158
                        'This value must contain at least 7 characters.',
159
                    ],
160
                ],
161
            ],
162
            'attributes, multiple StopOnError rules combined with other top level rules' => [
163
                [],
164
                [
165
                    'a' => new Required(),
166
                    'b' => new StopOnError([
167
                        new Required(),
168
                        new Number(min: 7),
169
                    ]),
170
                    'c' => new StopOnError([
171
                        new Required(),
172
                        new Number(min: 42),
173
                    ]),
174
                    'd' => new Required(),
175
                ],
176
                [
177
                    'a' => ['Value not passed.'],
178
                    'b' => ['Value not passed.'],
179
                    'c' => ['Value not passed.'],
180
                    'd' => ['Value not passed.'],
181
                ],
182
            ],
183
            'attributes, multiple StopOnError rules combined with other top level rules, skipOnError: true' => [
184
                [],
185
                [
186
                    'a' => new Required(),
187
                    'b' => new StopOnError(
188
                        [
189
                            new Required(),
190
                            new Number(min: 7),
191
                        ],
192
                        skipOnError: true,
193
                    ),
194
                    'c' => new StopOnError(
195
                        [
196
                            new Required(),
197
                            new Number(min: 42),
198
                        ],
199
                        skipOnError: true,
200
                    ),
201
                    'd' => new Required(),
202
                ],
203
                [
204
                    'a' => ['Value not passed.'],
205
                    'd' => ['Value not passed.'],
206
                ],
207
            ],
208
            'check for missing data set' => [
209
                ['b' => null],
210
                [
211
                    'a' => new StopOnError([
212
                        new Required(),
213
                    ]),
214
                    'b' => new Required(),
215
                ],
216
                [
217
                    'a' => ['Value not passed.'],
218
                    'b' => ['Value cannot be blank.'],
219
                ],
220
            ],
221
            'rules normalization, callable' => [
222
                [],
223
                new StopOnError([
224
                    static fn (): Result => (new Result())->addError('Custom error.'),
225
                ]),
226
                ['' => ['Custom error.']],
227
            ],
228
        ];
229
    }
230
231
    public function testWhen(): void
232
    {
233
        $when = static fn (mixed $value): bool => $value !== null;
234
        $this->testWhenInternal(
235
            new StopOnError([new Length(min: 10)]),
236
            new StopOnError([new Length(min: 10)], when: $when),
237
        );
238
    }
239
240
    protected function getDifferentRuleInHandlerItems(): array
241
    {
242
        return [StopOnError::class, StopOnErrorHandler::class];
243
    }
244
}
245