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

Allow resetting lazy managers on Symfony 6.2 #1545

Merged
merged 2 commits into from
Jul 13, 2022
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
3 changes: 2 additions & 1 deletion Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Component\VarExporter\LazyGhostObjectInterface;
use Symfony\Contracts\Service\ResetInterface;

use function array_keys;
Expand Down Expand Up @@ -75,7 +76,7 @@ private function resetOrClearManager(string $managerName, string $serviceId): vo

assert($manager instanceof EntityManagerInterface);

if (! $manager instanceof LazyLoadingInterface || $manager->isOpen()) {
if ((! $manager instanceof LazyLoadingInterface && ! $manager instanceof LazyGhostObjectInterface) || $manager->isOpen()) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
$manager->clear();

return;
Expand Down
10 changes: 10 additions & 0 deletions Tests/LazyGhostObjectEntityManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\VarExporter\LazyGhostObjectInterface;

interface LazyGhostObjectEntityManagerInterface extends LazyGhostObjectInterface, EntityManagerInterface
{
}
30 changes: 29 additions & 1 deletion Tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ProxyManager\Proxy\ProxyInterface;
use stdClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\VarExporter\LazyGhostObjectInterface;

use function assert;
use function interface_exists;
Expand Down Expand Up @@ -169,6 +170,33 @@ public function testReset(): void
$registry->reset();
}

public function testResetGhostObject(): void
{
if (! interface_exists(EntityManagerInterface::class) || ! interface_exists(LazyGhostObjectInterface::class)) {
self::markTestSkipped('This test requires ORM and VarExporter 6.2+');
}

/** @psalm-suppress MissingDependency https://github.com/vimeo/psalm/issues/8258 */
$ghostManager = $this->createMock(LazyGhostObjectEntityManagerInterface::class);
/** @psalm-suppress MissingDependency https://github.com/vimeo/psalm/issues/8258 */
$ghostManager->expects($this->once())->method('resetLazyGhostObject')->willReturn(true);

$container = $this->createMock(ContainerInterface::class);
$container->method('initialized')
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
->willReturnOnConsecutiveCalls(false, true, true);
$container->method('get')
->withConsecutive(['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
->willReturnOnConsecutiveCalls($ghostManager, $ghostManager, $ghostManager);

$entityManagers = [
'uninitialized' => 'doctrine.orm.uninitialized_entity_manager',
'ghost' => 'doctrine.orm.ghost_entity_manager',
];

(new Registry($container, [], $entityManagers, 'default', 'default'))->reset();
}

public function testIdentityMapsStayConsistentAfterReset(): void
{
if (! interface_exists(EntityManagerInterface::class)) {
Expand All @@ -183,7 +211,7 @@ public function testIdentityMapsStayConsistentAfterReset(): void
$entityManager = $container->get('doctrine.orm.default_entity_manager');
$repository = $entityManager->getRepository(TestCustomClassRepoEntity::class);

$this->assertInstanceOf(ProxyInterface::class, $entityManager);
$this->assertInstanceOf(interface_exists(LazyGhostObjectInterface::class) ? LazyGhostObjectInterface::class : ProxyInterface::class, $entityManager);
assert($entityManager instanceof EntityManagerInterface);
assert($registry instanceof Registry);
assert($repository instanceof TestCustomClassRepoRepository);
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<errorLevel type="suppress">
<!-- We use the "Foo" namespace in unit tests. We are aware that those classes don't exist. -->
<referencedClass name="Foo\*"/>
<referencedClass name="Symfony\Component\VarExporter\LazyGhostObjectInterface"/>
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
Expand Down