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

Skip to content

Commit 93d00be

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 93d00be

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

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

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