Skip to content

Commit fc3e124

Browse files
Disparityfabpot
authored andcommitted
Added support \IteratorAggregate for UniqueEntityValidator
Expand the list of supported types of results returned from the repositories. Added processing of type \IteratorAggregate (and as a consequence doctrine Collection)
1 parent 0aff658 commit fc3e124

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Tests\Validator\Constraints;
1313

14+
use Doctrine\Common\Collections\ArrayCollection;
1415
use Doctrine\Common\Persistence\ManagerRegistry;
1516
use Doctrine\Common\Persistence\ObjectManager;
1617
use Doctrine\Common\Persistence\ObjectRepository;
@@ -330,6 +331,44 @@ public function testValidateUniquenessWithUnrewoundArray()
330331
$this->assertNoViolation();
331332
}
332333

334+
/**
335+
* @dataProvider resultTypesProvider
336+
*/
337+
public function testValidateResultTypes($entity1, $result)
338+
{
339+
$constraint = new UniqueEntity(array(
340+
'message' => 'myMessage',
341+
'fields' => array('name'),
342+
'em' => self::EM_NAME,
343+
'repositoryMethod' => 'findByCustom',
344+
));
345+
346+
$repository = $this->createRepositoryMock();
347+
$repository->expects($this->once())
348+
->method('findByCustom')
349+
->will($this->returnValue($result))
350+
;
351+
$this->em = $this->createEntityManagerMock($repository);
352+
$this->registry = $this->createRegistryMock($this->em);
353+
$this->validator = $this->createValidator();
354+
$this->validator->initialize($this->context);
355+
356+
$this->validator->validate($entity1, $constraint);
357+
358+
$this->assertNoViolation();
359+
}
360+
361+
public function resultTypesProvider()
362+
{
363+
$entity = new SingleIntIdEntity(1, 'foo');
364+
365+
return array(
366+
array($entity, array($entity)),
367+
array($entity, new \ArrayIterator(array($entity))),
368+
array($entity, new ArrayCollection(array($entity))),
369+
);
370+
}
371+
333372
public function testAssociatedEntity()
334373
{
335374
$constraint = new UniqueEntity(array(

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public function validate($entity, Constraint $constraint)
109109
$repository = $em->getRepository(get_class($entity));
110110
$result = $repository->{$constraint->repositoryMethod}($criteria);
111111

112+
if ($result instanceof \IteratorAggregate) {
113+
$result = $result->getIterator();
114+
}
115+
112116
/* If the result is a MongoCursor, it must be advanced to the first
113117
* element. Rewinding should have no ill effect if $result is another
114118
* iterator implementation.

0 commit comments

Comments
 (0)