From d1e7960f9918779b169cfa3688cebebfb28014ab Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 16 Jan 2015 22:12:42 +0100 Subject: [PATCH] #1172 - tests to verify that proxies keep distinct entity manager instances even across merging operations --- .../MergeUninitializedProxyTest.php | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/MergeUninitializedProxyTest.php b/tests/Doctrine/Tests/ORM/Functional/MergeUninitializedProxyTest.php index eb033ca5a..d86c28fa3 100644 --- a/tests/Doctrine/Tests/ORM/Functional/MergeUninitializedProxyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/MergeUninitializedProxyTest.php @@ -4,8 +4,14 @@ namespace Doctrine\Tests\ORM\Functional; +use Doctrine\DBAL\Logging\DebugStack; +use Doctrine\DBAL\Logging\SQLLogger; +use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\Proxy\Proxy; +use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Tools\ToolsException; +use Doctrine\Tests\TestUtil; class MergeUninitializedProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase { @@ -62,6 +68,57 @@ class MergeUninitializedProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertFalse($managed->__isInitialized()); } + public function testMergingProxyFromDifferentEntityManagerDoesNotReplaceInitializer() + { + $em1 = $this->createEntityManager($logger1 = new DebugStack()); + $em2 = $this->createEntityManager($logger2 = new DebugStack()); + + $file1 = new MUPFile(); + + $em1->persist($file1); + $em1->flush(); + $em1->clear(); + + $queryCount1 = count($logger1->queries); + $queryCount2 = count($logger1->queries); + + $proxy1 = $em1->getReference(MUPFile::CLASSNAME, $file1->fileId); + $proxy2 = $em2->getReference(MUPFile::CLASSNAME, $file1->fileId); + $merged2 = $em2->merge($proxy1); + + $this->assertNotSame($proxy1, $merged2); + $this->assertSame($proxy2, $merged2); + + $this->assertFalse($proxy1->__isInitialized()); + $this->assertFalse($proxy2->__isInitialized()); + + $proxy1->__load(); + + $this->assertCount( + $queryCount1 + 1, + $logger1->queries, + 'Loading the first proxy was done through the first entity manager' + ); + $this->assertCount( + $queryCount2, + $logger2->queries, + 'No queries were executed on the second entity manager, as it is unrelated with the first proxy' + ); + + $proxy2->__load(); + + $this->assertCount( + $queryCount1 + 1, + $logger1->queries, + 'Loading the second proxy does not affect the first entity manager' + ); + $this->assertCount( + $queryCount2 + 1, + $logger2->queries, + 'Loading of the second proxy instance was done through the second entity manager' + ); + } + public function testMergeDetachedIntoEntity() { $file = new MUPFile; @@ -163,6 +220,37 @@ class MergeUninitializedProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($picture2->file, $picture->file, "Detached proxy was not merged into managed proxy"); } + /** + * @param SQLLogger $logger + * + * @return EntityManager + */ + private function createEntityManager(SQLLogger $logger) + { + $config = new Configuration(); + + $config->setProxyDir(realpath(__DIR__ . '/../../Proxies/../..')); + $config->setProxyNamespace('Doctrine\Tests\Proxies'); + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver( + array(realpath(__DIR__ . '/../../Models/Cache')), + true + )); + + $connection = TestUtil::getConnection(); + + $connection->getConfiguration()->setSQLLogger($logger); + + $entityManager = EntityManager::create($connection, $config); + + try { + (new SchemaTool($entityManager)) + ->createSchema([$this->_em->getClassMetadata(MUPFile::CLASSNAME)]); + } catch (ToolsException $ignored) { + // tables were already created + } + + return $entityManager; + } } /**