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

GH8014Test::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
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),
0 ignored issues
show
Bug introduced by
The type Doctrine\Test\ORM\Functional\Ticket\GH8014Foo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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();
0 ignored issues
show
Bug introduced by
The type Doctrine\Test\ORM\Functional\Ticket\GH8014Bar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    eval(<<<EOPHP
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
72
    /**
73
     * @Entity()
74
     */
75
    class GH8014Foo
76
    {
77
        /**
78
         * @var integer
79
         *
80
         * @Id()
81
         * @Column(name="id", type="integer")
82
         * @GeneratedValue(strategy="IDENTITY")
83
         */
84
        public \$id;
85
86
        /**
87
         * @Embedded(class="Doctrine\Test\ORM\Functional\Ticket\GH8014Bar")
88
         */
89
        public ?GH8014Bar \$bar = null;
90
    }
91
92
    /**
93
     * @Embeddable()
94
     */
95
    class GH8014Bar
96
    {
97
        /**
98
         * @Column(type="datetime_immutable", nullable=true)
99
         */
100
        public DateTimeImmutable \$startDate;
101
    }
102
EOPHP
103
    );
104
}
105