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

GreaterThanOrEqualTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 125
c 1
b 0
f 0
dl 0
loc 198
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetName() 0 4 1
A testSkipOnError() 0 3 1
B dataOptions() 0 146 1
A dataValidationPassed() 0 5 1
A dataValidationFailed() 0 8 1
A testWhen() 0 4 1
A testInitWithoutTarget() 0 5 1
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\GreaterThanOrEqual;
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 GreaterThanOrEqualTest extends RuleTestCase
16
{
17
    use RuleWithOptionsTestTrait;
18
    use SkipOnErrorTestTrait;
19
    use WhenTestTrait;
20
21
    public function testGetName(): void
22
    {
23
        $rule = new GreaterThanOrEqual(1);
24
        $this->assertSame('greaterThanOrEqual', $rule->getName());
25
    }
26
27
    public function dataOptions(): array
28
    {
29
        return [
30
            [
31
                new GreaterThanOrEqual(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 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 GreaterThanOrEqual(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 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 GreaterThanOrEqual(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 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 GreaterThanOrEqual(
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 GreaterThanOrEqual(99)]],
182
            ['100', [new GreaterThanOrEqual('100')]],
183
        ];
184
    }
185
186
    public function dataValidationFailed(): array
187
    {
188
        $message = 'Value must be greater than or equal to "100".';
189
190
        return [
191
            [99, [new GreaterThanOrEqual(100)], ['' => [$message]]],
192
            ['99', [new GreaterThanOrEqual(100)], ['' => [$message]]],
193
            'custom error' => [99, [new GreaterThanOrEqual(100, message: 'Custom error')], ['' => ['Custom error']]],
194
        ];
195
    }
196
197
    public function testInitWithoutTarget(): void
198
    {
199
        $this->expectException(InvalidArgumentException::class);
200
        $this->expectExceptionMessage('Either "targetValue" or "targetAttribute" must be specified');
201
        new GreaterThanOrEqual();
202
    }
203
204
    public function testSkipOnError(): void
205
    {
206
        $this->testSkipOnErrorInternal(new GreaterThanOrEqual(1), new GreaterThanOrEqual(1, skipOnError: true));
207
    }
208
209
    public function testWhen(): void
210
    {
211
        $when = static fn (mixed $value): bool => $value !== null;
212
        $this->testWhenInternal(new GreaterThanOrEqual(1), new GreaterThanOrEqual(1, when: $when));
213
    }
214
}
215