Skip to content

Commit a9a1a54

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: Typo fix [2.3] Static Code Analysis for Components Added support \IteratorAggregate for UniqueEntityValidator Fix #17306 Paths with % in it are note allowed (like urlencoded) Added sort order SORT_STRING for params in UriSigner Remove normalizer cache in Serializer class
2 parents c695ac6 + fc3e124 commit a9a1a54

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;
@@ -336,6 +337,44 @@ public function testValidateUniquenessWithUnrewoundArray()
336337
$this->assertNoViolation();
337338
}
338339

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

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)