From 76cf83fb4d702392a6e3c733a76c7a2444961077 Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Wed, 26 Sep 2018 16:41:28 +0200 Subject: [PATCH 1/2] Support user defined repositories A verification was done with EntityRepository inheritance. This commit drop it in favor of a check on interface ObjectRepository which allow user to define its own complete repository. --- Repository/ContainerRepositoryFactory.php | 4 +-- .../ContainerRepositoryFactoryTest.php | 33 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Repository/ContainerRepositoryFactory.php b/Repository/ContainerRepositoryFactory.php index 02ef8b5d9..992a2b6c3 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() {} +} From 9d3a0a6733ecf1c7bed34a783e79d92baf207292 Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Fri, 12 Oct 2018 10:30:09 +0200 Subject: [PATCH 2/2] Fix compatibility with older versions Older versions of PHP does not support the current implementation so I changed it. Also in order to be able to run the test suite with old versions of Doctrine, this change was also required (because the interface differ). --- Tests/Repository/ContainerRepositoryFactoryTest.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/Tests/Repository/ContainerRepositoryFactoryTest.php b/Tests/Repository/ContainerRepositoryFactoryTest.php index b6d0aea36..2ff96dff5 100644 --- a/Tests/Repository/ContainerRepositoryFactoryTest.php +++ b/Tests/Repository/ContainerRepositoryFactoryTest.php @@ -88,7 +88,7 @@ public function testServiceRepositoriesCanNotExtendsEntityRepository() $this->markTestSkipped('Symfony 3.3 is needed for this feature.'); } - $repo = new StubCustomRepository(); + $repo = $this->getMockBuilder(ObjectRepository::class)->getMock(); $container = $this->createContainer(['my_repo' => $repo]); @@ -97,7 +97,7 @@ public function testServiceRepositoriesCanNotExtendsEntityRepository() $factory = new ContainerRepositoryFactory($container); $factory->getRepository($em, 'Foo\CoolEntity'); $actualRepo = $factory->getRepository($em, 'Foo\CoolEntity'); - $this->assertInstanceOf(StubCustomRepository::class, $actualRepo); + $this->assertSame($repo, $actualRepo); } @@ -189,12 +189,3 @@ 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() {} -}