LessThanTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 73
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 6

6 Methods

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