Skip to content

Commit 2a4ebca

Browse files
committed
Refactor tests to avoid using instance properties to track postLoad
The old proxy implementation of doctrine/common was triggered by public methods rather than access to properties (making public properties unsupported in entities), so tests could use public instance properties to track the state of postLoad lifecycle callbacks without triggering the proxy initialization when reading that state (which then changes the state of triggering the postLoad callback). As the new proxy implementation hooks into properties instead, the tests now use a static method (ensuring it is reset properly before loading the instance for which we care about the tracking) instead of an instance property.
1 parent cc29ae0 commit 2a4ebca

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

tests/Tests/ORM/Functional/LifecycleCallbackTest.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public function testPreSavePostSaveCallbacksAreInvoked(): void
6666
self::assertTrue($entity->postPersistCallbackInvoked);
6767

6868
$this->_em->clear();
69+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
6970

7071
$query = $this->_em->createQuery('select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e');
7172
$result = $query->getResult();
72-
self::assertTrue($result[0]->postLoadCallbackInvoked);
73+
self::assertTrue($result[0]::$postLoadCallbackInvoked);
7374

7475
$result[0]->value = 'hello again';
7576

@@ -130,12 +131,14 @@ public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger(): v
130131
$id = $entity->getId();
131132

132133
$this->_em->clear();
134+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
133135

134136
$reference = $this->_em->getReference(LifecycleCallbackTestEntity::class, $id);
135-
self::assertFalse($reference->postLoadCallbackInvoked);
137+
self::assertFalse($reference::$postLoadCallbackInvoked);
138+
$this->assertTrue($this->isUninitializedObject($reference));
136139

137140
$reference->getValue(); // trigger proxy load
138-
self::assertTrue($reference->postLoadCallbackInvoked);
141+
self::assertTrue($reference::$postLoadCallbackInvoked);
139142
}
140143

141144
/** @group DDC-958 */
@@ -148,13 +151,14 @@ public function testPostLoadTriggeredOnRefresh(): void
148151
$id = $entity->getId();
149152

150153
$this->_em->clear();
154+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
151155

152156
$reference = $this->_em->find(LifecycleCallbackTestEntity::class, $id);
153-
self::assertTrue($reference->postLoadCallbackInvoked);
154-
$reference->postLoadCallbackInvoked = false;
157+
self::assertTrue($reference::$postLoadCallbackInvoked);
158+
$reference::$postLoadCallbackInvoked = false;
155159

156160
$this->_em->refresh($reference);
157-
self::assertTrue($reference->postLoadCallbackInvoked, 'postLoad should be invoked when refresh() is called.');
161+
self::assertTrue($reference::$postLoadCallbackInvoked, 'postLoad should be invoked when refresh() is called.');
158162
}
159163

160164
/** @group DDC-113 */
@@ -197,6 +201,7 @@ public function testCascadedEntitiesLoadedInPostLoad(): void
197201

198202
$this->_em->flush();
199203
$this->_em->clear();
204+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
200205

201206
$dql = <<<'DQL'
202207
SELECT
@@ -214,9 +219,9 @@ public function testCascadedEntitiesLoadedInPostLoad(): void
214219
->createQuery(sprintf($dql, $e1->getId(), $e2->getId()))
215220
->getResult();
216221

217-
self::assertTrue(current($entities)->postLoadCallbackInvoked);
222+
self::assertTrue(current($entities)::$postLoadCallbackInvoked);
218223
self::assertTrue(current($entities)->postLoadCascaderNotNull);
219-
self::assertTrue(current($entities)->cascader->postLoadCallbackInvoked);
224+
self::assertTrue(current($entities)->cascader::$postLoadCallbackInvoked);
220225
self::assertEquals(current($entities)->cascader->postLoadEntitiesCount, 2);
221226
}
222227

@@ -239,6 +244,8 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIteration(): void
239244

240245
$this->_em->flush();
241246
$this->_em->clear();
247+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
248+
LifecycleCallbackCascader::$postLoadCallbackInvoked = false;
242249

243250
$dql = <<<'DQL'
244251
SELECT
@@ -256,7 +263,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIteration(): void
256263
$result = iterator_to_array($query->iterate());
257264

258265
foreach ($result as $entity) {
259-
self::assertTrue($entity[0]->postLoadCallbackInvoked);
266+
self::assertTrue($entity[0]::$postLoadCallbackInvoked);
260267
self::assertFalse($entity[0]->postLoadCascaderNotNull);
261268

262269
break;
@@ -265,7 +272,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIteration(): void
265272
$iterableResult = iterator_to_array($query->toIterable());
266273

267274
foreach ($iterableResult as $entity) {
268-
self::assertTrue($entity->postLoadCallbackInvoked);
275+
self::assertTrue($entity::$postLoadCallbackInvoked);
269276
self::assertFalse($entity->postLoadCascaderNotNull);
270277

271278
break;
@@ -283,6 +290,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIterationWithSimple
283290

284291
$this->_em->flush();
285292
$this->_em->clear();
293+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
286294

287295
$query = $this->_em->createQuery(
288296
'SELECT e FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity AS e'
@@ -291,7 +299,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIterationWithSimple
291299
$result = iterator_to_array($query->iterate(null, Query::HYDRATE_SIMPLEOBJECT));
292300

293301
foreach ($result as $entity) {
294-
self::assertTrue($entity[0]->postLoadCallbackInvoked);
302+
self::assertTrue($entity[0]::$postLoadCallbackInvoked);
295303
self::assertFalse($entity[0]->postLoadCascaderNotNull);
296304

297305
break;
@@ -300,7 +308,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIterationWithSimple
300308
$result = iterator_to_array($query->toIterable([], Query::HYDRATE_SIMPLEOBJECT));
301309

302310
foreach ($result as $entity) {
303-
self::assertTrue($entity->postLoadCallbackInvoked);
311+
self::assertTrue($entity::$postLoadCallbackInvoked);
304312
self::assertFalse($entity->postLoadCascaderNotNull);
305313

306314
break;
@@ -325,6 +333,8 @@ public function testPostLoadIsInvokedOnFetchJoinedEntities(): void
325333

326334
$this->_em->flush();
327335
$this->_em->clear();
336+
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
337+
LifecycleCallbackCascader::$postLoadCallbackInvoked = false;
328338

329339
$dql = <<<'DQL'
330340
SELECT
@@ -342,9 +352,9 @@ public function testPostLoadIsInvokedOnFetchJoinedEntities(): void
342352
->createQuery($dql)->setParameter('entA_id', $entA->getId())
343353
->getOneOrNullResult();
344354

345-
self::assertTrue($fetchedA->postLoadCallbackInvoked);
355+
self::assertTrue($fetchedA::$postLoadCallbackInvoked);
346356
foreach ($fetchedA->entities as $fetchJoinedEntB) {
347-
self::assertTrue($fetchJoinedEntB->postLoadCallbackInvoked);
357+
self::assertTrue($fetchJoinedEntB::$postLoadCallbackInvoked);
348358
}
349359
}
350360

@@ -492,7 +502,7 @@ class LifecycleCallbackTestEntity
492502
public $postPersistCallbackInvoked = false;
493503

494504
/** @var bool */
495-
public $postLoadCallbackInvoked = false;
505+
public static $postLoadCallbackInvoked = false;
496506

497507
/** @var bool */
498508
public $postLoadCascaderNotNull = false;
@@ -546,7 +556,7 @@ public function doStuffOnPostPersist(): void
546556
/** @PostLoad */
547557
public function doStuffOnPostLoad(): void
548558
{
549-
$this->postLoadCallbackInvoked = true;
559+
self::$postLoadCallbackInvoked = true;
550560
$this->postLoadCascaderNotNull = isset($this->cascader);
551561
}
552562

@@ -572,7 +582,7 @@ class LifecycleCallbackCascader
572582
{
573583
/* test stuff */
574584
/** @var bool */
575-
public $postLoadCallbackInvoked = false;
585+
public static $postLoadCallbackInvoked = false;
576586

577587
/** @var int */
578588
public $postLoadEntitiesCount = 0;
@@ -599,7 +609,7 @@ public function __construct()
599609
/** @PostLoad */
600610
public function doStuffOnPostLoad(): void
601611
{
602-
$this->postLoadCallbackInvoked = true;
612+
self::$postLoadCallbackInvoked = true;
603613
$this->postLoadEntitiesCount = count($this->entities);
604614
}
605615

tests/Tests/ORM/Functional/Ticket/DDC1690Test.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,20 @@ public function testChangeTracking(): void
5151
$parentId = $parent->getId();
5252
$childId = $child->getId();
5353
unset($parent, $child);
54+
DDC1690Parent::$addPropertyChangedListenerInvoked = false;
55+
DDC1690Child::$addPropertyChangedListenerInvoked = false;
5456

5557
$parent = $this->_em->find(DDC1690Parent::class, $parentId);
5658
$child = $this->_em->find(DDC1690Child::class, $childId);
5759

60+
self::assertTrue($parent::$addPropertyChangedListenerInvoked);
5861
self::assertEquals(1, count($parent->listeners));
59-
self::assertCount(0, $child->listeners);
62+
$this->assertTrue($this->isUninitializedObject($child));
63+
self::assertFalse($child::$addPropertyChangedListenerInvoked);
6064

6165
$this->_em->getUnitOfWork()->initializeObject($child);
6266

67+
self::assertTrue($child::$addPropertyChangedListenerInvoked);
6368
self::assertCount(1, $child->listeners);
6469
unset($parent, $child);
6570

@@ -106,6 +111,11 @@ protected function onPropertyChanged($propName, $oldValue, $newValue): void
106111
*/
107112
class DDC1690Parent extends NotifyBaseEntity
108113
{
114+
/**
115+
* @var bool
116+
*/
117+
public static $addPropertyChangedListenerInvoked = false;
118+
109119
/**
110120
* @var int
111121
* @Id
@@ -151,11 +161,23 @@ public function getChild(): DDC1690Child
151161
{
152162
return $this->child;
153163
}
164+
165+
public function addPropertyChangedListener(PropertyChangedListener $listener): void
166+
{
167+
self::$addPropertyChangedListenerInvoked = true;
168+
169+
parent::addPropertyChangedListener($listener);
170+
}
154171
}
155172

156173
/** @Entity */
157174
class DDC1690Child extends NotifyBaseEntity
158175
{
176+
/**
177+
* @var bool
178+
*/
179+
public static $addPropertyChangedListenerInvoked = false;
180+
159181
/**
160182
* @var int
161183
* @Id
@@ -201,4 +223,11 @@ public function getParent(): DDC1690Parent
201223
{
202224
return $this->parent;
203225
}
226+
227+
public function addPropertyChangedListener(PropertyChangedListener $listener): void
228+
{
229+
self::$addPropertyChangedListenerInvoked = true;
230+
231+
parent::addPropertyChangedListener($listener);
232+
}
204233
}

tests/Tests/ORM/Functional/Ticket/DDC2230Test.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ public function testNotifyTrackingCalledOnProxyInitialization(): void
5757
$this->_em->persist($insertedAddress);
5858
$this->_em->flush();
5959
$this->_em->clear();
60+
DDC2230Address::$listener = null; // Reset the tracking state
6061

6162
$addressProxy = $this->_em->getReference(DDC2230Address::class, $insertedAddress->id);
6263
assert($addressProxy instanceof DDC2230Address);
6364

6465
self::assertTrue($this->isUninitializedObject($addressProxy));
65-
self::assertNull($addressProxy->listener);
66+
self::assertNull($addressProxy::$listener);
6667

6768
$this->_em->getUnitOfWork()->initializeObject($addressProxy);
6869

69-
self::assertSame($this->_em->getUnitOfWork(), $addressProxy->listener);
70+
self::assertSame($this->_em->getUnitOfWork(), $addressProxy::$listener);
7071
}
7172
}
7273

@@ -102,12 +103,12 @@ class DDC2230Address implements NotifyPropertyChanged
102103
*/
103104
public $id;
104105

105-
/** @var \Doctrine\Common\PropertyChangedListener */
106-
public $listener;
106+
/** @var \Doctrine\Common\PropertyChangedListener|null */
107+
public static $listener;
107108

108109
/** {@inheritDoc} */
109110
public function addPropertyChangedListener(PropertyChangedListener $listener)
110111
{
111-
$this->listener = $listener;
112+
self::$listener = $listener;
112113
}
113114
}

0 commit comments

Comments
 (0)