No Entity Manager defined exception
This commit is contained in:
parent
0e57c7b757
commit
af98688f94
@ -367,4 +367,50 @@ class UniqueValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
$violationsList = $validator->validate($associated);
|
||||
}
|
||||
|
||||
public function testDedicatedEntityManagerNullObject()
|
||||
{
|
||||
$uniqueFields = array('name');
|
||||
$entityManagerName = 'foo';
|
||||
|
||||
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
|
||||
|
||||
$constraint = new UniqueEntity(array(
|
||||
'fields' => $uniqueFields,
|
||||
'em' => $entityManagerName,
|
||||
));
|
||||
|
||||
$uniqueValidator = new UniqueEntityValidator($registry);
|
||||
|
||||
$entity = new SingleIntIdEntity(1, null);
|
||||
|
||||
$this->setExpectedException(
|
||||
'Symfony\Component\Validator\Exception\ConstraintDefinitionException',
|
||||
'Object manager "foo" does not exist.'
|
||||
);
|
||||
|
||||
$uniqueValidator->validate($entity, $constraint);
|
||||
}
|
||||
|
||||
public function testEntityManagerNullObject()
|
||||
{
|
||||
$uniqueFields = array('name');
|
||||
|
||||
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
|
||||
|
||||
$constraint = new UniqueEntity(array(
|
||||
'fields' => $uniqueFields,
|
||||
));
|
||||
|
||||
$uniqueValidator = new UniqueEntityValidator($registry);
|
||||
|
||||
$entity = new SingleIntIdEntity(1, null);
|
||||
|
||||
$this->setExpectedException(
|
||||
'Symfony\Component\Validator\Exception\ConstraintDefinitionException',
|
||||
'Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"'
|
||||
);
|
||||
|
||||
$uniqueValidator->validate($entity, $constraint);
|
||||
}
|
||||
}
|
||||
|
@ -62,8 +62,16 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||
|
||||
if ($constraint->em) {
|
||||
$em = $this->registry->getManager($constraint->em);
|
||||
|
||||
if (!$em) {
|
||||
throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em));
|
||||
}
|
||||
} else {
|
||||
$em = $this->registry->getManagerForClass(get_class($entity));
|
||||
|
||||
if (!$em) {
|
||||
throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_class($entity)));
|
||||
}
|
||||
}
|
||||
|
||||
$className = $this->context->getClassName();
|
||||
|
Reference in New Issue
Block a user