Skip to content

Commit

Permalink
Merge pull request #1160 from Ocramius/hotfix/#1159-multiple-entity-m…
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
deeky666 committed Oct 19, 2014
2 parents 0f26c62 + f28fa2d commit 06b5c84
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 6 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ As of PHP 5.6, instantiation of new entities is deferred to the
[`doctrine/instantiator`](https://github.com/doctrine/instantiator) library, which will avoid calling `__clone`
or any public API on instantiated objects.

## BC BREAK: `Doctrine\ORM\Repository\DefaultRepositoryFactory` is now `final`

Please implement the `Doctrine\ORM\Repository\RepositoryFactory` interface instead of extending
the `Doctrine\ORM\Repository\DefaultRepositoryFactory`.

# Upgrade to 2.4

## BC BREAK: Compatibility Bugfix in PersistentCollection#matching()
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand Down
151 changes: 151 additions & 0 deletions tests/Doctrine/Tests/ORM/Repository/DefaultRepositoryFactoryTest.php
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;
}
}

0 comments on commit 06b5c84

Please sign in to comment.