File: DDC2084Test.php

package info (click to toggle)
doctrine 3.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 11,236 kB
  • sloc: php: 105,633; xml: 1,312; makefile: 35; sh: 14
file content (128 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\ORM\Functional\Ticket\DDC2084\MyEntity1;
use Doctrine\Tests\ORM\Functional\Ticket\DDC2084\MyEntity2;
use Doctrine\ORM\ORMInvalidArgumentException;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-2084')]
class DDC2084Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            __NAMESPACE__ . '\DDC2084\MyEntity1',
            __NAMESPACE__ . '\DDC2084\MyEntity2',
        );
    }

    public function loadFixture(): MyEntity1
    {
        $e2 = new MyEntity2('Foo');
        $e1 = new MyEntity1($e2);

        $this->_em->persist($e2);
        $this->_em->flush();

        $this->_em->persist($e1);
        $this->_em->flush();

        $this->_em->clear();

        return $e1;
    }

    public function testIssue(): void
    {
        $e1 = $this->loadFixture();
        $e2 = $e1->getMyEntity2();
        $e  = $this->_em->find(__NAMESPACE__ . '\DDC2084\MyEntity1', $e2);

        self::assertInstanceOf(__NAMESPACE__ . '\DDC2084\MyEntity1', $e);
        self::assertInstanceOf(__NAMESPACE__ . '\DDC2084\MyEntity2', $e->getMyEntity2());
        self::assertEquals('Foo', $e->getMyEntity2()->getValue());
    }

    public function testInvalidIdentifierBindingEntityException(): void
    {
        $this->expectException(ORMInvalidArgumentException::class);
        $this->expectExceptionMessage(
            <<<'EXCEPTION'
Binding entities to query parameters only allowed for entities that have an identifier.
Class "Doctrine\Tests\ORM\Functional\Ticket\DDC2084\MyEntity2" does not have an identifier.
EXCEPTION,
        );
        $this->_em->find(__NAMESPACE__ . '\DDC2084\MyEntity1', new MyEntity2('Foo'));
    }
}

namespace Doctrine\Tests\ORM\Functional\Ticket\DDC2084;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table;

#[Table(name: 'DDC2084_ENTITY1')]
#[Entity]
class MyEntity1
{
    public function __construct(
        #[Id]
        #[OneToOne(targetEntity: 'MyEntity2')]
        #[JoinColumn(name: 'entity2_id', referencedColumnName: 'id', nullable: false)]
        private MyEntity2 $entity2,
    ) {
    }

    public function setMyEntity2(MyEntity2 $myEntity2): void
    {
        $this->entity2 = $myEntity2;
    }

    public function getMyEntity2(): MyEntity2
    {
        return $this->entity2;
    }
}

#[Table(name: 'DDC2084_ENTITY2')]
#[Entity]
class MyEntity2
{
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue(strategy: 'AUTO')]
    private int $id;

    public function __construct(
        #[Column]
        private string $value,
    ) {
    }

    public function getId(): int
    {
        return $this->id;
    }

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

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