File: TypedNoDefaultReflectionPropertyTest.php

package info (click to toggle)
php-doctrine-persistence 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 732 kB
  • sloc: php: 3,610; makefile: 22
file content (86 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Persistence\Reflection;

use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
use PHPUnit\Framework\TestCase;

class TypedNoDefaultReflectionPropertyTest extends TestCase
{
    public function testGetValue(): void
    {
        $object = new TypedNoDefaultReflectionPropertyTestClass();

        $reflProperty = new TypedNoDefaultReflectionProperty(TypedNoDefaultReflectionPropertyTestClass::class, 'test');

        self::assertNull($reflProperty->getValue($object));

        $object->test = 'testValue';

        self::assertSame('testValue', $reflProperty->getValue($object));

        unset($object->test);

        self::assertNull($reflProperty->getValue($object));
    }

    public function testSetValueNull(): void
    {
        $reflection = new TypedNoDefaultReflectionProperty(TypedFoo::class, 'id');

        $object = new TypedFoo();
        $object->setId(1);

        self::assertTrue($reflection->isInitialized($object));

        $reflection->setValue($object, null);

        self::assertNull($reflection->getValue($object));
        self::assertFalse($reflection->isInitialized($object));
    }

    public function testSetValueNullOnNullableProperty(): void
    {
        $reflection = new TypedNoDefaultReflectionProperty(TypedNullableFoo::class, 'value');

        $object = new TypedNullableFoo();

        $reflection->setValue($object, null);

        self::assertNull($reflection->getValue($object));
        self::assertTrue($reflection->isInitialized($object));
        self::assertNull($object->getValue());
    }
}

class TypedNoDefaultReflectionPropertyTestClass
{
    public string $test;
}

class TypedFoo
{
    private int $id;

    public function setId(mixed $id): void
    {
        $this->id = $id;
    }
}

class TypedNullableFoo
{
    private string|null $value;

    public function setValue(mixed $value): void
    {
        $this->value = $value;
    }

    public function getValue(): mixed
    {
        return $this->value;
    }
}