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

GreaterThanTest::testWhen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\GreaterThan;
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 GreaterThanTest extends RuleTestCase
16
{
17
    use RuleWithOptionsTestTrait;
18
    use SkipOnErrorTestTrait;
19
    use WhenTestTrait;
20
21
    public function testGetName(): void
22
    {
23
        $rule = new GreaterThan(1);
24
        $this->assertSame('greaterThan', $rule->getName());
25
    }
26
27
    public function dataOptions(): array
28
    {
29
        return [
30
            [
31
                new GreaterThan(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 be greater than "{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 GreaterThan(
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
                    skipOnEmpty: true,
74
                    skipOnError: true,
75
                    when: static fn (): bool => true,
76
                ),
77
                [
78
                    'targetAttribute' => 'test',
79
                    'incorrectInputMessage' => [
80
                        'template' => 'Custom message 1.',
81
                        'parameters' => [
82
                            'targetAttribute' => 'test',
83
                        ],
84
                    ],
85
                    'incorrectDataSetTypeMessage' => [
86
                        'template' => 'Custom message 2.',
87
                        'parameters' => [
88
                            'targetAttribute' => 'test',
89
                        ],
90
                    ],
91
                    'message' => [
92
                        'template' => 'Custom message 3.',
93
                        'parameters' => [
94
                            'targetAttribute' => 'test',
95
                        ],
96
                    ],
97
                    'type' => 'original',
98
                    'operator' => '>',
99
                    'skipOnEmpty' => true,
100
                    'skipOnError' => true,
101
                ],
102
            ],
103
        ];
104
    }
105
106
    public function dataValidationPassed(): array
107
    {
108
        return [
109
            [100, [new GreaterThan(99)]],
110
            ['100', [new GreaterThan('99')]],
111
        ];
112
    }
113
114
    public function dataValidationFailed(): array
115
    {
116
        $message = 'Value must be greater than "100".';
117
118
        return [
119
            [99, [new GreaterThan(100)], ['' => [$message]]],
120
            ['100', [new GreaterThan(100)], ['' => [$message]]],
121
            'custom error' => [99, [new GreaterThan(100, message: 'Custom error')], ['' => ['Custom error']]],
122
        ];
123
    }
124
125
    public function testSkipOnError(): void
126
    {
127
        $this->testSkipOnErrorInternal(new GreaterThan(1), new GreaterThan(1, skipOnError: true));
128
    }
129
130
    public function testWhen(): void
131
    {
132
        $when = static fn (mixed $value): bool => $value !== null;
133
        $this->testWhenInternal(new GreaterThan(1), new GreaterThan(1, when: $when));
134
    }
135
}
136