8000 bug #9535 No Entity Manager defined exception (armetiz) · symfony/symfony@ccf0575 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ccf0575

Browse files
committed
bug #9535 No Entity Manager defined exception (armetiz)
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
2 parents 0e57c7b + af98688 commit ccf0575

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,50 @@ public function testAssociatedCompositeEntity()
367367
);
368368
$violationsList = $validator->validate($associated);
369369
}
370+
371+
public function testDedicatedEntityManagerNullObject()
372+
{
373+
$uniqueFields = array('name');
374+
$entityManagerName = 'foo';
375+
376+
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
377+
378+
$constraint = new UniqueEntity(array(
379+
'fields' => $uniqueFields,
380+
'em' => $entityManagerName,
381+
));
382+
383+
$uniqueValidator = new UniqueEntityValidator($registry);
384+
385+
$entity = new SingleIntIdEntity(1, null);
386+
387+
$this->setExpectedException(
388+
'Symfony\Component\Validator\Exception\ConstraintDefinitionException',
389+
'Object manager "foo" does not exist.'
390+
);
391+
392+
$uniqueValidator->validate($entity, $constraint);
393+
}
394+
395+
public function testEntityManagerNullObject()
396+
{
397+
$uniqueFields = array('name');
398+
399+
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
400+
401+
$constraint = new UniqueEntity(array(
402+
'fields' => $uniqueFields,
403+
));
404+
405+
$uniqueValidator = new UniqueEntityValidator($registry);
406+
407+
$entity = new SingleIntIdEntity(1, null);
408+
409+
$this->setExpectedException(
410+
'Symfony\Component\Validator\Exception\ConstraintDefinitionException',
411+
'Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"'
412+
);
413+
414+
$uniqueValidator->validate($entity, $constraint);
415+
}
370416
}

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ public function validate($entity, Constraint $constraint)
6262

6363
if ($constraint->em) {
6464
$em = $this->registry->getManager($constraint->em);
65+
66+
if (!$em) {
67+
throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em));
68+
}
6569
} else {
6670
$em = $this->registry->getManagerForClass(get_class($entity));
71+
72+
if (!$em) {
73+
throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_class($entity)));
74+
}
6775
}
6876

6977
$className = $this->context->getClassName();

0 commit comments

Comments
 (0)
0