Skip to content

Commit

Permalink
Restore document proxy state to uninitialized on load exception
Browse files Browse the repository at this point in the history
  • Loading branch information
notrix committed Jun 9, 2023
1 parent e5fb1a4 commit 58398f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/Doctrine/ORM/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ReflectionProperty;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\VarExporter;
use Throwable;

use function array_flip;
use function str_replace;
Expand Down Expand Up @@ -204,7 +205,17 @@ private function createInitializer(ClassMetadata $classMetadata, EntityPersister

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

if ($entityPersister->loadById($identifier, $proxy) === null) {
try {
$entity = $entityPersister->loadById($identifier, $proxy);
} catch (Throwable $exception) {
$proxy->__setInitializer($initializer);
$proxy->__setCloner($cloner);
$proxy->__setInitialized(false);

throw $exception;
}

if ($entity === null) {
$proxy->__setInitializer($initializer);
$proxy->__setCloner($cloner);
$proxy->__setInitialized(false);
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
use Doctrine\Tests\OrmTestCase;
use Doctrine\Tests\PHPUnitCompatibility\MockBuilderCompatibilityTools;
use Exception;
use ReflectionProperty;
use stdClass;

Expand Down Expand Up @@ -145,6 +146,33 @@ public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
self::assertFalse($proxy->__isInitialized());
}

public function testExceptionOnProxyLoadingDoesNotMarkTheProxyAsInitialized(): void
{
$persister = $this
->getMockBuilderWithOnlyMethods(BasicEntityPersister::class, ['load', 'getClassMetadata'])
->disableOriginalConstructor()
->getMock();
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);

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

$exception = new Exception('Literally any kind of connection exception');

$persister
->expects(self::atLeastOnce())
->method('load')
->will(self::throwException($exception));

try {
$proxy->getDescription();
self::fail('An exception was expected to be raised');
} catch (Exception $exception) {
}

self::assertFalse($proxy->__isInitialized(), 'The proxy should not be initialized');
}

/** @group DDC-2432 */
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
{
Expand Down

0 comments on commit 58398f5

Please sign in to comment.