diff --git a/Registry.php b/Registry.php index 444aaead0..ec4690f08 100644 --- a/Registry.php +++ b/Registry.php @@ -9,7 +9,6 @@ use Symfony\Bridge\Doctrine\ManagerRegistry; use Symfony\Bridge\Doctrine\RegistryInterface; use Symfony\Contracts\Service\ResetInterface; -use function interface_exists; /** * References all Doctrine connections and entity managers in a given Container. @@ -170,12 +169,25 @@ public function getEntityManagerForClass($class) public function reset() : void { - if (! interface_exists(LazyLoadingInterface::class)) { + foreach ($this->getManagerNames() as $managerName => $serviceId) { + $this->resetOrClearManager($managerName, $serviceId); + } + } + + private function resetOrClearManager(string $managerName, string $serviceId) : void + { + if (! $this->container->initialized($serviceId)) { return; } - foreach (array_keys($this->getManagerNames()) as $managerName) { - $this->resetManager($managerName); + $manager = $this->container->get($serviceId); + + if (! $manager instanceof LazyLoadingInterface) { + $manager->clear(); + + return; } + + $this->resetManager($managerName); } } diff --git a/Tests/RegistryTest.php b/Tests/RegistryTest.php index a2eccabcd..2468fcca9 100644 --- a/Tests/RegistryTest.php +++ b/Tests/RegistryTest.php @@ -2,7 +2,10 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests; +use Closure; use Doctrine\Bundle\DoctrineBundle\Registry; +use Doctrine\ORM\EntityManagerInterface; +use ProxyManager\Proxy\LazyLoadingInterface; use stdClass; class RegistryTest extends TestCase @@ -125,13 +128,33 @@ public function testResetUnknownEntityManager() public function testReset() { + $noProxyManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); + $noProxyManager->expects($this->once()) + ->method('clear'); + + $proxyManager = $this->getMockBuilder(LazyLoadingInterface::class)->getMock(); + $proxyManager->expects($this->once()) + ->method('setProxyInitializer') + ->with($this->isInstanceOf(Closure::class)); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); - $container->expects($this->once()) + $container->expects($this->any()) ->method('initialized') - ->with($this->equalTo('doctrine.orm.default_entity_manager')) - ->will($this->returnValue(false)); + ->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager']) + ->willReturnOnConsecutiveCalls(false, true, true, true); - $registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default'); + $container->expects($this->any()) + ->method('get') + ->withConsecutive(['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager']) + ->willReturnOnConsecutiveCalls($noProxyManager, $proxyManager, $proxyManager); + + $entityManagers = [ + 'uninitialized' => 'doctrine.orm.uninitialized_entity_manager', + 'noproxy' => 'doctrine.orm.noproxy_entity_manager', + 'proxy' => 'doctrine.orm.proxy_entity_manager', + ]; + + $registry = new Registry($container, [], $entityManagers, 'default', 'default'); $registry->reset(); } }