Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user defined repositories #857

Merged
merged 2 commits into from Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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).
Nek- marked this conversation as resolved.
Show resolved Hide resolved
* @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