Skip to content

Commit

Permalink
Merge pull request #857 from Nek-/patch-1
Browse files Browse the repository at this point in the history
Support user defined repositories
  • Loading branch information
kimhemsoe committed Oct 16, 2018
2 parents 34926a4 + 9d3a0a6 commit 8f6c3fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Repository/ContainerRepositoryFactory.php
Expand Up @@ -50,8 +50,8 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName
if ($this->container && $this->container->has($customRepositoryName)) {
$repository = $this->container->get($customRepositoryName);

if (! $repository instanceof EntityRepository) {
throw new \RuntimeException(sprintf('The service "%s" must extend EntityRepository (or a base class, like ServiceEntityRepository).', $repositoryServiceId));
if (! $repository instanceof ObjectRepository) {
throw new \RuntimeException(sprintf('The service "%s" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).', $repositoryServiceId));
}

return $repository;
Expand Down
24 changes: 22 additions & 2 deletions Tests/Repository/ContainerRepositoryFactoryTest.php
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
Expand Down Expand Up @@ -63,9 +64,9 @@ public function testCustomRepositoryIsReturned()

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The service "my_repo" must extend EntityRepository (or a base class, like ServiceEntityRepository).
* @expectedExceptionMessage The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).
*/
public function testServiceRepositoriesMustExtendEntityRepository()
public function testServiceRepositoriesMustExtendObjectRepository()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
Expand All @@ -81,6 +82,25 @@ public function testServiceRepositoriesMustExtendEntityRepository()
$factory->getRepository($em, 'Foo\CoolEntity');
}

public function testServiceRepositoriesCanNotExtendsEntityRepository()
{
if (! interface_exists(ContainerInterface::class)) {
$this->markTestSkipped('Symfony 3.3 is needed for this feature.');
}

$repo = $this->getMockBuilder(ObjectRepository::class)->getMock();

$container = $this->createContainer(['my_repo' => $repo]);

$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);

$factory = new ContainerRepositoryFactory($container);
$factory->getRepository($em, 'Foo\CoolEntity');
$actualRepo = $factory->getRepository($em, 'Foo\CoolEntity');
$this->assertSame($repo, $actualRepo);
}


/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The "Doctrine\Bundle\DoctrineBundle\Tests\Repository\StubServiceRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service".
Expand Down

0 comments on commit 8f6c3fc

Please sign in to comment.