Passed
Pull Request — master (#626)
by
unknown
04:17
created

AtLeastTest.php$7 ➔ testSkipOnError()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\EmptyCondition\NeverEmpty;
9
use Yiisoft\Validator\Rule\AtLeast;
10
use Yiisoft\Validator\Rule\AtLeastHandler;
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\Data\AtLeastDto;
17
18
final class AtLeastTest extends RuleTestCase
19
{
20
    use DifferentRuleInHandlerTestTrait;
21
    use RuleWithOptionsTestTrait;
22
    use SkipOnErrorTestTrait;
23
    use WhenTestTrait;
24
25
    public function testMinGreaterThanAttributesCount(): void
26
    {
27
        $this->expectException(InvalidArgumentException::class);
28
        $this->expectExceptionMessage('$min must be no greater than amount of $attributes.');
29
        new AtLeast(['attr'], min: 2);
30
    }
31
32
    public function testGetName(): void
33
    {
34
        $rule = new AtLeast(['attr']);
35
        $this->assertSame('atLeast', $rule->getName());
36
    }
37
38
    public function dataOptions(): array
39
    {
40
        return [
41
            [
42
                new AtLeast(['attr1', 'attr2']),
43
                [
44
                    'attributes' => [
45
                        'attr1',
46
                        'attr2',
47
                    ],
48
                    'min' => 1,
49
                    'incorrectInputMessage' => [
50
                        'template' => 'The value must be an array or an object.',
51
                        'parameters' => [],
52
                    ],
53
                    'message' => [
54
                        'template' => 'At least {min, number} {min, plural, one{attribute} other{attributes}} from ' .
55
                            'this list must be filled: {attributes}.',
56
                        'parameters' => ['min' => 1],
57
                    ],
58
                    'skipOnEmpty' => false,
59
                    'skipOnError' => false,
60
                ],
61
            ],
62
            [
63
                new AtLeast(['attr1', 'attr2'], min: 2),
64
                [
65
                    'attributes' => [
66
                        'attr1',
67
                        'attr2',
68
                    ],
69
                    'min' => 2,
70
                    'incorrectInputMessage' => [
71
                        'template' => 'The value must be an array or an object.',
72
                        'parameters' => [],
73
                    ],
74
                    'message' => [
75
                        'template' => 'At least {min, number} {min, plural, one{attribute} other{attributes}} from ' .
76
                            'this list must be filled: {attributes}.',
77
                        'parameters' => ['min' => 2],
78
                    ],
79
                    'skipOnEmpty' => false,
80
                    'skipOnError' => false,
81
                ],
82
            ],
83
            'callable skip on empty' => [
84
                new AtLeast(['attr1', 'attr2'], skipOnEmpty: new NeverEmpty()),
85
                [
86
                    'attributes' => [
87
                        'attr1',
88
                        'attr2',
89
                    ],
90
                    'min' => 1,
91
                    'incorrectInputMessage' => [
92
                        'template' => 'The value must be an array or an object.',
93
                        'parameters' => [],
94
                    ],
95
                    'message' => [
96
                        'template' => 'At least {min, number} {min, plural, one{attribute} other{attributes}} from ' .
97
                            'this list must be filled: {attributes}.',
98
                        'parameters' => ['min' => 1],
99
                    ],
100
                    'skipOnEmpty' => null,
101
                    'skipOnError' => false,
102
                ],
103
            ],
104
        ];
105
    }
106
107
    public function dataValidationPassed(): array
108
    {
109
        return [
110
            [
111
                new class () {
112
                    public $attr1 = 1;
113
                    public $attr2 = null;
114
                },
115
                [new AtLeast(['attr1', 'attr2'])],
116
            ],
117
            [
118
                new class () {
119
                    public $attr1 = null;
120
                    public $attr2 = 1;
121
                },
122
                [new AtLeast(['attr2'])],
123
            ],
124
            [
125
                new class () {
126
                    private int $attr1 = 1;
0 ignored issues
show
introduced by
The private property $attr1 is not used, and could be removed.
Loading history...
127
                    private $attr2 = null;
0 ignored issues
show
introduced by
The private property $attr2 is not used, and could be removed.
Loading history...
128
                },
129
                [new AtLeast(['attr1', 'attr2'])],
130
            ],
131
            [
132
                ['attr1' => 1, 'attr2' => null],
133
                [new AtLeast(['attr1', 'attr2'])],
134
            ],
135
            [
136
                ['attr1' => null, 'attr2' => 1],
137
                [new AtLeast(['attr2'])],
138
            ],
139
            [
140
                new class () {
141
                    public $obj;
142
143
                    public function __construct()
144
                    {
145
                        $this->obj = new class () {
146
                            public $attr1 = 1;
147
                            public $attr2 = null;
148
                        };
149
                    }
150
                },
151
                ['obj' => new AtLeast(['attr1', 'attr2'])],
152
            ],
153
            [
154
                new class () {
155
                    public $obj;
156
157
                    public function __construct()
158
                    {
159
                        $this->obj = new class () {
160
                            public $attr1 = null;
161
                            public $attr2 = 1;
162
                        };
163
                    }
164
                },
165
                ['obj' => new AtLeast(['attr2'])],
166
            ],
167
            [
168
                ['obj' => ['attr1' => 1, 'attr2' => null]],
169
                ['obj' => new AtLeast(['attr1', 'attr2'])],
170
            ],
171
            [
172
                ['obj' => ['attr1' => null, 'attr2' => 1]],
173
                ['obj' => new AtLeast(['attr2'])],
174
            ],
175
            'more than "min" attributes are filled' => [
176
                ['attr1' => 1, 'attr2' => 2],
177
                [new AtLeast(['attr1', 'attr2'])],
178
            ],
179
            'class attribute' => [
180
                new AtLeastDto(1),
181
            ],
182
        ];
183
    }
184
185
    public function dataValidationFailed(): array
186
    {
187
        $class = new class () {
188
            public $attr1 = 1;
189
            public $attr2 = null;
190
        };
191
        $array = ['attr1' => 1, 'attr2' => null];
192
193
        return [
194
            'incorrect input' => [
195
                1,
196
                [new AtLeast(['attr2'])],
197
                ['' => ['The value must be an array or an object.']],
198
            ],
199
            'custom incorrect input message' => [
200
                1,
201
                [new AtLeast(['attr2'], incorrectInputMessage: 'Custom incorrect input message.')],
202
                ['' => ['Custom incorrect input message.']],
203
            ],
204
            'custom incorrect input message with parameters' => [
205
                1,
206
                [new AtLeast(['attr2'], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
207
                ['' => ['Attribute - , type - int.']],
208
            ],
209
            'custom incorrect input message with parameters, attribute set' => [
210
                ['attribute' => 1],
211
                [
212
                    'attribute' => new AtLeast(
213
                        ['attr2'],
214
                        incorrectInputMessage: 'Attribute - {attribute}, type - {type}.',
215
                    ),
216
                ],
217
                ['attribute' => ['Attribute - attribute, type - int.']],
218
            ],
219
            'object' => [
220
                $class,
221
                [new AtLeast(['attr2'])],
222
                ['' => ['At least 1 attribute from this list must be filled: "attr2".']],
223
            ],
224
            'object, custom min' => [
225
                $class,
226
                [new AtLeast(['attr1', 'attr2'], min: 2)],
227
                ['' => ['At least 2 attributes from this list must be filled: "attr1", "attr2".']],
228
            ],
229
            'array' => [
230
                $array,
231
                [new AtLeast(['attr2'])],
232
                ['' => ['At least 1 attribute from this list must be filled: "attr2".']],
233
            ],
234
            'array, custom min' => [
235
                $array,
236
                [new AtLeast(['attr1', 'attr2'], min: 2)],
237
                ['' => ['At least 2 attributes from this list must be filled: "attr1", "attr2".']],
238
            ],
239
            'custom message' => [
240
                $class,
241
                [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Custom message.')],
242
                ['' => ['Custom message.']],
243
            ],
244
            'custom message with parameters' => [
245
                $class,
246
                [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attributes - {attributes}, min - {min}.')],
247
                ['' => ['Attributes - "attr1", "attr2", min - 2.']],
248
            ],
249
            'custom message with parameters, attribute set' => [
250
                ['data' => $class],
251
                ['data' => new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attributes - {attributes}, min - {min}.')],
252
                ['data' => ['Attributes - "attr1", "attr2", min - 2.']],
253
            ],
254
            'class attribute' => [
255
                new AtLeastDto(),
256
                null,
257
                ['' => ['At least 1 attribute from this list must be filled: "A", "B", "C".']],
258
            ],
259
        ];
260
    }
261
262
    public function testSkipOnError(): void
263
    {
264
        $this->testSkipOnErrorInternal(new AtLeast(['attr']), new AtLeast(['attr'], skipOnError: true));
265
    }
266
267
    public function testWhen(): void
268
    {
269
        $when = static fn (mixed $value): bool => $value !== null;
270
        $this->testWhenInternal(new AtLeast(['attr']), new AtLeast(['attr'], when: $when));
271
    }
272
273
    protected function getDifferentRuleInHandlerItems(): array
274
    {
275
        return [AtLeast::class, AtLeastHandler::class];
276
    }
277
}
278