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:
  Clear open entity managers on kernel reset
  • Loading branch information
alcaeus committed Jan 18, 2020
2 parents e52fb91 + 18fb7d2 commit 6926771
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 64 deletions.
5 changes: 4 additions & 1 deletion Registry.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Bundle\DoctrineBundle;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -64,7 +65,9 @@ private function resetOrClearManager(string $managerName, string $serviceId) : v

$manager = $this->container->get($serviceId);

if (! $manager instanceof LazyLoadingInterface) {
assert($manager instanceof EntityManagerInterface);

if (! $manager instanceof LazyLoadingInterface || $manager->isOpen()) {
$manager->clear();

return;
Expand Down
70 changes: 10 additions & 60 deletions Tests/Command/ImportMappingDoctrineCommandTest.php
Expand Up @@ -2,32 +2,34 @@

namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;

/**
* @group legacy
*/
class ImportMappingDoctrineCommandTest extends TestCase
{
/** @var ImportMappingTestingKernel|null */
/** @var TestKernel|null */
private $kernel;

/** @var CommandTester|null */
private $commandTester;

protected function setup() : void
{
$this->kernel = new ImportMappingTestingKernel();
$this->kernel = new class() extends TestKernel {
public function registerBundles() : iterable
{
yield from parent::registerBundles();
yield new ImportMappingTestFooBundle();
}
};

$this->kernel->boot();

$connection = $this->kernel->getContainer()
Expand Down Expand Up @@ -108,58 +110,6 @@ public function testExecuteAnnotationsWithNamespace() : void
}
}

class ImportMappingTestingKernel extends Kernel
{
/** @var string|null */
private $projectDir;

public function __construct()
{
parent::__construct('test', true);
}

public function registerBundles() : iterable
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
new ImportMappingTestFooBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader) : void
{
$loader->load(function (ContainerBuilder $container) : void {
$container->loadFromExtension('framework', ['secret' => 'F00']);

$container->loadFromExtension('doctrine', [
'dbal' => [
'driver' => 'pdo_sqlite',
'path' => $this->getCacheDir() . '/testing.db',
],
'orm' => [],
]);

// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
$container->register('logger', NullLogger::class);
});
}

public function getProjectDir() : string
{
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
}

return $this->projectDir;
}

public function getRootDir() : string
{
return $this->getProjectDir();
}
}

class ImportMappingTestFooBundle extends Bundle
{
public function getPath() : string
Expand Down
Expand Up @@ -2,8 +2,13 @@

namespace Fixtures\Bundles\RepositoryServiceBundle\Repository;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

class TestCustomClassRepoRepository extends EntityRepository
{
public function getEntityManager() : EntityManager
{
return parent::getEntityManager();
}
}
68 changes: 68 additions & 0 deletions Tests/DependencyInjection/Fixtures/TestKernel.php
@@ -0,0 +1,68 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

class TestKernel extends Kernel
{
/** @var string|null */
private $projectDir;

public function __construct()
{
parent::__construct('test', true);
}

public function registerBundles() : iterable
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(static function (ContainerBuilder $container) {
// @todo Setting the kernel.name parameter can be removed once the dependency on DoctrineCacheBundle has been dropped
$container->setParameter('kernel.name', 'foo');
$container->loadFromExtension('framework', ['secret' => 'F00']);

$container->loadFromExtension('doctrine', [
'dbal' => ['driver' => 'pdo_sqlite'],
'orm' => [
'mappings' => [
'RepositoryServiceBundle' => [
'type' => 'annotation',
'dir' => __DIR__ . '/Bundles/RepositoryServiceBundle/Entity',
'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity',
],
],
],
]);

// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
$container->register('logger', NullLogger::class);
});
}

public function getProjectDir() : string
{
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
}

return $this->projectDir;
}

public function getRootDir() : string
{
return $this->getProjectDir();
}
}
35 changes: 34 additions & 1 deletion Tests/RegistryTest.php
Expand Up @@ -4,8 +4,12 @@

use Closure;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\ORM\EntityManagerInterface;
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomClassRepoEntity;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository;
use ProxyManager\Proxy\LazyLoadingInterface;
use ProxyManager\Proxy\ProxyInterface;
use stdClass;

class RegistryTest extends TestCase
Expand Down Expand Up @@ -132,7 +136,7 @@ public function testReset() : void
$noProxyManager->expects($this->once())
->method('clear');

$proxyManager = $this->getMockBuilder(LazyLoadingInterface::class)->getMock();
$proxyManager = $this->getMockBuilder([LazyLoadingInterface::class, EntityManagerInterface::class])->getMock();
$proxyManager->expects($this->once())
->method('setProxyInitializer')
->with($this->isInstanceOf(Closure::class));
Expand All @@ -157,4 +161,33 @@ public function testReset() : void
$registry = new Registry($container, [], $entityManagers, 'default', 'default');
$registry->reset();
}

public function testIdentityMapsStayConsistentAfterReset()
{
$kernel = new TestKernel();
$kernel->boot();

$container = $kernel->getContainer();
$registry = $container->get('doctrine');
$entityManager = $container->get('doctrine.orm.default_entity_manager');
$repository = $entityManager->getRepository(TestCustomClassRepoEntity::class);

$this->assertInstanceOf(ProxyInterface::class, $entityManager);
assert($entityManager instanceof EntityManagerInterface);
assert($registry instanceof Registry);
assert($repository instanceof TestCustomClassRepoRepository);

$entity = new TestCustomClassRepoEntity();
$repository->getEntityManager()->persist($entity);

$this->assertTrue($entityManager->getUnitOfWork()->isEntityScheduled($entity));
$this->assertTrue($repository->getEntityManager()->getUnitOfWork()->isEntityScheduled($entity));

$registry->reset();

$this->assertFalse($entityManager->getUnitOfWork()->isEntityScheduled($entity));
$this->assertFalse($repository->getEntityManager()->getUnitOfWork()->isEntityScheduled($entity));

$entityManager->flush();
}
}
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -44,11 +44,12 @@
"phpunit/phpunit": "^7.5",
"symfony/phpunit-bridge": "^4.2",
"symfony/property-info": "^4.3.3|^5.0",
"symfony/proxy-manager-bridge": "^3.4|^4.3.3|^5.0",
"symfony/twig-bridge": "^3.4.30|^4.3.3|^5.0",
"symfony/validator": "^3.4.30|^4.3.3|^5.0",
"symfony/web-profiler-bundle": "^3.4.30|^4.3.3|^5.0",
"symfony/yaml": "^3.4.30|^4.3.3|^5.0",
"twig/twig": "^1.34|^2.12",
"symfony/web-profiler-bundle": "^3.4.30|^4.3.3|^5.0"
"twig/twig": "^1.34|^2.12"
},
"config": {
"sort-packages": true
Expand Down

0 comments on commit 6926771

Please sign in to comment.