Skip to content

Commit

Permalink
Support user defined repositories
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nek- committed Oct 12, 2018
1 parent a03386c commit c6a0702
Show file tree
Hide file tree
Showing 2 changed files with 33 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
33 changes: 31 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 = 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".
Expand Down Expand Up @@ -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() {}
}

0 comments on commit c6a0702

Please sign in to comment.