Passed
Pull Request — master (#408)
by
unknown
02:42
created

AtLeastTest.php$4 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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