Skip to content

Commit

Permalink
Merge branch '1.12.x' into 2.0.x
Browse files Browse the repository at this point in the history
* 1.12.x:
  Fix resetting non-lazy managers
  • Loading branch information
alcaeus committed Dec 19, 2019
2 parents 522c166 + 08f9447 commit 0ef972d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
20 changes: 16 additions & 4 deletions Registry.php
Expand Up @@ -7,7 +7,6 @@
use Psr\Container\ContainerInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Contracts\Service\ResetInterface;
use function interface_exists;

/**
* References all Doctrine connections and entity managers in a given Container.
Expand Down Expand Up @@ -52,12 +51,25 @@ public function getAliasNamespace($alias)

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);
}
}
31 changes: 27 additions & 4 deletions Tests/RegistryTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 0ef972d

Please sign in to comment.