Skip to content

Commit

Permalink
bug #9535 No Entity Manager defined exception (armetiz)
Browse files Browse the repository at this point in the history
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #9535).

Discussion
----------

No Entity Manager defined exception

This PR is linked to this issue : #9534

Commits
-------

719ccb7 No Entity Manager defined exception
  • Loading branch information
fabpot committed Nov 22, 2013
2 parents 0e57c7b + af98688 commit ccf0575
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Expand Up @@ -367,4 +367,50 @@ public function testAssociatedCompositeEntity()
);
$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);
}
}
Expand Up @@ -62,8 +62,16 @@ public function validate($entity, Constraint $constraint)

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();
Expand Down

0 comments on commit ccf0575

Please sign in to comment.