Passed
Pull Request — master (#412)
by
unknown
17:58 queued 15:20
created

CompositeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 220
rs 10
c 0
b 0
f 0
wmc 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ getDifferentRuleInHandlerItems() 0 3 1
A hp$0 ➔ dataValidationPassed() 0 27 1
dataValidationFailed() 0 27 ?
dataValidationPassed() 0 27 ?
testSkipOnError() 0 3 ?
B dataOptions() 0 108 1
A hp$0 ➔ testOptionsWithNotRule() 0 12 1
A hp$0 ➔ testWhen() 0 4 1
A hp$0 ➔ testSkipOnError() 0 3 1
getDifferentRuleInHandlerItems() 0 3 ?
A hp$0 ➔ dataValidationFailed() 0 27 1
A testGetName() 0 4 1
testWhen() 0 4 ?
testOptionsWithNotRule() 0 12 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\Rule\Composite;
9
use Yiisoft\Validator\Rule\CompositeHandler;
10
use Yiisoft\Validator\Rule\Number;
11
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
12
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
13
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
14
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
15
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
16
use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions;
17
18
final class CompositeTest extends RuleTestCase
19
{
20
    use DifferentRuleInHandlerTestTrait;
21
    use RuleWithOptionsTestTrait;
22
    use SkipOnErrorTestTrait;
23
    use WhenTestTrait;
24
25
    public function testGetName(): void
26
    {
27
        $rule = new Composite([]);
28
        $this->assertSame('composite', $rule->getName());
29
    }
30
31
    public function dataOptions(): array
32
    {
33
        return [
34
            [
35
                new Composite([
36
                    new Number(max: 13, integerPattern: '/1/', numberPattern: '/1/'),
37
                    new Number(max: 14, integerPattern: '/2/', numberPattern: '/2/'),
38
                ]),
39
                [
40
                    'skipOnEmpty' => false,
41
                    'skipOnError' => false,
42
                    'rules' => [
43
                        [
44
                            'number',
45
                            'asInteger' => false,
46
                            'min' => null,
47
                            'max' => 13,
48
                            'incorrectInputMessage' => [
49
                                'template' => 'The allowed types are integer, float and string.',
50
                                'parameters' => [],
51
                            ],
52
                            'notNumberMessage' => [
53
                                'template' => 'Value must be a number.',
54
                                'parameters' => [],
55
                            ],
56
                            'tooSmallMessage' => [
57
                                'template' => 'Value must be no less than {min}.',
58
                                'parameters' => ['min' => null],
59
                            ],
60
                            'tooBigMessage' => [
61
                                'template' => 'Value must be no greater than {max}.',
62
                                'parameters' => ['max' => 13],
63
                            ],
64
                            'skipOnEmpty' => false,
65
                            'skipOnError' => false,
66
                            'integerPattern' => '/1/',
67
                            'numberPattern' => '/1/',
68
                        ],
69
                        [
70
                            'number',
71
                            'asInteger' => false,
72
                            'min' => null,
73
                            'max' => 14,
74
                            'incorrectInputMessage' => [
75
                                'template' => 'The allowed types are integer, float and string.',
76
                                'parameters' => [],
77
                            ],
78
                            'notNumberMessage' => [
79
                                'template' => 'Value must be a number.',
80
                                'parameters' => [],
81
                            ],
82
                            'tooSmallMessage' => [
83
                                'template' => 'Value must be no less than {min}.',
84
                                'parameters' => ['min' => null],
85
                            ],
86
                            'tooBigMessage' => [
87
                                'template' => 'Value must be no greater than {max}.',
88
                                'parameters' => ['max' => 14],
89
                            ],
90
                            'skipOnEmpty' => false,
91
                            'skipOnError' => false,
92
                            'integerPattern' => '/2/',
93
                            'numberPattern' => '/2/',
94
                        ],
95
                    ],
96
                ],
97
            ],
98
            'rule without options' => [
99
                new Composite([
100
                    new Number(max: 13, integerPattern: '/1/', numberPattern: '/1/'),
101
                    new RuleWithoutOptions(),
102
                ]),
103
                [
104
                    'skipOnEmpty' => false,
105
                    'skipOnError' => false,
106
                    'rules' => [
107
                        [
108
                            'number',
109
                            'asInteger' => false,
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
                            'tooSmallMessage' => [
121
                                'template' => 'Value must be no less than {min}.',
122
                                'parameters' => [
123
                                    'min' => null,
124
                                ],
125
                            ],
126
                            'tooBigMessage' => [
127
                                'template' => 'Value must be no greater than {max}.',
128
                                'parameters' => [
129
                                    'max' => 13,
130
                                ],
131
                            ],
132
                            'skipOnEmpty' => false,
133
                            'skipOnError' => false,
134
                            'integerPattern' => '/1/',
135
                            'numberPattern' => '/1/',
136
                        ],
137
                        [
138
                            'test',
139
                        ],
140
                    ],
141
                ],
142
            ],
143
        ];
144
    }
145
146
    public function testOptionsWithNotRule(): void
147
    {
148
        $rule = new Composite([
149
            new Number(max: 13, integerPattern: '/1/', numberPattern: '/1/'),
150
            new class () {
151
            },
152
        ]);
153
154
        $this->expectException(InvalidArgumentException::class);
155
        $message = 'Every rule must implement "Yiisoft\Validator\RuleInterface". Type "class@anonymous" given.';
156
        $this->expectExceptionMessage($message);
157
        $rule->getOptions();
158
    }
159
160
    public function dataValidationPassed(): array
161
    {
162
        return [
163
            [
164
                20,
165
                [
166
                    new Composite(
167
                        rules: [new Number(max: 13)],
168
                        when: fn () => false,
169
                    ),
170
                ],
171
            ],
172
            [
173
                20,
174
                [
175
                    new Composite(
176
                        rules: [new Number(max: 13)],
177
                        skipOnError: true,
178
                    ),
179
                ],
180
            ],
181
            [
182
                null,
183
                [
184
                    new Composite(
185
                        rules: [new Number(max: 13)],
186
                        skipOnEmpty: true,
187
                    ),
188
                ],
189
            ],
190
        ];
191
    }
192
193
    public function dataValidationFailed(): array
194
    {
195
        return [
196
            [
197
                20,
198
                [
199
                    new Composite(
200
                        rules: [new Number(max: 13), new Number(min: 21)],
201
                        when: fn () => true,
202
                    ),
203
                ],
204
                [
205
                    '' => [
206
                        'Value must be no greater than 13.',
207
                        'Value must be no less than 21.',
208
                    ],
209
                ],
210
            ],
211
            'custom error' => [
212
                20,
213
                [
214
                    new Composite(
215
                        rules: [new Number(max: 13, tooBigMessage: 'Custom error')],
216
                        when: fn () => true,
217
                    ),
218
                ],
219
                ['' => ['Custom error']],
220
            ],
221
        ];
222
    }
223
224
    public function testSkipOnError(): void
225
    {
226
        $this->testSkipOnErrorInternal(new Composite([]), new Composite([], skipOnError: true));
227
    }
228
229
    public function testWhen(): void
230
    {
231
        $when = static fn (mixed $value): bool => $value !== null;
232
        $this->testWhenInternal(new Composite([]), new Composite([], when: $when));
233
    }
234
235
    protected function getDifferentRuleInHandlerItems(): array
236
    {
237
        return [Composite::class, CompositeHandler::class];
238
    }
239
}
240