Skip to content

Commit 58398f5

Browse files
committed
Restore document proxy state to uninitialized on load exception
1 parent e5fb1a4 commit 58398f5

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/Doctrine/ORM/Proxy/ProxyFactory.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ReflectionProperty;
2222
use Symfony\Component\VarExporter\ProxyHelper;
2323
use Symfony\Component\VarExporter\VarExporter;
24+
use Throwable;
2425

2526
use function array_flip;
2627
use function str_replace;
@@ -204,7 +205,17 @@ private function createInitializer(ClassMetadata $classMetadata, EntityPersister
204205

205206
$identifier = $classMetadata->getIdentifierValues($proxy);
206207

207-
if ($entityPersister->loadById($identifier, $proxy) === null) {
208+
try {
209+
$entity = $entityPersister->loadById($identifier, $proxy);
210+
} catch (Throwable $exception) {
211+
$proxy->__setInitializer($initializer);
212+
$proxy->__setCloner($cloner);
213+
$proxy->__setInitialized(false);
214+
215+
throw $exception;
216+
}
217+
218+
if ($entity === null) {
208219
$proxy->__setInitializer($initializer);
209220
$proxy->__setCloner($cloner);
210221
$proxy->__setInitialized(false);

tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
2222
use Doctrine\Tests\OrmTestCase;
2323
use Doctrine\Tests\PHPUnitCompatibility\MockBuilderCompatibilityTools;
24+
use Exception;
2425
use ReflectionProperty;
2526
use stdClass;
2627

@@ -145,6 +146,33 @@ public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
145146
self::assertFalse($proxy->__isInitialized());
146147
}
147148

149+
public function testExceptionOnProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
150+
{
151+
$persister = $this
152+
->getMockBuilderWithOnlyMethods(BasicEntityPersister::class, ['load', 'getClassMetadata'])
153+
->disableOriginalConstructor()
154+
->getMock();
155+
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
156+
157+
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
158+
assert($proxy instanceof Proxy);
159+
160+
$exception = new Exception('Literally any kind of connection exception');
161+
162+
$persister
163+
->expects(self::atLeastOnce())
164+
->method('load')
165+
->will(self::throwException($exception));
166+
167+
try {
168+
$proxy->getDescription();
169+
self::fail('An exception was expected to be raised');
170+
} catch (Exception $exception) {
171+
}
172+
173+
self::assertFalse($proxy->__isInitialized(), 'The proxy should not be initialized');
174+
}
175+
148176
/** @group DDC-2432 */
149177
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
150178
{

0 commit comments

Comments
 (0)