File: DDC440Test.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 (206 lines) | stat: -rw-r--r-- 5,678 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\Collection;
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\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

class DDC440Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(DDC440Phone::class, DDC440Client::class);
    }

    #[Group('DDC-440')]
    public function testOriginalEntityDataEmptyWhenProxyLoadedFromTwoAssociations(): void
    {
        /* The key of the problem is that the first phone is fetched via two association, mainPhone and phones.
         *
         * You will notice that the original_entity_datas are not loaded for the first phone. (They are for the second)
         *
         * In the Client entity definition, if you define the mainPhone relation after the phones relation, both assertions pass.
         * (for the sake or this test, I defined the mainPhone relation before the phones relation)
         *
         */

        //Initialize some data
        $client = new DDC440Client();
        $client->setName('Client1');

        $phone = new DDC440Phone();
        $phone->setId(1);
        $phone->setNumber('418 111-1111');
        $phone->setClient($client);

        $phone2 = new DDC440Phone();
        $phone2->setId(2);
        $phone2->setNumber('418 222-2222');
        $phone2->setClient($client);

        $client->setMainPhone($phone);

        $this->_em->persist($client);
        $this->_em->flush();
        $id = $client->getId();
        $this->_em->clear();

        $uw           = $this->_em->getUnitOfWork();
        $client       = $this->_em->find(DDC440Client::class, $id);
        $clientPhones = $client->getPhones();

        $p1 = $clientPhones[1];
        $p2 = $clientPhones[2];

        // Test the first phone.  The assertion actually failed because original entity data is not set properly.
        // This was because it is also set as MainPhone and that one is created as a proxy, not the
        // original object when the find on Client is called. However loading proxies did not work correctly.
        self::assertInstanceOf(DDC440Phone::class, $p1);
        $originalData = $uw->getOriginalEntityData($p1);
        self::assertEquals($phone->getNumber(), $originalData['number']);

        //If you comment out previous test, this one should pass
        self::assertInstanceOf(DDC440Phone::class, $p2);
        $originalData = $uw->getOriginalEntityData($p2);
        self::assertEquals($phone2->getNumber(), $originalData['number']);
    }
}

#[Table(name: 'phone')]
#[Entity]
class DDC440Phone
{
    /** @var int */
    #[Column(name: 'id', type: 'integer')]
    #[Id]
    #[GeneratedValue(strategy: 'AUTO')]
    protected $id;

    /** @var DDC440Client */
    #[JoinColumn(name: 'client_id', referencedColumnName: 'id')]
    #[ManyToOne(targetEntity: 'DDC440Client', inversedBy: 'phones')]
    protected $client;

    /** @var string */
    #[Column(name: 'phonenumber', type: 'string', length: 255)]
    protected $number;

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

    public function getNumber(): string
    {
        return $this->number;
    }

    public function setClient(DDC440Client $value, bool $updateInverse = true): void
    {
        $this->client = $value;
        if ($updateInverse) {
            $value->addPhone($this);
        }
    }

    public function getClient(): DDC440Client
    {
        return $this->client;
    }

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

    public function setId(int $value): void
    {
        $this->id = $value;
    }
}

#[Table(name: 'client')]
#[Entity]
class DDC440Client
{
    /** @var int */
    #[Column(name: 'id', type: 'integer')]
    #[Id]
    #[GeneratedValue(strategy: 'AUTO')]
    protected $id;

    /** @var DDC440Phone */
    #[JoinColumn(name: 'main_phone_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
    #[OneToOne(targetEntity: 'DDC440Phone', fetch: 'EAGER')]
    protected $mainPhone;

    /** @phpstan-var Collection<int, DDC440Phone> */
    #[OneToMany(targetEntity: 'DDC440Phone', mappedBy: 'client', cascade: ['persist', 'remove'], fetch: 'EAGER', indexBy: 'id')]
    #[OrderBy(['number' => 'ASC'])]
    protected $phones;

    /** @var string */
    #[Column(name: 'name', type: 'string', length: 255)]
    protected $name;

    public function __construct()
    {
    }

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

    public function getName(): string
    {
        return $this->name;
    }

    public function addPhone(DDC440Phone $value): void
    {
        $this->phones[] = $value;
        $value->setClient($this, false);
    }

    /** @phpstan-return Collection<int, DDC440Phone> */
    public function getPhones(): Collection
    {
        return $this->phones;
    }

    public function setMainPhone(DDC440Phone $value): void
    {
        $this->mainPhone = $value;
    }

    public function getMainPhone(): DDC440Phone
    {
        return $this->mainPhone;
    }

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

    public function setId($value): void
    {
        $this->id = $value;
    }
}