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

Better error message when missing entity mapping #825

Merged
merged 1 commit into from Oct 30, 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
8 changes: 8 additions & 0 deletions Repository/ServiceEntityRepository.php
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityRepository;
use LogicException;

/**
* Optional EntityRepository base class with a simplified constructor (for autowiring).
Expand All @@ -28,6 +29,13 @@ public function __construct(ManagerRegistry $registry, $entityClass)
{
$manager = $registry->getManagerForClass($entityClass);

if (null === $manager) {
throw new LogicException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
$entityClass
));
}

parent::__construct($manager, $manager->getClassMetadata($entityClass));
}
}
20 changes: 20 additions & 0 deletions Tests/Repository/ServiceEntityRepositoryTest.php
@@ -0,0 +1,20 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;

class ServiceEntityRepositoryTest extends TestCase
{
/**
* @expectedException \LogicException
* @expectedExceptionMessage Could not find the entity manager for class "Doctrine\Bundle\DoctrineBundle\Tests\Repository\TestEntity". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.
*/
public function testConstructorThrowsExceptionWhenNoManagerFound()
{
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
new ServiceEntityRepository($registry, TestEntity::class);
}
}