8000 bug #17078 [Bridge] [Doctrine] [Validator] Added support \IteratorAgg… · symfony/symfony@aa9801e · GitHub
[go: up one dir, main page]

Skip to content

Commit aa9801e

Browse files
committed
bug #17078 [Bridge] [Doctrine] [Validator] Added support \IteratorAggregate for UniqueEntityValidator (Disparity)
This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes #17078). Discussion ---------- [Bridge] [Doctrine] [Validator] Added support \IteratorAggregate for UniqueEntityValidator | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Expand the list of supported types of results returned from the repositories. Added processing of type \IteratorAggregate (and as a consequence doctrine Collection) Commits ------- 6ebd179 Added support \IteratorAggregate for UniqueEntityValidator
2 parents 5d63c55 + 6ebd179 commit aa9801e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Symfony/Bridge/Doctrine/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(

src/Symfony/Bridge/Doctrine/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)
0