Skip to content

Commit 3272e1c

Browse files
authored
Merge pull request #12008 from greg0ire/add-test-to-todo-list
Ensure proxies implementations behave the same on entity not found
2 parents 06109f3 + 69da22d commit 3272e1c

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/Proxy/ProxyFactory.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,23 @@ public function __construct(
185185
public function getProxy(string $className, array $identifier): object
186186
{
187187
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
188-
$classMetadata = $this->em->getClassMetadata($className);
189-
$entityPersister = $this->uow->getEntityPersister($className);
190-
191-
$proxy = $classMetadata->reflClass->newLazyGhost(static function (object $object) use ($identifier, $entityPersister): void {
192-
$entityPersister->loadById($identifier, $object);
188+
$classMetadata = $this->em->getClassMetadata($className);
189+
$entityPersister = $this->uow->getEntityPersister($className);
190+
$identifierFlattener = $this->identifierFlattener;
191+
192+
$proxy = $classMetadata->reflClass->newLazyGhost(static function (object $object) use (
193+
$identifier,
194+
$entityPersister,
195+
$identifierFlattener,
196+
$classMetadata,
197+
): void {
198+
$original = $entityPersister->loadById($identifier, $object);
199+
if ($original === null) {
200+
throw EntityNotFoundException::fromClassNameAndIdentifier(
201+
$classMetadata->getName(),
202+
$identifierFlattener->flattenIdentifier($classMetadata, $identifier),
203+
);
204+
}
193205
}, ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE);
194206

195207
foreach ($identifier as $idField => $value) {

tests/Tests/ORM/Proxy/ProxyFactoryTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,13 @@ public function testSkipAbstractClassesOnGeneration(): void
122122
#[Group('DDC-2432')]
123123
public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
124124
{
125-
if ($this->emMock->getConfiguration()->isNativeLazyObjectsEnabled()) {
126-
self::markTestSkipped('This test is not relevant when native lazy objects are enabled');
127-
}
128-
129125
$persister = $this->getMockBuilder(BasicEntityPersister::class)
130126
->onlyMethods(['load'])
131127
->disableOriginalConstructor()
132128
->getMock();
133129
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
134130

135131
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
136-
assert($proxy instanceof Proxy);
137132

138133
$persister
139134
->expects(self::atLeastOnce())
@@ -146,24 +141,31 @@ public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
146141
} catch (EntityNotFoundException) {
147142
}
148143

149-
self::assertFalse($proxy->__isInitialized());
144+
self::assertUninitializedLazyObject($proxy);
150145
}
151146

152-
#[Group('DDC-2432')]
153-
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
147+
private static function assertUninitializedLazyObject(object $proxy): void
154148
{
155-
if ($this->emMock->getConfiguration()->isNativeLazyObjectsEnabled()) {
156-
self::markTestSkipped('This test is not relevant when native lazy objects are enabled');
149+
if ($proxy instanceof Proxy) {
150+
self::assertFalse($proxy->__isInitialized());
151+
152+
return;
157153
}
158154

155+
$reflectionClass = new ReflectionClass($proxy);
156+
self::assertTrue($reflectionClass->isUninitializedLazyObject($proxy));
157+
}
158+
159+
#[Group('DDC-2432')]
160+
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
161+
{
159162
$persister = $this->getMockBuilder(BasicEntityPersister::class)
160163
->onlyMethods(['load', 'getClassMetadata'])
161164
->disableOriginalConstructor()
162165
->getMock();
163166
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
164167

165168
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
166-
assert($proxy instanceof Proxy);
167169

168170
$persister
169171
->expects(self::atLeastOnce())
@@ -177,7 +179,7 @@ public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
177179
} catch (EntityNotFoundException) {
178180
}
179181

180-
self::assertFalse($proxy->__isInitialized());
182+
self::assertUninitializedLazyObject($proxy);
181183
}
182184

183185
public function testProxyClonesParentFields(): void

0 commit comments

Comments
 (0)