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)
This commit is contained in:
parent
5d63c554e8
commit
6ebd1792d8
@ -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(
|
||||
|
@ -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.
|
||||
|
Reference in New Issue
Block a user