From e9fd82e1b0fc5478823388b0e39f12d1da69c1cc Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 12 Jul 2022 17:55:09 +0200 Subject: [PATCH 1/2] Allow resetting lazy managers on Symfony 6.2 --- Registry.php | 3 +- .../LazyGhostObjectEntityManagerInterface.php | 10 ++++++ Tests/RegistryTest.php | 34 ++++++++++++++++++- psalm.xml.dist | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 Tests/LazyGhostObjectEntityManagerInterface.php diff --git a/Registry.php b/Registry.php index a29c74724..12b143e86 100644 --- a/Registry.php +++ b/Registry.php @@ -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; @@ -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; diff --git a/Tests/LazyGhostObjectEntityManagerInterface.php b/Tests/LazyGhostObjectEntityManagerInterface.php new file mode 100644 index 000000000..79a78549a --- /dev/null +++ b/Tests/LazyGhostObjectEntityManagerInterface.php @@ -0,0 +1,10 @@ +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)) { @@ -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); diff --git a/psalm.xml.dist b/psalm.xml.dist index a0cb0fb23..3fc5fee0c 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -43,6 +43,7 @@ + From 5ec48b1855b0131c250f13c83c64fe4f9b09cfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Wed, 13 Jul 2022 21:59:10 +0200 Subject: [PATCH 2/2] Psalm: suppress bogus MissingDependency --- Tests/RegistryTest.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Tests/RegistryTest.php b/Tests/RegistryTest.php index 70f3af3ea..953e3c422 100644 --- a/Tests/RegistryTest.php +++ b/Tests/RegistryTest.php @@ -176,19 +176,16 @@ public function testResetGhostObject(): void 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); - $ghostManager->expects($this->once()) - ->method('resetLazyGhostObject') - ->willReturn(true); + /** @psalm-suppress MissingDependency https://github.com/vimeo/psalm/issues/8258 */ + $ghostManager->expects($this->once())->method('resetLazyGhostObject')->willReturn(true); - $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); - $container->expects($this->any()) - ->method('initialized') + $container = $this->createMock(ContainerInterface::class); + $container->method('initialized') ->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.ghost_entity_manager']) ->willReturnOnConsecutiveCalls(false, true, true); - - $container->expects($this->any()) - ->method('get') + $container->method('get') ->withConsecutive(['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager']) ->willReturnOnConsecutiveCalls($ghostManager, $ghostManager, $ghostManager); @@ -197,8 +194,7 @@ public function testResetGhostObject(): void 'ghost' => 'doctrine.orm.ghost_entity_manager', ]; - $registry = new Registry($container, [], $entityManagers, 'default', 'default'); - $registry->reset(); + (new Registry($container, [], $entityManagers, 'default', 'default'))->reset(); } public function testIdentityMapsStayConsistentAfterReset(): void