InTest::dataValidationPassed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
rs 9.456
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use ArrayObject;
8
use Yiisoft\Validator\Rule\In;
9
use Yiisoft\Validator\Rule\InHandler;
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 InTest 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 In(range(1, 10));
26
        $this->assertSame(In::class, $rule->getName());
27
    }
28
29
    public function dataOptions(): array
30
    {
31
        return [
32
            [
33
                new In(range(1, 10)),
34
                [
35
                    'values' => range(1, 10),
36
                    'strict' => false,
37
                    'not' => false,
38
                    'message' => [
39
                        'template' => 'This value is not in the list of acceptable values.',
40
                        'parameters' => [],
41
                    ],
42
                    'skipOnEmpty' => false,
43
                    'skipOnError' => false,
44
                ],
45
            ],
46
            [
47
                new In(range(1, 2), strict: true),
48
                [
49
                    'values' => [1, 2],
50
                    'strict' => true,
51
                    'not' => false,
52
                    'message' => [
53
                        'template' => 'This value is not in the list of acceptable values.',
54
                        'parameters' => [],
55
                    ],
56
                    'skipOnEmpty' => false,
57
                    'skipOnError' => false,
58
                ],
59
            ],
60
            [
61
                new In(range(1, 2), not: true),
62
                [
63
                    'values' => [1, 2],
64
                    'strict' => false,
65
                    'not' => true,
66
                    'message' => [
67
                        'template' => 'This value is not in the list of acceptable values.',
68
                        'parameters' => [],
69
                    ],
70
                    'skipOnEmpty' => false,
71
                    'skipOnError' => false,
72
                ],
73
            ],
74
        ];
75
    }
76
77
    public function dataValidationPassed(): array
78
    {
79
        return [
80
            [1, [new In(range(1, 10))]],
81
            [10, [new In(range(1, 10))]],
82
            ['10', [new In(range(1, 10))]],
83
            ['5', [new In(range(1, 10))]],
84
85
            [['a'], [new In([['a'], ['b']])]],
86
            ['a', [new In(new ArrayObject(['a', 'b']))]],
87
88
            [1, [new In(range(1, 10), strict: true)]],
89
            [5, [new In(range(1, 10), strict: true)]],
90
            [10, [new In(range(1, 10), strict: true)]],
91
92
            [0, [new In(range(1, 10), not: true)]],
93
            [11, [new In(range(1, 10), not: true)]],
94
            [5.5, [new In(range(1, 10), not: true)]],
95
96
            'arrays, simple case' => [[1, 2], [new In([[1, 2], [3, 4]])]],
97
            'arrays, non-strict equality (partially), non-strict mode' => [['1', 2], [new In([[1, 2], [3, 4]])]],
98
            'arrays, non-strict equality (fully), non-strict mode' => [['1', '2'], [new In([[1, 2], [3, 4]])]],
99
            'arrays, nested' => [
100
                ['data' => ['value' => [1, 2]]],
101
                [
102
                    new In([
103
                        ['data' => ['value' => [1, 2]]],
104
                        ['data' => ['value' => [3, 4]]],
105
                    ]),
106
                ],
107
            ],
108
            'arrays, nested, different keys order' => [
109
                [
110
                    'data2' => ['value2' => [7, 8], 'value1' => [5, 6]],
111
                    'data1' => ['value2' => [3, 4], 'value1' => [1, 2]],
112
                ],
113
                [
114
                    new In([
115
                        [
116
                            'data1' => ['value1' => [1, 2], 'value2' => [3, 4]],
117
                            'data2' => ['value1' => [5, 6], 'value2' => [7, 8]],
118
                        ],
119
                        [
120
                            'data1' => ['value1' => [9, 10], 'value2' => [11, 12]],
121
                            'data2' => ['value1' => [13, 14], 'value2' => [15, 16]],
122
                        ],
123
                    ]),
124
                ],
125
            ],
126
        ];
127
    }
128
129
    public function dataValidationFailed(): array
130
    {
131
        $errors = ['' => ['This value is not in the list of acceptable values.']];
132
133
        return [
134
            [0, [new In(range(1, 10))], $errors],
135
            [11, [new In(range(1, 10))], $errors],
136
            [5.5, [new In(range(1, 10))], $errors],
137
138
            [null, [new In(range(1, 10))], $errors],
139
            ['0', [new In(range(1, 10))], $errors],
140
            [0, [new In(range(1, 10))], $errors],
141
            ['', [new In(range(1, 10))], $errors],
142
143
            ['1', [new In(range(1, 10), strict: true)], $errors],
144
            ['10', [new In(range(1, 10), strict: true)], $errors],
145
            ['5.5', [new In(range(1, 10), strict: true)], $errors],
146
            [['1', '2', '3', '4', '5', '6'], [new In(range(1, 10), strict: true)], $errors],
147
            [['1', '2', '3', 4, 5, 6], [new In(range(1, 10), strict: true)], $errors],
148
149
            [1, [new In(range(1, 10), not: true)], $errors],
150
            [10, [new In(range(1, 10), not: true)], $errors],
151
            ['10', [new In(range(1, 10), not: true)], $errors],
152
            ['5', [new In(range(1, 10), not: true)], $errors],
153
154
            'arrays, non-strict equality (partially), strict mode' => [
155
                ['1', 2],
156
                [new In([[1, 2], [3, 4]], strict: true)],
157
                $errors,
158
            ],
159
            'arrays, non-strict equality (fully), strict mode' => [
160
                ['1', '2'],
161
                [new In([[1, 2], [3, 4]], strict: true)],
162
                $errors,
163
            ],
164
            'arrays, items are not from acceptable list' => [[5, 6], [new In([[1, 2], [3, 4]])], $errors],
165
            'arrays, items from acceptable list but not as a unit' => [[2, 3], [new In([[1, 2], [3, 4]])], $errors],
166
            'arrays, reversed order' => [[2, 1], [new In([[1, 2], [3, 4]])], $errors],
167
            'arrays, nested, items are not from acceptable list' => [
168
                ['data' => ['value' => [5, 6]]],
169
                [
170
                    new In([
171
                        ['data' => ['value' => [1, 2]]],
172
                        ['data' => ['value' => [3, 4]]],
173
                    ]),
174
                ],
175
                $errors,
176
            ],
177
            'arrays, nested, reversed order of values in lists' => [
178
                [
179
                    'data2' => ['value2' => [8, 7], 'value1' => [6, 5]],
180
                    'data1' => ['value2' => [4, 3], 'value1' => [2, 1]],
181
                ],
182
                [
183
                    new In([
184
                        [
185
                            'data1' => ['value1' => [1, 2], 'value2' => [3, 4]],
186
                            'data2' => ['value1' => [5, 6], 'value2' => [7, 8]],
187
                        ],
188
                        [
189
                            'data1' => ['value1' => [9, 10], 'value2' => [11, 12]],
190
                            'data2' => ['value1' => [13, 14], 'value2' => [15, 16]],
191
                        ],
192
                    ]),
193
                ],
194
                $errors,
195
            ],
196
            'attempt to use a a subset' => [
197
                [
198
                    ['data' => ['value' => [1, 2]]],
199
                    ['data' => ['value' => [3, 4]]],
200
                ],
201
                [
202
                    new In([
203
                        ['data' => ['value' => [1, 2]]],
204
                        ['data' => ['value' => [3, 4]]],
205
                        ['data' => ['value' => [5, 6]]],
206
                    ]),
207
                ],
208
                $errors,
209
            ],
210
211
            'custom error' => [15, [new In(range(1, 10), message: 'Custom error')], ['' => ['Custom error']]],
212
        ];
213
    }
214
215
    public function testSkipOnError(): void
216
    {
217
        $this->testSkipOnErrorInternal(new In(range(1, 10)), new In(range(1, 10), skipOnError: true));
218
    }
219
220
    public function testWhen(): void
221
    {
222
        $when = static fn (mixed $value): bool => $value !== null;
223
        $this->testWhenInternal(new In(range(1, 10)), new In(range(1, 10), when: $when));
224
    }
225
226
    protected function getDifferentRuleInHandlerItems(): array
227
    {
228
        return [In::class, InHandler::class];
229
    }
230
}
231