Passed
Pull Request — master (#580)
by
unknown
02:38
created

ObjectParserTest::testDataWithClassString()

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Helper;
6
7
use Error;
8
use InvalidArgumentException;
9
use PHPUnit\Framework\TestCase;
10
use Yiisoft\Validator\Helper\ObjectParser;
11
use Yiisoft\Validator\Rule\Number;
12
use Yiisoft\Validator\Rule\Required;
13
use Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache1;
14
use Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache2;
15
use Yiisoft\Validator\Tests\Support\Data\SimpleDto;
16
use Yiisoft\Validator\Tests\Support\Rule\CustomUrlRuleSet;
17
18
final class ObjectParserTest extends TestCase
19
{
20
    public function testInvalidSource(): void
21
    {
22
        $this->expectException(InvalidArgumentException::class);
23
        $this->expectExceptionMessage('Class "non-exist-class" not found.');
24
        new ObjectParser('non-exist-class');
25
    }
26
27
    public function dataSkipStaticProperties(): array
28
    {
29
        return [
30
            [
31
                ['a' => 4, 'b' => 2],
32
                new class () {
33
                    public int $a = 4;
34
                    public static int $b = 2;
35
                },
36
                false,
37
            ],
38
            [
39
                ['a' => 4, 'c' => 'hello'],
40
                new class () {
41
                    public int $a = 4;
42
                    public static int $b = 2;
43
                    public string $c = 'hello';
44
                },
45
                true,
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @dataProvider dataSkipStaticProperties
52
     */
53
    public function testSkipStaticProperties(array $expectedData, object $object, bool $skipStaticProperties): void
54
    {
55
        $parser = new ObjectParser($object, skipStaticProperties: $skipStaticProperties);
56
57
        $this->assertSame($expectedData, $parser->getData());
58
    }
59
60
    public function testSkipStaticPropertiesDefault(): void
61
    {
62
        $object = new class () {
63
            public int $a = 4;
64
            public static int $b = 2;
65
        };
66
67
        $parser = new ObjectParser($object);
68
69
        $this->assertSame(['a' => 4, 'b' => 2], $parser->getData());
70
    }
71
72
    public function testDataWithClassString(): void
73
    {
74
        $parser = new ObjectParser(SimpleDto::class);
75
76
        $this->assertSame([], $parser->getData());
77
        $this->assertNull($parser->getAttributeValue('a'));
78
        $this->assertNull($parser->getAttributeValue('x'));
79
        $this->assertFalse($parser->hasAttribute('a'));
80
        $this->assertFalse($parser->hasAttribute('x'));
81
    }
82
83
    public function testGetRulesExpectingAttributeInheritance(): void
84
    {
85
        $object = new class () {
86
            #[CustomUrlRuleSet]
87
            public string $url;
88
        };
89
        $parser = new ObjectParser($object);
90
91
        $this->expectException(Error::class);
92
93
        $className = CustomUrlRuleSet::class;
94
        $this->expectExceptionMessage("Attempting to use non-attribute class \"$className\" as attribute");
95
96
        $parser->getRules();
97
    }
98
99
    public function testCache(): void
100
    {
101
        $parser1 = new ObjectParser(new ObjectForTestingCache1());
102
        $cacheKey1 = 'Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache1_7_0';
103
        $this->assertArrayNotHasKey($cacheKey1, ObjectParser::getCache());
104
105
        $expectedRules1 = [
106
            'a' => [new Required()],
107
            'b' => [new Number(min: 1)],
108
            'c' => [new Number(max: 2)],
109
        ];
110
        $this->assertEquals($expectedRules1, $parser1->getRules());
111
        $this->assertArrayHasKey($cacheKey1, ObjectParser::getCache());
112
        $this->assertArrayHasKey('rules', ObjectParser::getCache()[$cacheKey1]);
113
        $this->assertArrayHasKey('reflectionProperties', ObjectParser::getCache()[$cacheKey1]);
114
        $this->assertArrayHasKey('reflectionSource', ObjectParser::getCache()[$cacheKey1]);
115
        $this->assertEquals($expectedRules1, $parser1->getRules());
116
117
        $parser2 = new ObjectParser(new ObjectForTestingCache2());
118
        $cacheKey2 = 'Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache2_7_0';
119
        $this->assertArrayHasKey($cacheKey1, ObjectParser::getCache());
120
        $this->assertArrayNotHasKey($cacheKey2, ObjectParser::getCache());
121
122
        $parser2->getReflectionProperties();
123
        $this->assertArrayHasKey($cacheKey1, ObjectParser::getCache());
124
        $this->assertArrayHasKey($cacheKey2, ObjectParser::getCache());
125
        $this->assertArrayNotHasKey('rules', ObjectParser::getCache()[$cacheKey2]);
126
        $this->assertArrayHasKey('reflectionProperties', ObjectParser::getCache()[$cacheKey2]);
127
        $this->assertArrayHasKey('reflectionSource', ObjectParser::getCache()[$cacheKey2]);
128
129
        $expectedRules2 = [
130
            'd' => [new Required()],
131
            'e' => [new Number(min: 5)],
132
            'f' => [new Number(max: 6)],
133
        ];
134
        $this->assertEquals($expectedRules2, $parser2->getRules());
135
        $this->assertArrayHasKey('rules', ObjectParser::getCache()[$cacheKey2]);
136
        $this->assertEquals($expectedRules2, $parser2->getRules());
137
        $this->assertEquals($expectedRules1, $parser1->getRules());
138
    }
139
}
140