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
This commit is contained in:
Fabien Potencier 2016-01-11 11:04:47 +01:00
commit aa9801e8a2
2 changed files with 43 additions and 0 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Bridge\Doctrine\Tests\Validator\Constraints;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
@ -330,6 +331,44 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
$this->assertNoViolation();
}
/**
* @dataProvider resultTypesProvider
*/
public function testValidateResultTypes($entity1, $result)
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name'),
'em' => self::EM_NAME,
'repositoryMethod' => 'findByCustom',
));
$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->method('findByCustom')
->will($this->returnValue($result))
;
$this->em = $this->createEntityManagerMock($repository);
$this->registry = $this->createRegistryMock($this->em);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);
$this->validator->validate($entity1, $constraint);
$this->assertNoViolation();
}
public function resultTypesProvider()
{
$entity = new SingleIntIdEntity(1, 'foo');
return array(
array($entity, array($entity)),
array($entity, new \ArrayIterator(array($entity))),
array($entity, new ArrayCollection(array($entity))),
);
}
public function testAssociatedEntity()
{
$constraint = new UniqueEntity(array(

View File

@ -109,6 +109,10 @@ class UniqueEntityValidator extends ConstraintValidator
$repository = $em->getRepository(get_class($entity));
$result = $repository->{$constraint->repositoryMethod}($criteria);
if ($result instanceof \IteratorAggregate) {
$result = $result->getIterator();
}
/* If the result is a MongoCursor, it must be advanced to the first
* element. Rewinding should have no ill effect if $result is another
* iterator implementation.