Passed
Push — master ( c6195c...a768dd )
by Alexander
06:46 queued 02:57
created

ObjectParserTest::testInvalidSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Helper;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Validator\Helper\ObjectParser;
10
use Yiisoft\Validator\Tests\Support\Data\SimpleDto;
11
use Yiisoft\Validator\Tests\Support\Rule\CustomUrlRuleSet;
12
13
final class ObjectParserTest extends TestCase
14
{
15
    public function testInvalidSource(): void
16
    {
17
        $this->expectException(InvalidArgumentException::class);
18
        $this->expectExceptionMessage('Class "non-exist-class" not found.');
19
        new ObjectParser('non-exist-class');
20
    }
21
22
    public function dataSkipStaticProperties(): array
23
    {
24
        return [
25
            [
26
                ['a' => 4, 'b' => 2],
27
                new class () {
28
                    public int $a = 4;
29
                    public static int $b = 2;
30
                },
31
                false,
32
            ],
33
            [
34
                ['a' => 4, 'c' => 'hello'],
35
                new class () {
36
                    public int $a = 4;
37
                    public static int $b = 2;
38
                    public string $c = 'hello';
39
                },
40
                true,
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * @dataProvider dataSkipStaticProperties
47
     */
48
    public function testSkipStaticProperties(array $expectedData, object $object, bool $skipStaticProperties): void
49
    {
50
        $parser = new ObjectParser($object, skipStaticProperties: $skipStaticProperties);
51
52
        $this->assertSame($expectedData, $parser->getData());
53
    }
54
55
    public function testSkipStaticPropertiesDefault(): void
56
    {
57
        $object = new class () {
58
            public int $a = 4;
59
            public static int $b = 2;
60
        };
61
62
        $parser = new ObjectParser($object);
63
64
        $this->assertSame(['a' => 4, 'b' => 2], $parser->getData());
65
    }
66
67
    public function testDataWithClassString(): void
68
    {
69
        $parser = new ObjectParser(SimpleDto::class);
70
71
        $this->assertSame([], $parser->getData());
72
        $this->assertNull($parser->getAttributeValue('a'));
73
        $this->assertNull($parser->getAttributeValue('x'));
74
        $this->assertFalse($parser->hasAttribute('a'));
75
        $this->assertFalse($parser->hasAttribute('x'));
76
    }
77
78
    public function testGetRulesExpectingAttributeInheritance(): void
79
    {
80
        $object = new class () {
81
            #[CustomUrlRuleSet]
82
            public string $url;
83
        };
84
        $parser = new ObjectParser($object);
85
86
        $this->expectError();
87
88
        $className = CustomUrlRuleSet::class;
89
        $this->expectErrorMessage("Attempting to use non-attribute class \"$className\" as attribute");
90
91
        $parser->getRules();
92
    }
93
}
94