Add a customRepository option to the uniqueEntity validator
This commit is contained in:
parent
42f1f12ec5
commit
2a6c222c51
|
@ -47,6 +47,42 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
|
||||||
return $registry;
|
return $registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function createRepositoryMock()
|
||||||
|
{
|
||||||
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createEntityManagerMock($repositoryMock)
|
||||||
|
{
|
||||||
|
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
$em->expects($this->any())
|
||||||
|
->method('getRepository')
|
||||||
|
->will($this->returnValue($repositoryMock))
|
||||||
|
;
|
||||||
|
|
||||||
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
|
||||||
|
$classMetadata
|
||||||
|
->expects($this->any())
|
||||||
|
->method('hasField')
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
$refl = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionProperty')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
$classMetadata->reflFields = array('name' => $refl);
|
||||||
|
$em->expects($this->any())
|
||||||
|
->method('getClassMetadata')
|
||||||
|
->will($this->returnValue($classMetadata))
|
||||||
|
;
|
||||||
|
|
||||||
|
return $em;
|
||||||
|
}
|
||||||
|
|
||||||
protected function createMetadataFactoryMock($metadata)
|
protected function createMetadataFactoryMock($metadata)
|
||||||
{
|
{
|
||||||
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
|
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
|
||||||
|
@ -69,7 +105,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
|
||||||
return $validatorFactory;
|
return $validatorFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null)
|
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy')
|
||||||
{
|
{
|
||||||
if (!$validateClass) {
|
if (!$validateClass) {
|
||||||
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
|
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
|
||||||
|
@ -83,7 +119,12 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
|
||||||
$uniqueValidator = new UniqueEntityValidator($registry);
|
$uniqueValidator = new UniqueEntityValidator($registry);
|
||||||
|
|
||||||
$metadata = new ClassMetadata($validateClass);
|
$metadata = new ClassMetadata($validateClass);
|
||||||
$metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName, 'errorPath' => $errorPath)));
|
$metadata->addConstraint(new UniqueEntity(array(
|
||||||
|
'fields' => $uniqueFields,
|
||||||
|
'em' => $entityManagerName,
|
||||||
|
'errorPath' => $errorPath,
|
||||||
|
'repositoryMethod' => $repositoryMethod
|
||||||
|
)));
|
||||||
|
|
||||||
$metadataFactory = $this->createMetadataFactoryMock($metadata);
|
$metadataFactory = $this->createMetadataFactoryMock($metadata);
|
||||||
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
|
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
|
||||||
|
@ -194,6 +235,23 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
|
||||||
$this->assertEquals(1, $violationsList->count(), 'Violation found on entity with conflicting entity existing in the database.');
|
$this->assertEquals(1, $violationsList->count(), 'Violation found on entity with conflicting entity existing in the database.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidateUniquenessUsingCustiomRepositoryMethod()
|
||||||
|
{
|
||||||
|
$entityManagerName = 'foo';
|
||||||
|
$repository = $this->createRepositoryMock();
|
||||||
|
$repository->expects($this->any())
|
||||||
|
->method('findByCustom')
|
||||||
|
->will($this->returnValue(array()))
|
||||||
|
;
|
||||||
|
$em = $this->createEntityManagerMock($repository);
|
||||||
|
$validator = $this->createValidator($entityManagerName, $em, null, array(), null, 'findByCustom');
|
||||||
|
|
||||||
|
$entity1 = new SingleIdentEntity(1, 'foo');
|
||||||
|
|
||||||
|
$violationsList = $validator->validate($entity1);
|
||||||
|
$this->assertEquals(0, $violationsList->count(), 'Violation is using custom repository method.');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group GH-1635
|
* @group GH-1635
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -24,6 +24,7 @@ class UniqueEntity extends Constraint
|
||||||
public $message = 'This value is already used.';
|
public $message = 'This value is already used.';
|
||||||
public $service = 'doctrine.orm.validator.unique';
|
public $service = 'doctrine.orm.validator.unique';
|
||||||
public $em = null;
|
public $em = null;
|
||||||
|
public $repositoryMethod = 'findBy';
|
||||||
public $fields = array();
|
public $fields = array();
|
||||||
public $errorPath = null;
|
public $errorPath = null;
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
$repository = $em->getRepository($className);
|
$repository = $em->getRepository($className);
|
||||||
$result = $repository->findBy($criteria);
|
$result = $repository->{$constraint->repositoryMethod}($criteria);
|
||||||
|
|
||||||
/* If the result is a MongoCursor, it must be advanced to the first
|
/* If the result is a MongoCursor, it must be advanced to the first
|
||||||
* element. Rewinding should have no ill effect if $result is another
|
* element. Rewinding should have no ill effect if $result is another
|
||||||
|
|
Reference in New Issue