Passed
Pull Request — master (#542)
by Alexander
05:06 queued 02:25
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 Yiisoft\Validator\Rule\CompareType;
8
use Yiisoft\Validator\Rule\GreaterThan;
9
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
10
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
11
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
13
14
final class GreaterThanTest extends RuleTestCase
15
{
16
    use RuleWithOptionsTestTrait;
17
    use SkipOnErrorTestTrait;
18
    use WhenTestTrait;
19
20
    public function testGetName(): void
21
    {
22
        $rule = new GreaterThan(1);
23
        $this->assertSame('greaterThan', $rule->getName());
24
    }
25
26
    public function dataOptions(): array
27
    {
28
        return [
29
            [
30
                new GreaterThan(1),
31
                [
32
                    'targetValue' => 1,
33
                    'targetAttribute' => null,
34
                    'incorrectInputMessage' => [
35
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
36
                        'parameters' => [
37
                            'targetValue' => 1,
38
                            'targetAttribute' => null,
39
                            'targetValueOrAttribute' => 1,
40
                        ],
41
                    ],
42
                    'incorrectDataSetTypeMessage' => [
43
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
44
                        'parameters' => [
45
                            'targetValue' => 1,
46
                            'targetAttribute' => null,
47
                            'targetValueOrAttribute' => 1,
48
                        ],
49
                    ],
50
                    'message' => [
51
                        'template' => 'Value must be greater than "{targetValueOrAttribute}".',
52
                        'parameters' => [
53
                            'targetValue' => 1,
54
                            'targetAttribute' => null,
55
                            'targetValueOrAttribute' => 1,
56
                        ],
57
                    ],
58
                    'type' => 'string',
59
                    'operator' => '>',
60
                    'skipOnEmpty' => false,
61
                    'skipOnError' => false,
62
                ],
63
            ],
64
            [
65
                new GreaterThan(1, type: CompareType::NUMBER),
66
                [
67
                    'targetValue' => 1,
68
                    'targetAttribute' => null,
69
                    'incorrectInputMessage' => [
70
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
71
                        'parameters' => [
72
                            'targetValue' => 1,
73
                            'targetAttribute' => null,
74
                            'targetValueOrAttribute' => 1,
75
                        ],
76
                    ],
77
                    'incorrectDataSetTypeMessage' => [
78
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
79
                        'parameters' => [
80
                            'targetValue' => 1,
81
                            'targetAttribute' => null,
82
                            'targetValueOrAttribute' => 1,
83
                        ],
84
                    ],
85
                    'message' => [
86
                        'template' => 'Value must be greater than "{targetValueOrAttribute}".',
87
                        'parameters' => [
88
                            'targetValue' => 1,
89
                            'targetAttribute' => null,
90
                            'targetValueOrAttribute' => 1,
91
                        ],
92
                    ],
93
                    'type' => 'number',
94
                    'operator' => '>',
95
                    'skipOnEmpty' => false,
96
                    'skipOnError' => false,
97
                ],
98
            ],
99
            [
100
                new GreaterThan(null, 'attribute'),
101
                [
102
                    'targetValue' => null,
103
                    'targetAttribute' => 'attribute',
104
                    'incorrectInputMessage' => [
105
                        'template' => 'The allowed types are integer, float, string, boolean and null.',
106
                        'parameters' => [
107
                            'targetValue' => null,
108
                            'targetAttribute' => 'attribute',
109
                            'targetValueOrAttribute' => 'attribute',
110
                        ],
111
                    ],
112
                    'incorrectDataSetTypeMessage' => [
113
                        'template' => 'The attribute value returned from a custom data set must have a scalar type.',
114
                        'parameters' => [
115
                            'targetValue' => null,
116
                            'targetAttribute' => 'attribute',
117
                            'targetValueOrAttribute' => 'attribute',
118
                        ],
119
                    ],
120
                    'message' => [
121
                        'template' => 'Value must be greater than "{targetValueOrAttribute}".',
122
                        'parameters' => [
123
                            'targetValue' => null,
124
                            'targetAttribute' => 'attribute',
125
                            'targetValueOrAttribute' => 'attribute',
126
                        ],
127
                    ],
128
                    'type' => 'string',
129
                    'operator' => '>',
130
                    'skipOnEmpty' => false,
131
                    'skipOnError' => false,
132
                ],
133
            ],
134
            [
135
                new GreaterThan(
136
                    targetAttribute: 'test',
137
                    incorrectInputMessage: 'Custom message 1.',
138
                    incorrectDataSetTypeMessage: 'Custom message 2.',
139
                    message: 'Custom message 3.',
140
                ),
141
                [
142
                    'targetValue' => null,
143
                    'targetAttribute' => 'test',
144
                    'incorrectInputMessage' => [
145
                        'template' => 'Custom message 1.',
146
                        'parameters' => [
147
                            'targetValue' => null,
148
                            'targetAttribute' => 'test',
149
                            'targetValueOrAttribute' => 'test',
150
                        ],
151
                    ],
152
                    'incorrectDataSetTypeMessage' => [
153
                        'template' => 'Custom message 2.',
154
                        'parameters' => [
155
                            'targetValue' => null,
156
                            'targetAttribute' => 'test',
157
                            'targetValueOrAttribute' => 'test',
158
                        ],
159
                    ],
160
                    'message' => [
161
                        'template' => 'Custom message 3.',
162
                        'parameters' => [
163
                            'targetValue' => null,
164
                            'targetAttribute' => 'test',
165
                            'targetValueOrAttribute' => 'test',
166
                        ],
167
                    ],
168
                    'type' => 'string',
169
                    'operator' => '>',
170
                    'skipOnEmpty' => false,
171
                    'skipOnError' => false,
172
                ],
173
            ],
174
        ];
175
    }
176
177
    public function dataValidationPassed(): array
178
    {
179
        return [
180
            [100, [new GreaterThan(99)]],
181
            ['100', [new GreaterThan('99')]],
182
        ];
183
    }
184
185
    public function dataValidationFailed(): array
186
    {
187
        $message = 'Value must be greater than "100".';
188
189
        return [
190
            [99, [new GreaterThan(100)], ['' => [$message]]],
191
            ['100', [new GreaterThan(100)], ['' => [$message]]],
192
            'custom error' => [99, [new GreaterThan(100, message: 'Custom error')], ['' => ['Custom error']]],
193
        ];
194
    }
195
196
    public function testSkipOnError(): void
197
    {
198
        $this->testSkipOnErrorInternal(new GreaterThan(1), new GreaterThan(1, skipOnError: true));
199
    }
200
201
    public function testWhen(): void
202
    {
203
        $when = static fn (mixed $value): bool => $value !== null;
204
        $this->testWhenInternal(new GreaterThan(1), new GreaterThan(1, when: $when));
205
    }
206
}
207