Passed
Pull Request — master (#619)
by Alexander
06:58 queued 03:19
created

NumberTest::testIntegerEmptyPattern()   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 stdClass;
9
use Yiisoft\Validator\Rule\Integer;
10
use Yiisoft\Validator\Rule\Number;
11
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15
16
final class NumberTest extends RuleTestCase
17
{
18
    use RuleWithOptionsTestTrait;
19
    use SkipOnErrorTestTrait;
20
    use WhenTestTrait;
21
22
    public function testNumberEmptyPattern(): void
23
    {
24
        $this->expectException(InvalidArgumentException::class);
25
        $this->expectExceptionMessage('Pattern can\'t be empty.');
26
        new Number(pattern: '');
27
    }
28
29
    public function testIntegerEmptyPattern(): void
30
    {
31
        $this->expectException(InvalidArgumentException::class);
32
        $this->expectExceptionMessage('Pattern can\'t be empty.');
33
        new Integer(pattern: '');
34
    }
35
36
    public function testGetName(): void
37
    {
38
        $rule = new Number();
39
        $this->assertSame('number', $rule->getName());
40
    }
41
42
    public function dataOptions(): array
43
    {
44
        return [
45
            [
46
                new Number(),
47
                [
48
                    'min' => null,
49
                    'max' => null,
50
                    'incorrectInputMessage' => [
51
                        'template' => 'The allowed types are integer, float and string.',
52
                        'parameters' => [],
53
                    ],
54
                    'notNumberMessage' => [
55
                        'template' => 'Value must be a number.',
56
                        'parameters' => [],
57
                    ],
58
                    'lessThanMinMessage' => [
59
                        'template' => 'Value must be no less than {min}.',
60
                        'parameters' => ['min' => null],
61
                    ],
62
                    'greaterThanMaxMessage' => [
63
                        'template' => 'Value must be no greater than {max}.',
64
                        'parameters' => ['max' => null],
65
                    ],
66
                    'skipOnEmpty' => false,
67
                    'skipOnError' => false,
68
                    'pattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
69
                ],
70
            ],
71
            [
72
                new Number(min: 1),
73
                [
74
                    'min' => 1,
75
                    'max' => null,
76
                    'incorrectInputMessage' => [
77
                        'template' => 'The allowed types are integer, float and string.',
78
                        'parameters' => [],
79
                    ],
80
                    'notNumberMessage' => [
81
                        'template' => 'Value must be a number.',
82
                        'parameters' => [],
83
                    ],
84
                    'lessThanMinMessage' => [
85
                        'template' => 'Value must be no less than {min}.',
86
                        'parameters' => ['min' => 1],
87
                    ],
88
                    'greaterThanMaxMessage' => [
89
                        'template' => 'Value must be no greater than {max}.',
90
                        'parameters' => ['max' => null],
91
                    ],
92
                    'skipOnEmpty' => false,
93
                    'skipOnError' => false,
94
                    'pattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
95
                ],
96
            ],
97
            [
98
                new Number(max: 1),
99
                [
100
                    'min' => null,
101
                    'max' => 1,
102
                    'incorrectInputMessage' => [
103
                        'template' => 'The allowed types are integer, float and string.',
104
                        'parameters' => [],
105
                    ],
106
                    'notNumberMessage' => [
107
                        'template' => 'Value must be a number.',
108
                        'parameters' => [],
109
                    ],
110
                    'lessThanMinMessage' => [
111
                        'template' => 'Value must be no less than {min}.',
112
                        'parameters' => ['min' => null],
113
                    ],
114
                    'greaterThanMaxMessage' => [
115
                        'template' => 'Value must be no greater than {max}.',
116
                        'parameters' => ['max' => 1],
117
                    ],
118
                    'skipOnEmpty' => false,
119
                    'skipOnError' => false,
120
                    'pattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
121
                ],
122
            ],
123
            [
124
                new Number(min: 2, max: 10),
125
                [
126
                    'min' => 2,
127
                    'max' => 10,
128
                    'incorrectInputMessage' => [
129
                        'template' => 'The allowed types are integer, float and string.',
130
                        'parameters' => [],
131
                    ],
132
                    'notNumberMessage' => [
133
                        'template' => 'Value must be a number.',
134
                        'parameters' => [],
135
                    ],
136
                    'lessThanMinMessage' => [
137
                        'template' => 'Value must be no less than {min}.',
138
                        'parameters' => ['min' => 2],
139
                    ],
140
                    'greaterThanMaxMessage' => [
141
                        'template' => 'Value must be no greater than {max}.',
142
                        'parameters' => ['max' => 10],
143
                    ],
144
                    'skipOnEmpty' => false,
145
                    'skipOnError' => false,
146
                    'pattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
147
                ],
148
            ],
149
            [
150
                new Integer(),
151
                [
152
                    'min' => null,
153
                    'max' => null,
154
                    'incorrectInputMessage' => [
155
                        'template' => 'The allowed types are integer, float and string.',
156
                        'parameters' => [],
157
                    ],
158
                    'notNumberMessage' => [
159
                        'template' => 'Value must be an integer.',
160
                        'parameters' => [],
161
                    ],
162
                    'lessThanMinMessage' => [
163
                        'template' => 'Value must be no less than {min}.',
164
                        'parameters' => ['min' => null],
165
                    ],
166
                    'greaterThanMaxMessage' => [
167
                        'template' => 'Value must be no greater than {max}.',
168
                        'parameters' => ['max' => null],
169
                    ],
170
                    'skipOnEmpty' => false,
171
                    'skipOnError' => false,
172
                    'pattern' => '/^\s*[+-]?\d+\s*$/',
173
                ],
174
            ],
175
        ];
176
    }
177
178
    public function dataValidationPassed(): array
179
    {
180
        return [
181
            [20, [new Number()]],
182
            [0, [new Number()]],
183
            [.5, [new Number()]],
184
            [-20, [new Number()]],
185
            ['20', [new Number()]],
186
            [25.45, [new Number()]],
187
            ['25,45', [new Number()]],
188
            ['-1.23', [new Number()]],
189
            ['-4.423e-12', [new Number()]],
190
            ['12E3', [new Number()]],
191
192
            [20, [new Integer()]],
193
            [0, [new Integer()]],
194
            ['20', [new Integer()]],
195
            ['020', [new Integer()]],
196
            [0x14, [new Integer()]],
197
            ['5.5e1', [new Number()]],
198
199
            [1, [new Number(min: 1)]],
200
            [PHP_INT_MAX + 1, [new Number(min: 1)]],
201
202
            [1, [new Integer(min: 1)]],
203
204
            [1, [new Number(max: 1)]],
205
            [1, [new Number(max: 1.25)]],
206
            ['22e-12', [new Number(max: 1.25)]],
207
            ['125e-2', [new Number(max: 1.25)]],
208
            [1, [new Integer(max: 1.25)]],
209
210
            [0, [new Number(min: -10, max: 20)]],
211
            [-10, [new Number(min: -10, max: 20)]],
212
213
            [0, [new Integer(min: -10, max: 20)]],
214
        ];
215
    }
216
217
    public function dataValidationFailed(): array
218
    {
219
        $incorrectInputMessage = 'The allowed types are integer, float and string.';
220
        $notNumberMessage = 'Value must be a number.';
221
        $notIntegerMessage = 'Value must be an integer.';
222
223
        return [
224
            [false, [new Number()], ['' => [$incorrectInputMessage]]],
225
            [true, [new Number()], ['' => [$incorrectInputMessage]]],
226
            [[1, 2, 3], [new Number()], ['' => [$incorrectInputMessage]]],
227
            [new stdClass(), [new Number()], ['' => [$incorrectInputMessage]]],
228
            [fopen('php://stdin', 'rb'), [new Number()], ['' => [$incorrectInputMessage]]],
229
230
            ['12:45', [new Number()], ['' => [$notNumberMessage]]],
231
            ['e12', [new Number()], ['' => [$notNumberMessage]]],
232
            ['-e3', [new Number()], ['' => [$notNumberMessage]]],
233
            ['-4.534-e-12', [new Number()], ['' => [$notNumberMessage]]],
234
            ['12.23^4', [new Number()], ['' => [$notNumberMessage]]],
235
            ['43^32', [new Number()], ['' => [$notNumberMessage]]],
236
237
            [25.45, [new Integer()], ['' => [$notIntegerMessage]]],
238
            ['25,45', [new Integer()], ['' => [$notIntegerMessage]]],
239
            ['0x14', [new Integer()], ['' => [$notIntegerMessage]]],
240
241
            ['-1.23', [new Integer()], ['' => [$notIntegerMessage]]],
242
            ['-4.423e-12', [new Integer()], ['' => [$notIntegerMessage]]],
243
            ['12E3', [new Integer()], ['' => [$notIntegerMessage]]],
244
            ['e12', [new Integer()], ['' => [$notIntegerMessage]]],
245
            ['-e3', [new Integer()], ['' => [$notIntegerMessage]]],
246
            ['-4.534-e-12', [new Integer()], ['' => [$notIntegerMessage]]],
247
            ['12.23^4', [new Integer()], ['' => [$notIntegerMessage]]],
248
249
            [-1, [new Number(min: 1)], ['' => ['Value must be no less than 1.']]],
250
            ['22e-12', [new Number(min: 1)], ['' => ['Value must be no less than 1.']]],
251
252
            [-1, [new Integer(min: 1)], ['' => ['Value must be no less than 1.']]],
253
            ['22e-12', [new Integer(min: 1)], ['' => [$notIntegerMessage]]],
254
            [1.5, [new Number(max: 1.25)], ['' => ['Value must be no greater than 1.25.']]],
255
256
            // TODO: fix wrong message
257
            [1.5, [new Integer(max: 1.25)], ['' => [$notIntegerMessage]]],
258
            ['22e-12', [new Integer(max: 1.25)], ['' => [$notIntegerMessage]]],
259
            ['125e-2', [new Integer(max: 1.25)], ['' => [$notIntegerMessage]]],
260
261
            [-11, [new Number(min: -10, max: 20)], ['' => ['Value must be no less than -10.']]],
262
            [21, [new Number(min: -10, max: 20)], ['' => ['Value must be no greater than 20.']]],
263
            [-11, [new Integer(min: -10, max: 20)], ['' => ['Value must be no less than -10.']]],
264
            [22, [new Integer(min: -10, max: 20)], ['' => ['Value must be no greater than 20.']]],
265
            ['20e-1', [new Integer(min: -10, max: 20)], ['' => [$notIntegerMessage]]],
266
            'custom error' => [
267
                0,
268
                [new Number(min: 5, lessThanMinMessage: 'Value is too small.')],
269
                ['' => ['Value is too small.']],
270
            ],
271
        ];
272
    }
273
274
    public function testSkipOnError(): void
275
    {
276
        $this->testSkipOnErrorInternal(new Number(), new Number(skipOnError: true));
277
    }
278
279
    public function testWhen(): void
280
    {
281
        $when = static fn (mixed $value): bool => $value !== null;
282
        $this->testWhenInternal(new Number(), new Number(when: $when));
283
    }
284
}
285