Passed
Push — master ( dbf0a8...b29633 )
by
unknown
02:35
created

StopOnErrorTest::dataValidationFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 22
rs 9.8333
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\HasLength;
8
use Yiisoft\Validator\Rule\StopOnError;
9
use Yiisoft\Validator\Rule\StopOnErrorHandler;
10
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15
16
final class StopOnErrorTest extends RuleTestCase
17
{
18
    use DifferentRuleInHandlerTestTrait;
19
    use RuleWithOptionsTestTrait;
20
    use SkipOnErrorTestTrait;
21
    use WhenTestTrait;
22
23
    public function testGetName(): void
24
    {
25
        $rule = new StopOnError([new HasLength(min: 10)]);
26
        $this->assertSame('stopOnError', $rule->getName());
27
    }
28
29
    public function dataOptions(): array
30
    {
31
        return [
32
            [
33
                new StopOnError([new HasLength(min: 10)]),
34
                [
35
                    'skipOnEmpty' => false,
36
                    'skipOnError' => false,
37
                    'rules' => [
38
                        [
39
                            'hasLength',
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' => 'This 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 HasLength(min: 1),
86
                        new HasLength(max: 10),
87
                    ]),
88
                ],
89
            ],
90
        ];
91
    }
92
93
    public function dataValidationFailed(): array
94
    {
95
        return [
96
            'case1' => [
97
                'hello',
98
                [
99
                    new StopOnError([
100
                        new HasLength(min: 10),
101
                        new HasLength(max: 1),
102
                    ]),
103
                ],
104
                ['' => ['This value must contain at least 10 characters.']],
105
            ],
106
            'case2' => [
107
                'hello',
108
                [
109
                    new StopOnError([
110
                        new HasLength(max: 1),
111
                        new HasLength(min: 10),
112
                    ]),
113
                ],
114
                ['' => ['This value must contain at most 1 character.']],
115
            ],
116
        ];
117
    }
118
119
    public function testSkipOnError(): void
120
    {
121
        $this->testskipOnErrorInternal(
122
            new StopOnError([new HasLength(min: 10)]),
123
            new StopOnError([new HasLength(min: 10)], skipOnError: true),
124
        );
125
    }
126
127
    public function testWhen(): void
128
    {
129
        $when = static fn (mixed $value): bool => $value !== null;
130
        $this->testWhenInternal(
131
            new StopOnError([new HasLength(min: 10)]),
132
            new StopOnError([new HasLength(min: 10)], when: $when),
133
        );
134
    }
135
136
    protected function getDifferentRuleInHandlerItems(): array
137
    {
138
        return [StopOnError::class, StopOnErrorHandler::class];
139
    }
140
}
141