Passed
Pull Request — master (#447)
by Sergei
03:14
created

EachTest::dataValidationFailed()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 107
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 62
nc 1
nop 0
dl 0
loc 107
rs 8.829
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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