Skip to content

Commit

Permalink
Allow resetting lazy managers on Symfony 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jul 13, 2022
1 parent 2d8779d commit fde8a52
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
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()) {
$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
{
}
34 changes: 33 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,37 @@ 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+');
}

$ghostManager = $this->createMock(LazyGhostObjectEntityManagerInterface::class);
$ghostManager->expects($this->once())
->method('resetLazyGhostObject')
->willReturn(true);

$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->any())
->method('initialized')
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
->willReturnOnConsecutiveCalls(false, true, true);

$container->expects($this->any())
->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',
];

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

public function testIdentityMapsStayConsistentAfterReset(): void
{
if (! interface_exists(EntityManagerInterface::class)) {
Expand All @@ -183,7 +215,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

0 comments on commit fde8a52

Please sign in to comment.