EqualTest::testSkipOnError()   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 DateTime;
8
use Yiisoft\Validator\Rule\CompareType;
9
use Yiisoft\Validator\Rule\Equal;
10
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
14
15
final class EqualTest extends RuleTestCase
16
{
17
    use RuleWithOptionsTestTrait;
18
    use SkipOnErrorTestTrait;
19
    use WhenTestTrait;
20
21
    public function testGetName(): void
22
    {
23
        $rule = new Equal(1);
24
        $this->assertSame(Equal::class, $rule->getName());
25
    }
26
27
    public function dataOptions(): array
28
    {
29
        return [
30
            [
31
                new Equal(1),
32
                [
33
                    'targetValue' => 1,
34
                    'targetAttribute' => null,
35
                    'incorrectInputMessage' => [
36
                        'template' => 'The allowed types are integer, float, string, boolean, null and object ' .
37
                            'implementing \Stringable interface or \DateTimeInterface.',
38
                        'parameters' => [
39
                            'targetValue' => 1,
40
                            'targetAttribute' => null,
41
                            'targetValueOrAttribute' => 1,
42
                        ],
43
                    ],
44
                    'incorrectDataSetTypeMessage' => [
45
                        'template' => 'The attribute value returned from a custom data set must have one of the ' .
46
                            'following types: integer, float, string, boolean, null or an object implementing ' .
47
                            '\Stringable interface or \DateTimeInterface.',
48
                        'parameters' => [
49
                            'targetValue' => 1,
50
                            'targetAttribute' => null,
51
                            'targetValueOrAttribute' => 1,
52
                        ],
53
                    ],
54
                    'message' => [
55
                        'template' => 'Value must be equal to "{targetValueOrAttribute}".',
56
                        'parameters' => [
57
                            'targetValue' => 1,
58
                            'targetAttribute' => null,
59
                            'targetValueOrAttribute' => 1,
60
                        ],
61
                    ],
62
                    'type' => 'number',
63
                    'operator' => '==',
64
                    'skipOnEmpty' => false,
65
                    'skipOnError' => false,
66
                ],
67
            ],
68
            [
69
                new Equal(
70
                    new DateTime('2023-02-07 12:57:12'),
71
                    targetAttribute: 'test',
72
                    incorrectInputMessage: 'Custom message 1.',
73
                    incorrectDataSetTypeMessage: 'Custom message 2.',
74
                    message: 'Custom message 3.',
75
                    type: CompareType::ORIGINAL,
76
                    strict: true,
77
                    skipOnEmpty: true,
78
                    skipOnError: true,
79
                    when: static fn (): bool => true,
80
                ),
81
                [
82
                    'targetAttribute' => 'test',
83
                    'incorrectInputMessage' => [
84
                        'template' => 'Custom message 1.',
85
                        'parameters' => [
86
                            'targetAttribute' => 'test',
87
                        ],
88
                    ],
89
                    'incorrectDataSetTypeMessage' => [
90
                        'template' => 'Custom message 2.',
91
                        'parameters' => [
92
                            'targetAttribute' => 'test',
93
                        ],
94
                    ],
95
                    'message' => [
96
                        'template' => 'Custom message 3.',
97
                        'parameters' => [
98
                            'targetAttribute' => 'test',
99
                        ],
100
                    ],
101
                    'type' => 'original',
102
                    'operator' => '===',
103
                    'skipOnEmpty' => true,
104
                    'skipOnError' => true,
105
                ],
106
            ],
107
        ];
108
    }
109
110
    public function dataValidationPassed(): array
111
    {
112
        return [
113
            [100, [new Equal(100)]],
114
            ['100', [new Equal(100)]],
115
        ];
116
    }
117
118
    public function dataValidationFailed(): array
119
    {
120
        return [
121
            [101, [new Equal(100)], ['' => ['Value must be equal to "100".']]],
122
            ['100', [new Equal(100, strict: true)], ['' => ['Value must be strictly equal to "100".']]],
123
            'custom error' => [101, [new Equal(100, message: 'Custom error')], ['' => ['Custom error']]],
124
        ];
125
    }
126
127
    public function testSkipOnError(): void
128
    {
129
        $this->testskipOnErrorInternal(new Equal(1), new Equal(1, skipOnError: true));
130
    }
131
132
    public function testWhen(): void
133
    {
134
        $when = static fn (mixed $value): bool => $value !== null;
135
        $this->testWhenInternal(new Equal(1), new Equal(1, when: $when));
136
    }
137
}
138