Passed
Pull Request — master (#550)
by Alexander
05:42 queued 02:37
created

EachTest::testGetOptionsWithNotRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 Generator;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\Each;
10
use Yiisoft\Validator\Rule\EachHandler;
11
use Yiisoft\Validator\Rule\Number;
12
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
13
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
14
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
15
use Yiisoft\Validator\Tests\Rule\Base\RuleWithProvidedRulesTrait;
16
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
17
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
18
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions;
19
20
final class EachTest extends RuleTestCase
21
{
22
    use DifferentRuleInHandlerTestTrait;
23
    use RuleWithOptionsTestTrait;
24
    use RuleWithProvidedRulesTrait;
25
    use SkipOnErrorTestTrait;
26
    use WhenTestTrait;
27
28
    public function testGetName(): void
29
    {
30
        $rule = new Each();
31
        $this->assertSame('each', $rule->getName());
32
    }
33
34
    public function dataOptions(): array
35
    {
36
        return [
37
            [
38
                new Each([
39
                    new Number(max: 13, pattern: '/1/'),
40
                    new Number(max: 14, pattern: '/2/'),
41
                ]),
42
                [
43
                    'incorrectInputMessage' => [
44
                        'template' => 'Value must be array or iterable.',
45
                        'parameters' => [],
46
                    ],
47
                    'incorrectInputKeyMessage' => [
48
                        'template' => 'Every iterable key must have an integer or a string type.',
49
                        'parameters' => [],
50
                    ],
51
                    'skipOnEmpty' => false,
52
                    'skipOnError' => false,
53
                    'rules' => [
54
                        [
55
                            'number',
56
                            'min' => null,
57
                            'max' => 13,
58
                            'incorrectInputMessage' => [
59
                                'template' => 'The allowed types are integer, float and string.',
60
                                'parameters' => [],
61
                            ],
62
                            'notNumberMessage' => [
63
                                'template' => 'Value must be a number.',
64
                                'parameters' => [],
65
                            ],
66
                            'lessThanMinMessage' => [
67
                                'template' => 'Value must be no less than {min}.',
68
                                'parameters' => ['min' => null],
69
                            ],
70
                            'greaterThanMaxMessage' => [
71
                                'template' => 'Value must be no greater than {max}.',
72
                                'parameters' => ['max' => 13],
73
                            ],
74
                            'skipOnEmpty' => false,
75
                            'skipOnError' => false,
76
                            'pattern' => '/1/',
77
                        ],
78
                        [
79
                            'number',
80
                            'min' => null,
81
                            'max' => 14,
82
                            'incorrectInputMessage' => [
83
                                'template' => 'The allowed types are integer, float and string.',
84
                                'parameters' => [],
85
                            ],
86
                            'notNumberMessage' => [
87
                                'template' => 'Value must be a number.',
88
                                'parameters' => [],
89
                            ],
90
                            'lessThanMinMessage' => [
91
                                'template' => 'Value must be no less than {min}.',
92
                                'parameters' => ['min' => null],
93
                            ],
94
                            'greaterThanMaxMessage' => [
95
                                'template' => 'Value must be no greater than {max}.',
96
                                'parameters' => ['max' => 14],
97
                            ],
98
                            'skipOnEmpty' => false,
99
                            'skipOnError' => false,
100
                            'pattern' => '/2/',
101
                        ],
102
                    ],
103
                ],
104
            ],
105
            'rule without options' => [
106
                new Each([
107
                    new Number(max: 13, pattern: '/1/'),
108
                    new RuleWithoutOptions(),
109
                ]),
110
                [
111
                    'incorrectInputMessage' => [
112
                        'template' => 'Value must be array or iterable.',
113
                        'parameters' => [],
114
                    ],
115
                    'incorrectInputKeyMessage' => [
116
                        'template' => 'Every iterable key must have an integer or a string type.',
117
                        'parameters' => [],
118
                    ],
119
                    'skipOnEmpty' => false,
120
                    'skipOnError' => false,
121
                    'rules' => [
122
                        [
123
                            'number',
124
                            'min' => null,
125
                            'max' => 13,
126
                            'incorrectInputMessage' => [
127
                                'template' => 'The allowed types are integer, float and string.',
128
                                'parameters' => [],
129
                            ],
130
                            'notNumberMessage' => [
131
                                'template' => 'Value must be a number.',
132
                                'parameters' => [],
133
                            ],
134
                            'lessThanMinMessage' => [
135
                                'template' => 'Value must be no less than {min}.',
136
                                'parameters' => ['min' => null],
137
                            ],
138
                            'greaterThanMaxMessage' => [
139
                                'template' => 'Value must be no greater than {max}.',
140
                                'parameters' => ['max' => 13],
141
                            ],
142
                            'skipOnEmpty' => false,
143
                            'skipOnError' => false,
144
                            'pattern' => '/1/',
145
                        ],
146
                        [
147
                            'test',
148
                        ],
149
                    ],
150
                ],
151
            ],
152
        ];
153
    }
154
155
    public function testGetOptionsWithNotRule(): void
156
    {
157
        $this->testGetOptionsWithNotRuleInternal(Each::class);
158
    }
159
160
    public function dataValidationPassed(): array
161
    {
162
        return [
163
            [
164
                [10, 11],
165
                [new Each([new Number(max: 20)])],
166
            ],
167
        ];
168
    }
169
170
    public function dataValidationFailed(): array
171
    {
172
        $getGeneratorWithIncorrectKey = static function (): Generator {
173
            yield false => 0;
174
        };
175
176
        return [
177
            'incorrect input' => [1, [new Each([new Number(max: 13)])], ['' => ['Value must be array or iterable.']]],
178
            'custom incorrect input message' => [
179
                1,
180
                [new Each([new Number(max: 13)], incorrectInputMessage: 'Custom incorrect input message.')],
181
                ['' => ['Custom incorrect input message.']],
182
            ],
183
            'custom incorrect input message with parameters' => [
184
                1,
185
                [new Each([new Number(max: 13)], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
186
                ['' => ['Attribute - , type - int.']],
187
            ],
188
            'custom incorrect input message with parameters, attribute set' => [
189
                ['data' => 1],
190
                [
191
                    'data' => new Each(
192
                        [new Number(max: 13)],
193
                        incorrectInputMessage: 'Attribute - {attribute}, type - {type}.',
194
                    ),
195
                ],
196
                ['data' => ['Attribute - data, type - int.']],
197
            ],
198
199
            'incorrect input key' => [
200
                ['attribute' => $getGeneratorWithIncorrectKey()],
201
                ['attribute' => new Each([new Number(max: 13)])],
202
                ['attribute' => ['Every iterable key must have an integer or a string type.']],
203
            ],
204
            'custom incorrect input key message' => [
205
                ['attribute' => $getGeneratorWithIncorrectKey()],
206
                [
207
                    'attribute' => new Each(
208
                        [new Number(max: 13)],
209
                        incorrectInputKeyMessage: 'Custom incorrect input key message.',
210
                    ),
211
                ],
212
                ['attribute' => ['Custom incorrect input key message.']],
213
            ],
214
            'custom incorrect input key message with parameters' => [
215
                ['attribute' => $getGeneratorWithIncorrectKey()],
216
                [
217
                    'attribute' => new Each(
218
                        [new Number(max: 13)],
219
                        incorrectInputKeyMessage: 'Attribute - {attribute}, type - {type}.',
220
                    ),
221
                ],
222
                ['attribute' => ['Attribute - attribute, type - Generator.']],
223
            ],
224
225
            [
226
                [10, 20, 30],
227
                [new Each([new Number(max: 13)])],
228
                [
229
                    '1' => ['Value must be no greater than 13.'],
230
                    '2' => ['Value must be no greater than 13.'],
231
                ],
232
            ],
233
234
            'single rule' => [
235
                [10, 20, 30],
236
                [new Each(new Number(max: 13))],
237
                [
238
                    '1' => ['Value must be no greater than 13.'],
239
                    '2' => ['Value must be no greater than 13.'],
240
                ],
241
            ],
242
            'single callable rule' => [
243
                [10, 20],
244
                [new Each(static fn (): Result => (new Result())->addError('error'))],
245
                [
246
                    0 => ['error'],
247
                    1 => ['error'],
248
                ],
249
            ],
250
            'rules array with callable' => [
251
                [10, 20],
252
                [new Each([static fn (): Result => (new Result())->addError('error')])],
253
                [
254
                    0 => ['error'],
255
                    1 => ['error'],
256
                ],
257
            ],
258
259
            'custom message' => [
260
                [10, 20, 30],
261
                [new Each([new Number(max: 13, greaterThanMaxMessage: 'Custom too big message.')])],
262
                [
263
                    '1' => ['Custom too big message.'],
264
                    '2' => ['Custom too big message.'],
265
                ],
266
            ],
267
            'custom message with parameters' => [
268
                [10, 20, 30],
269
                [
270
                    new Each(
271
                        [new Number(max: 13, greaterThanMaxMessage: 'Max - {max}, value - {value}.')],
272
                    ),
273
                ],
274
                [
275
                    '1' => ['Max - 13, value - 20.'],
276
                    '2' => ['Max - 13, value - 30.'],
277
                ],
278
            ],
279
        ];
280
    }
281
282
    public function testSkipOnError(): void
283
    {
284
        $this->testSkipOnErrorInternal(new Each(), new Each(skipOnError: true));
285
    }
286
287
    public function testWhen(): void
288
    {
289
        $when = static fn (mixed $value): bool => $value !== null;
290
        $this->testWhenInternal(new Each(), new Each(when: $when));
291
    }
292
293
    protected function getDifferentRuleInHandlerItems(): array
294
    {
295
        return [Each::class, EachHandler::class];
296
    }
297
}
298