Passed
Pull Request — master (#449)
by Sergei
02:24
created

ObjectParserTest.php$2 ➔ testDataWithClassString()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 9
rs 9.9666
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
12
final class ObjectParserTest extends TestCase
13
{
14
    public function testInvalidSource(): void
15
    {
16
        $this->expectException(InvalidArgumentException::class);
17
        $this->expectExceptionMessage('Class "non-exist-class" not found.');
18
        new ObjectParser('non-exist-class');
19
    }
20
21
    public function dataSkipStaticProperties(): array
22
    {
23
        return [
24
            [
25
                ['a' => 4, 'b' => 2],
26
                new class () {
27
                    public int $a = 4;
28
                    public static int $b = 2;
29
                },
30
                false,
31
            ],
32
            [
33
                ['a' => 4, 'c' => 'hello'],
34
                new class () {
35
                    public int $a = 4;
36
                    public static int $b = 2;
37
                    public string $c = 'hello';
38
                },
39
                true,
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @dataProvider dataSkipStaticProperties
46
     */
47
    public function testSkipStaticProperties(array $expectedData, object $object, bool $skipStaticProperties): void
48
    {
49
        $parser = new ObjectParser($object, skipStaticProperties: $skipStaticProperties);
50
51
        $this->assertSame($expectedData, $parser->getData());
52
    }
53
54
    public function testSkipStaticPropertiesDefault(): void
55
    {
56
        $object = new class () {
57
            public int $a = 4;
58
            public static int $b = 2;
59
        };
60
61
        $parser = new ObjectParser($object);
62
63
        $this->assertSame(['a' => 4, 'b' => 2], $parser->getData());
64
    }
65
66
    public function testDataWithClassString(): void
67
    {
68
        $parser = new ObjectParser(SimpleDto::class);
69
70
        $this->assertSame([], $parser->getData());
71
        $this->assertNull($parser->getAttributeValue('a'));
72
        $this->assertNull($parser->getAttributeValue('x'));
73
        $this->assertFalse($parser->hasAttribute('a'));
74
        $this->assertFalse($parser->hasAttribute('x'));
75
    }
76
}
77