Passed
Pull Request — master (#542)
by
unknown
02:35
created

GreaterThanTest::testWithoutParameters()   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 InvalidArgumentException;
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.',
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' => 'string',
60
                    'operator' => '>',
61
                    'skipOnEmpty' => false,
62
                    'skipOnError' => false,
63
                ],
64
            ],
65
            [
66
                new GreaterThan(1, type: CompareType::NUMBER),
67
                [
68
                    'targetValue' => 1,
69
                    'targetAttribute' => null,
70
                    'incorrectInputMessage' => [
71
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
72
                        'parameters' => [
73
                            'targetValue' => 1,
74
                            'targetAttribute' => null,
75
                            'targetValueOrAttribute' => 1,
76
                        ],
77
                    ],
78
                    'incorrectDataSetTypeMessage' => [
79
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
80
                        'parameters' => [
81
                            'targetValue' => 1,
82
                            'targetAttribute' => null,
83
                            'targetValueOrAttribute' => 1,
84
                        ],
85
                    ],
86
                    'message' => [
87
                        'template' => 'Value must be greater than "{targetValueOrAttribute}".',
88
                        'parameters' => [
89
                            'targetValue' => 1,
90
                            'targetAttribute' => null,
91
                            'targetValueOrAttribute' => 1,
92
                        ],
93
                    ],
94
                    'type' => 'number',
95
                    'operator' => '>',
96
                    'skipOnEmpty' => false,
97
                    'skipOnError' => false,
98
                ],
99
            ],
100
            [
101
                new GreaterThan(null, 'attribute'),
102
                [
103
                    'targetValue' => null,
104
                    'targetAttribute' => 'attribute',
105
                    'incorrectInputMessage' => [
106
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
107
                        'parameters' => [
108
                            'targetValue' => null,
109
                            'targetAttribute' => 'attribute',
110
                            'targetValueOrAttribute' => 'attribute',
111
                        ],
112
                    ],
113
                    'incorrectDataSetTypeMessage' => [
114
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
115
                        'parameters' => [
116
                            'targetValue' => null,
117
                            'targetAttribute' => 'attribute',
118
                            'targetValueOrAttribute' => 'attribute',
119
                        ],
120
                    ],
121
                    'message' => [
122
                        'template' => 'Value must be greater than "{targetValueOrAttribute}".',
123
                        'parameters' => [
124
                            'targetValue' => null,
125
                            'targetAttribute' => 'attribute',
126
                            'targetValueOrAttribute' => 'attribute',
127
                        ],
128
                    ],
129
                    'type' => 'string',
130
                    'operator' => '>',
131
                    'skipOnEmpty' => false,
132
                    'skipOnError' => false,
133
                ],
134
            ],
135
            [
136
                new GreaterThan(
137
                    targetAttribute: 'test',
138
                    incorrectInputMessage: 'Custom message 1.',
139
                    incorrectDataSetTypeMessage: 'Custom message 2.',
140
                    message: 'Custom message 3.',
141
                ),
142
                [
143
                    'targetValue' => null,
144
                    'targetAttribute' => 'test',
145
                    'incorrectInputMessage' => [
146
                        'template' => 'Custom message 1.',
147
                        'parameters' => [
148
                            'targetValue' => null,
149
                            'targetAttribute' => 'test',
150
                            'targetValueOrAttribute' => 'test',
151
                        ],
152
                    ],
153
                    'incorrectDataSetTypeMessage' => [
154
                        'template' => 'Custom message 2.',
155
                        'parameters' => [
156
                            'targetValue' => null,
157
                            'targetAttribute' => 'test',
158
                            'targetValueOrAttribute' => 'test',
159
                        ],
160
                    ],
161
                    'message' => [
162
                        'template' => 'Custom message 3.',
163
                        'parameters' => [
164
                            'targetValue' => null,
165
                            'targetAttribute' => 'test',
166
                            'targetValueOrAttribute' => 'test',
167
                        ],
168
                    ],
169
                    'type' => 'string',
170
                    'operator' => '>',
171
                    'skipOnEmpty' => false,
172
                    'skipOnError' => false,
173
                ],
174
            ],
175
        ];
176
    }
177
178
    public function dataValidationPassed(): array
179
    {
180
        return [
181
            [100, [new GreaterThan(99)]],
182
            ['100', [new GreaterThan('99')]],
183
        ];
184
    }
185
186
    public function dataValidationFailed(): array
187
    {
188
        $message = 'Value must be greater than "100".';
189
190
        return [
191
            [99, [new GreaterThan(100)], ['' => [$message]]],
192
            ['100', [new GreaterThan(100)], ['' => [$message]]],
193
            'custom error' => [99, [new GreaterThan(100, message: 'Custom error')], ['' => ['Custom error']]],
194
        ];
195
    }
196
197
    public function testSkipOnError(): void
198
    {
199
        $this->testSkipOnErrorInternal(new GreaterThan(1), new GreaterThan(1, skipOnError: true));
200
    }
201
202
    public function testWhen(): void
203
    {
204
        $when = static fn (mixed $value): bool => $value !== null;
205
        $this->testWhenInternal(new GreaterThan(1), new GreaterThan(1, when: $when));
206
    }
207
}
208