diff --git a/Repository/ContainerRepositoryFactory.php b/Repository/ContainerRepositoryFactory.php index 02ef8b5d9..4230d111a 100644 --- a/Repository/ContainerRepositoryFactory.php +++ b/Repository/ContainerRepositoryFactory.php @@ -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; diff --git a/Tests/Repository/ContainerRepositoryFactoryTest.php b/Tests/Repository/ContainerRepositoryFactoryTest.php index a5543dcb8..b6d0aea36 100644 --- a/Tests/Repository/ContainerRepositoryFactoryTest.php +++ b/Tests/Repository/ContainerRepositoryFactoryTest.php @@ -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; @@ -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.'); @@ -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 = new StubCustomRepository(); + + $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->assertInstanceOf(StubCustomRepository::class, $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". @@ -169,3 +189,12 @@ class StubRepository extends EntityRepository class StubServiceRepository extends EntityRepository implements ServiceEntityRepositoryInterface { } + +class StubCustomRepository implements ObjectRepository +{ + public function find($id) {} + public function findAll() {} + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) {} + public function findOneBy(array $criteria) {} + public function getClassName() {} +}