Passed
Pull Request — master (#412)
by
unknown
17:58 queued 15:20
created

RulesNormalizerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dataNormalize() 0 19 1
A testNormalizeWithArrayResult() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Helper;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Validator\Helper\RulesNormalizer;
9
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility;
10
11
final class RulesNormalizerTest extends TestCase
12
{
13
    public function dataNormalize(): array
14
    {
15
        return [
16
            'null' => [[], null],
17
            'object' => [
18
                [
19
                    'name' => ['required'],
20
                    'age' => ['number'],
21
                    'number' => ['number'],
22
                ],
23
                new ObjectWithDifferentPropertyVisibility(),
24
            ],
25
            'class-string' => [
26
                [
27
                    'name' => ['required'],
28
                    'age' => ['number'],
29
                    'number' => ['number'],
30
                ],
31
                ObjectWithDifferentPropertyVisibility::class,
32
            ],
33
        ];
34
    }
35
36
    /**
37
     * @dataProvider dataNormalize
38
     *
39
     * More cases are covered in {@see ValidatorTest}.
40
     *
41
     * @see ValidatorTest::testDataAndRulesCombinations()
42
     * @see ValidatorTest::testRulesPropertyVisibility()
43
     * @see ValidatorTest::testWithEmptyArrayOfRules()
44
     * @see ValidatorTest::testDiverseTypes()
45
     * @see ValidatorTest::testNullAsDataSet()
46
     * @see ValidatorTest::testValidateWithSingleRule()
47
     */
48
    public function testNormalizeWithArrayResult(
49
        array $expected,
50
        callable|iterable|object|string|null $rules,
51
        mixed $data = null
52
    ): void {
53
        $rules = RulesNormalizer::normalize($rules, $data);
54
55
        $result = [];
56
        foreach ($rules as $attributeName => $attributeRules) {
57
            $result[$attributeName] = [];
58
            foreach ($attributeRules as $rule) {
59
                $result[$attributeName][] = $rule->getName();
60
            }
61
        }
62
63
        $this->assertSame($expected, $result);
64
    }
65
}
66