Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore document proxy state to uninitialized on load exception #10645

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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