Completed
Pull Request — 2.6 (#8015)
by
unknown
06:58
created

createEntityWithTheNullablePropertySet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Test\ORM\Functional\Ticket;
4
5
use DateTimeImmutable;
6
use Doctrine\Tests\OrmFunctionalTestCase;
7
8
final class GH8014Test extends OrmFunctionalTestCase
9
{
10
    protected function setUp()
11
    {
12
        parent::setUp();
13
14
        $this->_schemaTool->createSchema(
15
            [
16
                $this->_em->getClassMetadata(GH8014Foo::class),
17
            ]
18
        );
19
    }
20
21
    public function testNonNullablePropertyOfEmbeddableCanBeHydratedWithNullValueInDatabase()
22
    {
23
        if (PHP_VERSION_ID < 70400) {
24
            self::markTestSkipped('This test only applies to PHP versions higher than 7.4');
25
26
            return;
27
        }
28
29
        $foo = $this->createEntityWithoutTheNullablePropertySet();
30
        $foo = $this->_em->find(
31
            GH8014Foo::class,
32
            $foo->id
33
        ); // Used to throw "Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must be an instance of DateTimeImmutable, null used"
34
35
        $this->expectException(\Error::class);
36
        $this->expectExceptionMessage('Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must not be accessed before initialization');
37
        $foo->bar->startDate;
38
39
        $foo = $this->createEntityWithTheNullablePropertySet();
40
        $foo = $this->_em->find(GH8014Foo::class, $foo->id);
41
42
        $this->assertNotNull($foo->bar->startDate);
43
    }
44
45
    private function createEntityWithoutTheNullablePropertySet(): GH8014Foo
46
    {
47
        $foo = new GH8014Foo();
48
        $this->_em->persist($foo);
49
        $this->_em->flush();
50
        $this->_em->clear();
51
52
        return $foo;
53
    }
54
55
    private function createEntityWithTheNullablePropertySet(): GH8014Foo
56
    {
57
        $foo = new GH8014Foo();
58
        $foo->bar = new GH8014Bar();
59
        $foo->bar->startDate = new DateTimeImmutable();
60
        $this->_em->persist($foo);
61
        $this->_em->flush();
62
        $this->_em->clear();
63
64
        return $foo;
65
    }
66
}
67
68
/**
69
 * @Entity()
70
 */
71
class GH8014Foo
72
{
73
    /**
74
     * @var integer
75
     *
76
     * @Id()
77
     * @Column(name="id", type="integer")
78
     * @GeneratedValue(strategy="IDENTITY")
79
     */
80
    public $id;
81
82
    /**
83
     * @Embedded(class="Doctrine\Test\ORM\Functional\Ticket\GH8014Bar")
84
     */
85
    public ?GH8014Bar $bar = null;
86
}
87
88
/**
89
 * @Embeddable()
90
 */
91
class GH8014Bar
92
{
93
    /**
94
     * @Column(type="datetime_immutable", nullable=true)
95
     */
96
    public DateTimeImmutable $startDate;
97
}