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

CompositeTest.php$2 ➔ dataValidationFailed()   B

Complexity

Conditions 1

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 86
cc 1
rs 8.3054

1 Method

Rating   Name   Duplication   Size   Complexity  
A CompositeTest.php$2 ➔ __construct() 0 3 1

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 Yiisoft\Validator\Result;
8
use Yiisoft\Validator\Rule\Composite;
9
use Yiisoft\Validator\Rule\CompositeHandler;
10
use Yiisoft\Validator\Rule\Equal;
11
use Yiisoft\Validator\Rule\Number;
12
use Yiisoft\Validator\Rule\Required;
13
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
15
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
16
use Yiisoft\Validator\Tests\Rule\Base\RuleWithProvidedRulesTrait;
17
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
18
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
19
use Yiisoft\Validator\Tests\Support\Rule\CoordinatesRuleSet;
20
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions;
21
22
final class CompositeTest extends RuleTestCase
23
{
24
    use DifferentRuleInHandlerTestTrait;
25
    use RuleWithOptionsTestTrait;
26
    use RuleWithProvidedRulesTrait;
27
    use SkipOnErrorTestTrait;
28
    use WhenTestTrait;
29
30
    public function testGetName(): void
31
    {
32
        $rule = new Composite([]);
33
        $this->assertSame('composite', $rule->getName());
34
    }
35
36
    public function dataOptions(): array
37
    {
38
        return [
39
            [
40
                new Composite([
41
                    new Number(max: 13, pattern: '/1/'),
42
                    new Number(max: 14, pattern: '/2/'),
43
                ]),
44
                [
45
                    'skipOnEmpty' => false,
46
                    'skipOnError' => false,
47
                    'rules' => [
48
                        [
49
                            'number',
50
                            'min' => null,
51
                            'max' => 13,
52
                            'incorrectInputMessage' => [
53
                                'template' => 'The allowed types are integer, float and string.',
54
                                'parameters' => [],
55
                            ],
56
                            'notNumberMessage' => [
57
                                'template' => 'Value must be a number.',
58
                                'parameters' => [],
59
                            ],
60
                            'lessThanMinMessage' => [
61
                                'template' => 'Value must be no less than {min}.',
62
                                'parameters' => ['min' => null],
63
                            ],
64
                            'greaterThanMaxMessage' => [
65
                                'template' => 'Value must be no greater than {max}.',
66
                                'parameters' => ['max' => 13],
67
                            ],
68
                            'skipOnEmpty' => false,
69
                            'skipOnError' => false,
70
                            'pattern' => '/1/',
71
                        ],
72
                        [
73
                            'number',
74
                            'min' => null,
75
                            'max' => 14,
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' => null],
87
                            ],
88
                            'greaterThanMaxMessage' => [
89
                                'template' => 'Value must be no greater than {max}.',
90
                                'parameters' => ['max' => 14],
91
                            ],
92
                            'skipOnEmpty' => false,
93
                            'skipOnError' => false,
94
                            'pattern' => '/2/',
95
                        ],
96
                    ],
97
                ],
98
            ],
99
            'rule without options' => [
100
                new Composite([
101
                    new Number(max: 13, pattern: '/1/'),
102
                    new RuleWithoutOptions(),
103
                ]),
104
                [
105
                    'skipOnEmpty' => false,
106
                    'skipOnError' => false,
107
                    'rules' => [
108
                        [
109
                            'number',
110
                            'min' => null,
111
                            'max' => 13,
112
                            'incorrectInputMessage' => [
113
                                'template' => 'The allowed types are integer, float and string.',
114
                                'parameters' => [],
115
                            ],
116
                            'notNumberMessage' => [
117
                                'template' => 'Value must be a number.',
118
                                'parameters' => [],
119
                            ],
120
                            'lessThanMinMessage' => [
121
                                'template' => 'Value must be no less than {min}.',
122
                                'parameters' => [
123
                                    'min' => null,
124
                                ],
125
                            ],
126
                            'greaterThanMaxMessage' => [
127
                                'template' => 'Value must be no greater than {max}.',
128
                                'parameters' => [
129
                                    'max' => 13,
130
                                ],
131
                            ],
132
                            'skipOnEmpty' => false,
133
                            'skipOnError' => false,
134
                            'pattern' => '/1/',
135
                        ],
136
                        [
137
                            'test',
138
                        ],
139
                    ],
140
                ],
141
            ],
142
            'callable' => [
143
                new Composite([
144
                    static fn () => (new Result())->addError('Bad value.'),
145
                ]),
146
                [
147
                    'skipOnEmpty' => false,
148
                    'skipOnError' => false,
149
                    'rules' => [
150
                        [
151
                            'callback',
152
                            'method' => null,
153
                            'skipOnEmpty' => false,
154
                            'skipOnError' => false,
155
                        ],
156
                    ],
157
                ],
158
            ],
159
            'inheritance' => [
160
                new class () extends Composite {
161
                    public function getRules(): iterable
162
                    {
163
                        return [
164
                            new Required(),
165
                        ];
166
                    }
167
168
                    public function getOptions(): array
169
                    {
170
                        return [
171
                            'specific-key' => 42,
172
                            'rules' => $this->dumpRulesAsArray(),
173
                        ];
174
                    }
175
                },
176
                [
177
                    'specific-key' => 42,
178
                    'rules' => [
179
                        [
180
                            'required',
181
                            'message' => [
182
                                'template' => 'Value cannot be blank.',
183
                                'parameters' => [],
184
                            ],
185
                            'notPassedMessage' => [
186
                                'template' => 'Value not passed.',
187
                                'parameters' => [],
188
                            ],
189
                            'skipOnError' => false,
190
                        ],
191
                    ],
192
                ],
193
            ],
194
        ];
195
    }
196
197
    public function testGetOptionsWithNotRule(): void
198
    {
199
        $this->testGetOptionsWithNotRuleInternal(Composite::class);
200
    }
201
202
    public function dataValidationPassed(): array
203
    {
204
        return [
205
            [
206
                20,
207
                [
208
                    new Composite(
209
                        [
210
                            new Number(max: 13),
211
                            new Number(max: 14),
212
                        ],
213
                        when: fn () => false,
214
                    ),
215
                ],
216
            ],
217
            'override constructor' => [
218
                20,
219
                [
220
                    new class () extends Composite {
221
                        public function __construct()
222
                        {
223
                        }
224
                    },
225
                ],
226
            ],
227
            [
228
                null,
229
                [
230
                    new Composite(
231
                        [
232
                            new Number(max: 13),
233
                            new Number(max: 14),
234
                        ],
235
                        skipOnEmpty: true,
236
                    ),
237
                ],
238
            ],
239
            'multiple attributes via subclass' => [
240
                ['latitude' => -90, 'longitude' => 180],
241
                [new CoordinatesRuleSet()],
242
            ],
243
        ];
244
    }
245
246
    public function dataValidationFailed(): array
247
    {
248
        return [
249
            'callable' => [
250
                20,
251
                [
252
                    new Composite([
253
                        static fn () => (new Result())->addError('Bad value.'),
254
                        static fn () => (new Result())->addError('Very bad value.'),
255
                    ]),
256
                ],
257
                [
258
                    '' => [
259
                        'Bad value.',
260
                        'Very bad value.',
261
                    ],
262
                ],
263
            ],
264
            'when true' => [
265
                20,
266
                [
267
                    new Composite(
268
                        [new Number(max: 13), new Number(min: 21)],
269
                        when: fn () => true,
270
                    ),
271
                ],
272
                [
273
                    '' => [
274
                        'Value must be no greater than 13.',
275
                        'Value must be no less than 21.',
276
                    ],
277
                ],
278
            ],
279
            'skip on error with previous error' => [
280
                20,
281
                [
282
                    new Equal(19),
283
                    new Composite(
284
                        [new Number(max: 13)],
285
                        skipOnError: true,
286
                    ),
287
                ],
288
                [
289
                    '' => ['Value must be equal to "19".'],
290
                ],
291
            ],
292
            'skip on error without previous error' => [
293
                20,
294
                [
295
                    new Composite(
296
                        [new Number(max: 13)],
297
                        skipOnError: true,
298
                    ),
299
                ],
300
                [
301
                    '' => ['Value must be no greater than 13.'],
302
                ],
303
            ],
304
            'custom error' => [
305
                20,
306
                [
307
                    new Composite(
308
                        [new Number(max: 13, greaterThanMaxMessage: 'Custom error')],
309
                        when: fn () => true,
310
                    ),
311
                ],
312
                ['' => ['Custom error']],
313
            ],
314
            'override constructor' => [
315
                null,
316
                [
317
                    new class () extends Composite {
318
                        public function __construct()
319
                        {
320
                            $this->rules = [new Required()];
321
                        }
322
                    },
323
                ],
324
                ['' => ['Value cannot be blank.']],
325
            ],
326
            'multiple attributes' => [
327
                ['latitude' => -91, 'longitude' => 181],
328
                [new CoordinatesRuleSet()],
329
                [
330
                    'latitude' => ['Value must be no less than -90.'],
331
                    'longitude' => ['Value must be no greater than 180.'],
332
                ],
333
            ],
334
        ];
335
    }
336
337
    public function testSkipOnError(): void
338
    {
339
        $this->testSkipOnErrorInternal(new Composite([]), new Composite([], skipOnError: true));
340
    }
341
342
    public function testWhen(): void
343
    {
344
        $when = static fn (mixed $value): bool => $value !== null;
345
        $this->testWhenInternal(new Composite([]), new Composite([], when: $when));
346
    }
347
348
    protected function getDifferentRuleInHandlerItems(): array
349
    {
350
        return [Composite::class, CompositeHandler::class];
351
    }
352
}
353