-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…anagers-per-repository-factory #1159 - multiple entity managers per repository factory should be supported
- Loading branch information
Showing
3 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
* @author Guilherme Blanco <[email protected]> | ||
* @since 2.4 | ||
*/ | ||
class DefaultRepositoryFactory implements RepositoryFactory | ||
final class DefaultRepositoryFactory implements RepositoryFactory | ||
{ | ||
/** | ||
* The list of EntityRepository instances. | ||
|
@@ -41,13 +41,13 @@ class DefaultRepositoryFactory implements RepositoryFactory | |
*/ | ||
public function getRepository(EntityManagerInterface $entityManager, $entityName) | ||
{ | ||
$className = $entityManager->getClassMetadata($entityName)->getName(); | ||
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager); | ||
|
||
if (isset($this->repositoryList[$className])) { | ||
return $this->repositoryList[$className]; | ||
if (isset($this->repositoryList[$repositoryHash])) { | ||
return $this->repositoryList[$repositoryHash]; | ||
} | ||
|
||
return $this->repositoryList[$className] = $this->createRepository($entityManager, $entityName); | ||
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName); | ||
} | ||
|
||
/** | ||
|
@@ -58,7 +58,7 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName | |
* | ||
* @return \Doctrine\Common\Persistence\ObjectRepository | ||
*/ | ||
protected function createRepository(EntityManagerInterface $entityManager, $entityName) | ||
private function createRepository(EntityManagerInterface $entityManager, $entityName) | ||
{ | ||
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */ | ||
$metadata = $entityManager->getClassMetadata($entityName); | ||
|
151 changes: 151 additions & 0 deletions
151
tests/Doctrine/Tests/ORM/Repository/DefaultRepositoryFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Repository; | ||
|
||
use Doctrine\ORM\Repository\DefaultRepositoryFactory; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Tests for {@see \Doctrine\ORM\Repository\DefaultRepositoryFactory} | ||
* | ||
* @covers \Doctrine\ORM\Repository\DefaultRepositoryFactory | ||
*/ | ||
class DefaultRepositoryFactoryTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Doctrine\ORM\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* @var \Doctrine\ORM\Configuration|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $configuration; | ||
|
||
/** | ||
* @var DefaultRepositoryFactory | ||
*/ | ||
private $repositoryFactory; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->configuration = $this->getMock('Doctrine\\ORM\\Configuration'); | ||
$this->entityManager = $this->createEntityManager(); | ||
$this->repositoryFactory = new DefaultRepositoryFactory(); | ||
|
||
$this | ||
->configuration | ||
->expects($this->any()) | ||
->method('getDefaultRepositoryClassName') | ||
->will($this->returnValue('Doctrine\\Tests\\Models\\DDC869\\DDC869PaymentRepository')); | ||
} | ||
|
||
public function testCreatesRepositoryFromDefaultRepositoryClass() | ||
{ | ||
$this | ||
->entityManager | ||
->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnCallback(array($this, 'buildClassMetadata'))); | ||
|
||
$this->assertInstanceOf( | ||
'Doctrine\\Tests\\Models\\DDC869\\DDC869PaymentRepository', | ||
$this->repositoryFactory->getRepository($this->entityManager, __CLASS__) | ||
); | ||
} | ||
|
||
public function testCreatedRepositoriesAreCached() | ||
{ | ||
$this | ||
->entityManager | ||
->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnCallback(array($this, 'buildClassMetadata'))); | ||
|
||
$this->assertSame( | ||
$this->repositoryFactory->getRepository($this->entityManager, __CLASS__), | ||
$this->repositoryFactory->getRepository($this->entityManager, __CLASS__) | ||
); | ||
} | ||
|
||
public function testCreatesRepositoryFromCustomClassMetadata() | ||
{ | ||
$customMetadata = $this->buildClassMetadata(__DIR__); | ||
|
||
$customMetadata->customRepositoryClassName = 'Doctrine\\Tests\\Models\\DDC753\\DDC753DefaultRepository'; | ||
|
||
$this | ||
->entityManager | ||
->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnValue($customMetadata)); | ||
|
||
$this->assertInstanceOf( | ||
'Doctrine\\Tests\\Models\\DDC753\\DDC753DefaultRepository', | ||
$this->repositoryFactory->getRepository($this->entityManager, __CLASS__) | ||
); | ||
} | ||
|
||
public function testCachesDistinctRepositoriesPerDistinctEntityManager() | ||
{ | ||
$em1 = $this->createEntityManager(); | ||
$em2 = $this->createEntityManager(); | ||
|
||
$em1 | ||
->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnCallback(array($this, 'buildClassMetadata'))); | ||
$em2 | ||
->expects($this->any()) | ||
->method('getClassMetadata') | ||
->will($this->returnCallback(array($this, 'buildClassMetadata'))); | ||
|
||
$repo1 = $this->repositoryFactory->getRepository($em1, __CLASS__); | ||
$repo2 = $this->repositoryFactory->getRepository($em2, __CLASS__); | ||
|
||
$this->assertSame($repo1, $this->repositoryFactory->getRepository($em1, __CLASS__)); | ||
$this->assertSame($repo2, $this->repositoryFactory->getRepository($em2, __CLASS__)); | ||
|
||
$this->assertNotSame($repo1, $repo2); | ||
} | ||
|
||
/** | ||
* @private | ||
* | ||
* @param string $className | ||
* | ||
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\ORM\Mapping\ClassMetadata | ||
*/ | ||
public function buildClassMetadata($className) | ||
{ | ||
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject */ | ||
$metadata = $this | ||
->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$metadata->expects($this->any())->method('getName')->will($this->returnValue($className)); | ||
|
||
$metadata->customRepositoryClassName = null; | ||
|
||
return $metadata; | ||
} | ||
|
||
/** | ||
* @return \Doctrine\ORM\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function createEntityManager() | ||
{ | ||
$entityManager = $this->getMock('Doctrine\\ORM\\EntityManagerInterface'); | ||
|
||
$entityManager | ||
->expects($this->any()) | ||
->method('getConfiguration') | ||
->will($this->returnValue($this->configuration)); | ||
|
||
return $entityManager; | ||
} | ||
} |