Passed
Pull Request — 2.6 (#8015)
by
unknown
07:21
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
        if (PHP_VERSION_ID >= 70400) {
15
            $this->_schemaTool->createSchema(
16
                [
17
                    $this->_em->getClassMetadata(GH8014Foo::class),
18
                ]
19
            );
20
        }
21
    }
22
23
    public function testNonNullablePropertyOfEmbeddableCanBeHydratedWithNullValueInDatabase()
24
    {
25
        if (PHP_VERSION_ID < 70400) {
26
            self::markTestSkipped('This test only applies to PHP versions higher than 7.4');
27
28
            return;
29
        }
30
31
        $foo = $this->createEntityWithoutTheNullablePropertySet();
32
        $foo = $this->_em->find(
33
            GH8014Foo::class,
34
            $foo->id
35
        ); // Used to throw "Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must be an instance of DateTimeImmutable, null used"
36
37
        $this->expectException(\Error::class);
38
        $this->expectExceptionMessage('Typed property Doctrine\Test\ORM\Functional\Ticket\GH8014Bar::$startDate must not be accessed before initialization');
39
        $foo->bar->startDate;
40
41
        $foo = $this->createEntityWithTheNullablePropertySet();
42
        $foo = $this->_em->find(GH8014Foo::class, $foo->id);
43
44
        $this->assertNotNull($foo->bar->startDate);
45
    }
46
47
    private function createEntityWithoutTheNullablePropertySet(): GH8014Foo
48
    {
49
        $foo = new GH8014Foo();
50
        $this->_em->persist($foo);
51
        $this->_em->flush();
52
        $this->_em->clear();
53
54
        return $foo;
55
    }
56
57
    private function createEntityWithTheNullablePropertySet(): GH8014Foo
58
    {
59
        $foo = new GH8014Foo();
60
        $foo->bar = new GH8014Bar();
61
        $foo->bar->startDate = new DateTimeImmutable();
62
        $this->_em->persist($foo);
63
        $this->_em->flush();
64
        $this->_em->clear();
65
66
        return $foo;
67
    }
68
}
69
70
if (PHP_VERSION_ID >= 70400) {
71
    require_once __DIR__.'/GH8014_php74.php';
72
}
73