Passed
Pull Request — master (#464)
by Alexander
04:02 queued 01:28
created

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