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

LessThanOrEqualTest::testInitWithoutTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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 InvalidArgumentException;
8
use Yiisoft\Validator\Rule\CompareType;
9
use Yiisoft\Validator\Rule\LessThanOrEqual;
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 LessThanOrEqualTest extends RuleTestCase
16
{
17
    use RuleWithOptionsTestTrait;
18
    use SkipOnErrorTestTrait;
19
    use WhenTestTrait;
20
21
    public function testGetName(): void
22
    {
23
        $rule = new LessThanOrEqual(1);
24
        $this->assertSame('lessThanOrEqual', $rule->getName());
25
    }
26
27
    public function dataOptions(): array
28
    {
29
        return [
30
            [
31
                new LessThanOrEqual(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 less than or equal to "{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 LessThanOrEqual(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 less than or equal to "{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 LessThanOrEqual(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 less than or equal to "{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 LessThanOrEqual(
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 LessThanOrEqual(101)]],
182
            [100, [new LessThanOrEqual(100)]],
183
            ['100', [new LessThanOrEqual('101')]],
184
        ];
185
    }
186
187
    public function dataValidationFailed(): array
188
    {
189
        $message = 'Value must be less than or equal to "100".';
190
191
        return [
192
            [101, [new LessThanOrEqual(100)], ['' => [$message]]],
193
            ['101', [new LessThanOrEqual(100)], ['' => [$message]]],
194
            'custom error' => [101, [new LessThanOrEqual(100, message: 'Custom error')], ['' => ['Custom error']]],
195
        ];
196
    }
197
198
    public function testInitWithoutTarget(): void
199
    {
200
        $this->expectException(InvalidArgumentException::class);
201
        $this->expectExceptionMessage('Either "targetValue" or "targetAttribute" must be specified');
202
        new LessThanOrEqual();
203
    }
204
205
    public function testSkipOnError(): void
206
    {
207
        $this->testSkipOnErrorInternal(new LessThanOrEqual(1), new LessThanOrEqual(1, skipOnError: true));
208
    }
209
210
    public function testWhen(): void
211
    {
212
        $when = static fn (mixed $value): bool => $value !== null;
213
        $this->testWhenInternal(new LessThanOrEqual(1), new LessThanOrEqual(1, when: $when));
214
    }
215
}
216