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

testNonNullablePropertyOfEmbeddableCanBeHydratedWithNullValueInDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
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
        $foo = $this->createEntityWithoutTheNullablePropertySet();
24
        $foo = $this->_em->find(
25
            GH8014Foo::class,
26
            $foo->id
27
        ); // Used to throw "Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must be an instance of DateTimeImmutable, null used"
28
29
        $this->expectException(\Error::class);
30
        $this->expectExceptionMessage('Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must not be accessed before initialization');
31
        $foo->bar->startDate;
32
33
        $foo = $this->createEntityWithTheNullablePropertySet();
34
        $foo = $this->_em->find(GH8014Foo::class, $foo->id);
35
36
        $this->assertNotNull($foo->bar->startDate);
37
    }
38
39
    private function createEntityWithoutTheNullablePropertySet(): GH8014Foo
40
    {
41
        $foo = new GH8014Foo();
42
        $this->_em->persist($foo);
43
        $this->_em->flush();
44
        $this->_em->clear();
45
46
        return $foo;
47
    }
48
49
    private function createEntityWithTheNullablePropertySet(): GH8014Foo
50
    {
51
        $foo = new GH8014Foo();
52
        $foo->bar = new GH8014Bar();
53
        $foo->bar->startDate = new DateTimeImmutable();
54
        $this->_em->persist($foo);
55
        $this->_em->flush();
56
        $this->_em->clear();
57
58
        return $foo;
59
    }
60
}
61
62
/**
63
 * @Entity()
64
 */
65
class GH8014Foo
66
{
67
    /**
68
     * @var integer
69
     *
70
     * @Id()
71
     * @Column(name="id", type="integer")
72
     * @GeneratedValue(strategy="IDENTITY")
73
     */
74
    public $id;
75
76
    /**
77
     * @Embedded(class="Doctrine\Test\ORM\Functional\Ticket\GH8014Bar")
78
     */
79
    public ?GH8014Bar $bar = null;
80
}
81
82
/**
83
 * @Embeddable()
84
 */
85
class GH8014Bar
86
{
87
    /**
88
     * @Column(type="datetime_immutable", nullable=true)
89
     */
90
    public DateTimeImmutable $startDate;
91
}