File: IdentityMapTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (257 lines) | stat: -rw-r--r-- 9,056 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Query;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

/**
 * IdentityMapTest
 *
 * Tests correct behavior and usage of the identity map. Local values and associations
 * that are already fetched always prevail, unless explicitly refreshed.
 */
class IdentityMapTest extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        $this->useModelSet('cms');

        parent::setUp();
    }

    public function testBasicIdentityManagement(): void
    {
        $user           = new CmsUser();
        $user->status   = 'dev';
        $user->username = 'romanb';
        $user->name     = 'Roman B.';

        $address          = new CmsAddress();
        $address->country = 'de';
        $address->zip     = 1234;
        $address->city    = 'Berlin';

        $user->setAddress($address);

        $this->_em->persist($user);
        $this->_em->flush();
        $this->_em->clear();

        $user2 = $this->_em->find($user::class, $user->getId());
        self::assertNotSame($user2, $user);
        $user3 = $this->_em->find($user::class, $user->getId());
        self::assertSame($user2, $user3);

        $address2 = $this->_em->find($address::class, $address->getId());
        self::assertNotSame($address2, $address);
        $address3 = $this->_em->find($address::class, $address->getId());
        self::assertSame($address2, $address3);

        self::assertSame($user2->getAddress(), $address2); // !!!
    }

    public function testSingleValuedAssociationIdentityMapBehaviorWithRefresh(): void
    {
        $address          = new CmsAddress();
        $address->country = 'de';
        $address->zip     = '12345';
        $address->city    = 'Berlin';

        $user1           = new CmsUser();
        $user1->status   = 'dev';
        $user1->username = 'romanb';
        $user1->name     = 'Roman B.';

        $user2           = new CmsUser();
        $user2->status   = 'dev';
        $user2->username = 'gblanco';
        $user2->name     = 'Guilherme Blanco';

        $address->setUser($user1);

        $this->_em->persist($address);
        $this->_em->persist($user1);
        $this->_em->persist($user2);
        $this->_em->flush();

        self::assertSame($user1, $address->user);

        //external update to CmsAddress
        $this->_em->getConnection()->executeStatement('update cms_addresses set user_id = ?', [$user2->getId()]);

        // But we want to have this external change!
        // Solution 1: refresh(), broken atm!
        $this->_em->refresh($address);

        // Now the association should be "correct", referencing $user2
        self::assertSame($user2, $address->user);
        self::assertSame($user2->address, $address); // check back reference also

        // Attention! refreshes can result in broken bidirectional associations! this is currently expected!
        // $user1 still points to $address!
        self::assertSame($user1->address, $address);
    }

    public function testSingleValuedAssociationIdentityMapBehaviorWithRefreshQuery(): void
    {
        $address          = new CmsAddress();
        $address->country = 'de';
        $address->zip     = '12345';
        $address->city    = 'Berlin';

        $user1           = new CmsUser();
        $user1->status   = 'dev';
        $user1->username = 'romanb';
        $user1->name     = 'Roman B.';

        $user2           = new CmsUser();
        $user2->status   = 'dev';
        $user2->username = 'gblanco';
        $user2->name     = 'Guilherme Blanco';

        $address->setUser($user1);

        $this->_em->persist($address);
        $this->_em->persist($user1);
        $this->_em->persist($user2);
        $this->_em->flush();

        self::assertSame($user1, $address->user);

        //external update to CmsAddress
        $this->_em->getConnection()->executeStatement('update cms_addresses set user_id = ?', [$user2->getId()]);

        //select
        $q        = $this->_em->createQuery('select a, u from Doctrine\Tests\Models\CMS\CmsAddress a join a.user u');
        $address2 = $q->getSingleResult();

        self::assertSame($address, $address2);

        // Should still be $user1
        self::assertSame($user1, $address2->user);
        self::assertNull($user2->address);

        // But we want to have this external change!
        // Solution 2: Alternatively, a refresh query should work
        $q = $this->_em->createQuery('select a, u from Doctrine\Tests\Models\CMS\CmsAddress a join a.user u');
        $q->setHint(Query::HINT_REFRESH, true);
        $address3 = $q->getSingleResult();

        self::assertSame($address, $address3); // should still be the same, always from identity map

        // Now the association should be "correct", referencing $user2
        self::assertSame($user2, $address2->user);
        self::assertSame($user2->address, $address2); // check back reference also

        // Attention! refreshes can result in broken bidirectional associations! this is currently expected!
        // $user1 still points to $address2!
        self::assertSame($user1->address, $address2);
    }

    public function testCollectionValuedAssociationIdentityMapBehaviorWithRefreshQuery(): void
    {
        $user           = new CmsUser();
        $user->status   = 'dev';
        $user->username = 'romanb';
        $user->name     = 'Roman B.';

        $phone1              = new CmsPhonenumber();
        $phone1->phonenumber = 123;

        $phone2              = new CmsPhonenumber();
        $phone2->phonenumber = 234;

        $phone3              = new CmsPhonenumber();
        $phone3->phonenumber = 345;

        $user->addPhonenumber($phone1);
        $user->addPhonenumber($phone2);
        $user->addPhonenumber($phone3);

        $this->_em->persist($user); // cascaded to phone numbers
        $this->_em->flush();

        self::assertCount(3, $user->getPhonenumbers());
        self::assertFalse($user->getPhonenumbers()->isDirty());

        //external update to CmsAddress
        $this->_em->getConnection()->executeStatement('insert into cms_phonenumbers (phonenumber, user_id) VALUES (?,?)', [999, $user->getId()]);

        //select
        $q     = $this->_em->createQuery('select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p');
        $user2 = $q->getSingleResult();

        self::assertSame($user, $user2);

        // Should still be the same 3 phonenumbers
        self::assertCount(3, $user2->getPhonenumbers());

        // But we want to have this external change!
        // Solution 1: refresh().
        //$this->_em->refresh($user2); broken atm!
        // Solution 2: Alternatively, a refresh query should work
        $q = $this->_em->createQuery('select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p');
        $q->setHint(Query::HINT_REFRESH, true);
        $user3 = $q->getSingleResult();

        self::assertSame($user, $user3); // should still be the same, always from identity map

        // Now the collection should be refreshed with correct count
        self::assertCount(4, $user3->getPhonenumbers());
    }

    #[Group('non-cacheable')]
    public function testCollectionValuedAssociationIdentityMapBehaviorWithRefresh(): void
    {
        $user           = new CmsUser();
        $user->status   = 'dev';
        $user->username = 'romanb';
        $user->name     = 'Roman B.';

        $phone1              = new CmsPhonenumber();
        $phone1->phonenumber = 123;

        $phone2              = new CmsPhonenumber();
        $phone2->phonenumber = 234;

        $phone3              = new CmsPhonenumber();
        $phone3->phonenumber = 345;

        $user->addPhonenumber($phone1);
        $user->addPhonenumber($phone2);
        $user->addPhonenumber($phone3);

        $this->_em->persist($user); // cascaded to phone numbers
        $this->_em->flush();

        self::assertCount(3, $user->getPhonenumbers());

        //external update to CmsAddress
        $this->_em->getConnection()->executeStatement('insert into cms_phonenumbers (phonenumber, user_id) VALUES (?,?)', [999, $user->getId()]);

        //select
        $q     = $this->_em->createQuery('select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p');
        $user2 = $q->getSingleResult();

        self::assertSame($user, $user2);

        // Should still be the same 3 phonenumbers
        self::assertCount(3, $user2->getPhonenumbers());

        // But we want to have this external change!
        // Solution 1: refresh().
        $this->_em->refresh($user2);

        self::assertSame($user, $user2); // should still be the same, always from identity map

        // Now the collection should be refreshed with correct count
        self::assertCount(4, $user2->getPhonenumbers());
    }
}