8000 Added support \IteratorAggregate for UniqueEntityValidator · symfony/symfony@e0847b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0847b0

Browse files
committed
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 422430e commit e0847b0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 38 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;
@@ -137,6 +138,16 @@ private function createSchema(ObjectManager $em)
137138
));
138139
}
139140

141+
public function resultTypesProvider()
142+
{
143+
$entity = new SingleIntIdEntity(1, 'foo');
144+
return array(
145+
array($entity, array($entity)),
146+
array($entity, new \ArrayIterator(array($entity))),
147+
array($entity, new ArrayCollection(array($entity))),
148+
);
149+
}
150+
140151
/**
141152
* This is a functional test as there is a large integration necessary to get the validator working.
142153
*/
@@ -336,6 +347,33 @@ public function testValidateUniquenessWithUnrewoundArray()
336347
$this->assertNoViolation();
337348
}
338349

350+
/**
351+
* @dataProvider resultTypesProvider
352+
*/
353+
public function testValidateResultTypes($entity1, $result)
354+
{
355+
$constraint = new UniqueEntity(array(
356+
'message' => 'myMessage',
357+
'fields' => array('name'),
358+
'em' => self::EM_NAME,
359+
'repositoryMethod' => 'findByCustom',
360+
));
361+
362+
$repository = $this->createRepositoryMock();
363+
$repository->expects($this->once())
364+
->method('findByCustom')
365+
->will($this->returnValue($result))
366+
;
367+
$this->em = $this->createEntityManagerMock($repository);
368+
$this->registry = $this->createRegistryMock($this->em);
369+
$this->validator = $this->createValidator();
370+
$this->validator->initialize($this->context);
371+
372+
$this->validator->validate($entity1, $constraint);
373+
374+
$this->assertNoViolation();
375+
}
376+
339377
public function testAssociatedEntity()
340378
{
341379
$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
@@ -114,6 +114,10 @@ public function validate($entity, Constraint $constraint)
114114
$repository = $em->getRepository(get_class($entity));
115115
$result = $repository->{$constraint->repositoryMethod}($criteria);
116116

117+
if ($result instanceof \IteratorAggregate) {
118+
$result = $result->getIterator();
119+
}
120+
117121
/* If the result is a MongoCursor, it must be advanced to the first
118122
* element. Rewinding should have no ill effect if $result is another
119123
* iterator implementation.

0 commit comments

Comments
 (0)
2A3B
0