ObjectDataSetTest.php$9 ➔ dataProvider()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 147
rs 8

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\TestEnvironments\Php81\DataSet;
6
7
use PHPUnit\Framework\TestCase;
8
use Traversable;
9
use Yiisoft\Validator\DataSet\ObjectDataSet;
10
use Yiisoft\Validator\Rule\Count;
11
use Yiisoft\Validator\Rule\Each;
12
use Yiisoft\Validator\Rule\Length;
13
use Yiisoft\Validator\Rule\Nested;
14
use Yiisoft\Validator\Rule\Number;
15
use Yiisoft\Validator\Rule\Required;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\Tests\Support\Data\TitleTrait;
18
use Yiisoft\Validator\Tests\Support\Rule\NotRuleAttribute;
19
use Yiisoft\Validator\Tests\TestEnvironments\Support\Data\Charts\Chart;
20
21
final class ObjectDataSetTest extends TestCase
22
{
23
    /**
24
     * @dataProvider dataProvider
25
     *
26
     * @param RuleInterface[]|RuleInterface[][]|RuleInterface[][][] $expectedRules
27
     */
28
    public function testCollectRules(object $object, array $expectedRules): void
29
    {
30
        $dataSet = new ObjectDataSet($object);
31
32
        $rulesArray = $this->toNestedArray($dataSet->getRules());
33
34
        $this->assertEquals($expectedRules, $rulesArray);
35
    }
36
37
    public function dataProvider(): array
38
    {
39
        return [
40
            [
41
                new class () {
42
                },
43
                [],
44
            ],
45
            [
46
                new class () {
47
                    private $property1;
0 ignored issues
show
introduced by
The private property $property1 is not used, and could be removed.
Loading history...
48
                },
49
                [],
50
            ],
51
            [
52
                new class () {
53
                    #[NotRuleAttribute]
54
                    private $property1;
55
                },
56
                [],
57
            ],
58
            [
59
                new class () {
60
                    #[Required()]
61
                    private $property1;
62
                },
63
                [
64
                    'property1' => [
65
                        new Required(),
66
                    ],
67
                ],
68
            ],
69
            [
70
                new class () {
71
                    use TitleTrait;
72
                },
73
                [
74
                    'title' => [
75
                        new Length(max: 255),
76
                    ],
77
                ],
78
            ],
79
            [
80
                new class () {
81
                    #[Required()]
82
                    #[Length(max: 255, skipOnEmpty: true)]
83
                    private $property1;
84
                    #[Required()]
85
                    #[Length(max: 255, skipOnEmpty: true)]
86
                    private $property2;
0 ignored issues
show
introduced by
The private property $property2 is not used, and could be removed.
Loading history...
87
                },
88
                [
89
                    'property1' => [
90
                        new Required(),
91
                        new Length(max: 255, skipOnEmpty: true),
92
                    ],
93
                    'property2' => [
94
                        new Required(),
95
                        new Length(max: 255, skipOnEmpty: true),
96
                    ],
97
                ],
98
            ],
99
            [
100
                new class () {
101
                    #[Each([
102
                        new Required(),
103
                        new Length(max: 255, skipOnEmpty: true),
104
                    ])]
105
                    #[Length(max: 255, skipOnEmpty: true)]
106
                    private $property1;
107
                },
108
                [
109
                    'property1' => [
110
                        new Each([
111
                            new Required(),
112
                            new Length(max: 255, skipOnEmpty: true),
113
                        ]),
114
                        new Length(max: 255, skipOnEmpty: true),
115
                    ],
116
                ],
117
            ],
118
            [
119
                new class () {
120
                    #[Nested([
121
                        new Required(),
122
                        new Length(max: 255, skipOnEmpty: true),
123
                    ])]
124
                    #[Each([
125
                        new Required(),
126
                        new Length(max: 255, skipOnEmpty: true),
127
                    ])]
128
                    #[Length(max: 255, skipOnEmpty: true)]
129
                    private $property1;
130
                },
131
                [
132
                    'property1' => [
133
                        new Nested([
134
                            new Required(),
135
                            new Length(max: 255, skipOnEmpty: true),
136
                        ]),
137
                        new Each([
138
                            new Required(),
139
                            new Length(max: 255, skipOnEmpty: true),
140
                        ]),
141
                        new Length(max: 255, skipOnEmpty: true),
142
                    ],
143
                ],
144
            ],
145
            [
146
                new class () {
147
                    #[Length(max: 255, skipOnEmpty: true)]
148
                    #[Length(max: 255, skipOnEmpty: false)]
149
                    private $property1;
150
                },
151
                [
152
                    'property1' => [
153
                        new Length(max: 255, skipOnEmpty: true),
154
                        new Length(max: 255, skipOnEmpty: false),
155
                    ],
156
                ],
157
            ],
158
            [
159
                new class () {
160
                    #[Nested([
161
                        new Required(),
162
                        new Length(max: 255, skipOnEmpty: true),
163
                    ])]
164
                    #[Nested([
165
                        new Required(),
166
                        new Length(max: 255, skipOnEmpty: true),
167
                    ])]
168
                    #[Length(max: 255, skipOnEmpty: true)]
169
                    #[Length(max: 255, skipOnEmpty: false)]
170
                    private $property1;
171
                },
172
                [
173
                    'property1' => [
174
                        new Nested([
175
                            new Required(),
176
                            new Length(max: 255, skipOnEmpty: true),
177
                        ]),
178
                        new Nested([
179
                            new Required(),
180
                            new Length(max: 255, skipOnEmpty: true),
181
                        ]),
182
                        new Length(max: 255, skipOnEmpty: true),
183
                        new Length(max: 255, skipOnEmpty: false),
184
                    ],
185
                ],
186
            ],
187
        ];
188
    }
189
190
    public function testMoreComplexEmbeddedRule(): void
191
    {
192
        $dataSet = new ObjectDataSet(new Chart());
193
        $expectedRules = [
194
            'points' => [
195
                new Each([
196
                    new Nested([
197
                        'coordinates' => new Each([
198
                            new Nested(
199
                                [
200
                                    'x' => [new Number(min: -10, max: 10)],
201
                                    'y' => [new Number(min: -10, max: 10)],
202
                                ],
203
                                requirePropertyPath: true,
204
                                noPropertyPathMessage: 'Custom message 4.',
205
                            ),
206
                        ]),
207
                        'rgb' => [
208
                            new Count(3),
209
                            new Each(
210
                                [new Number(min: 0, max: 255)],
211
                                incorrectInputMessage: 'Custom message 5.',
212
                            ),
213
                        ],
214
                    ]),
215
                ]),
216
            ],
217
        ];
218
        $this->assertEquals($expectedRules, $dataSet->getRules());
219
    }
220
221
    private function toArray(iterable $rules): array
222
    {
223
        return $rules instanceof Traversable ? iterator_to_array($rules) : (array) $rules;
224
    }
225
226
    private function toNestedArray(iterable $doubleIterable): array
227
    {
228
        $actualRules = [];
229
        foreach ($doubleIterable as $key => $iterable) {
230
            $actualRules[$key] = $this->toArray($iterable);
231
        }
232
233
        return $actualRules;
234
    }
235
}
236