Passed
Pull Request — master (#415)
by
unknown
29:22 queued 26:39
created

AtLeastTest::dataValidationFailed()

Size

Total Lines 68
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 48
nc 1
nop 0
dl 0
loc 68
c 0
b 0
f 0

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