Passed
Pull Request — master (#521)
by
unknown
02:50
created

NotEqualTest::dataValidationPassed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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