Navigation Menu

Skip to content

Commit

Permalink
Add a customRepository option to the uniqueEntity validator
Browse files Browse the repository at this point in the history
  • Loading branch information
docteurklein committed Jul 19, 2012
1 parent 42f1f12 commit 2a6c222
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
Expand Up @@ -47,6 +47,42 @@ protected function createRegistryMock($entityManagerName, $em)
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')

This comment has been minimized.

Copy link
@webmozart

webmozart Jul 25, 2012

Contributor

StaticReflectionProperty exists only in Doctrine Common >= 2.3. This test fails on my machine with Doctrine Common 2.2.2.

This comment has been minimized.

Copy link
@webmozart

webmozart Jul 25, 2012

Contributor

Solution for now: #5052

This comment has been minimized.

Copy link
@docteurklein

docteurklein Jul 26, 2012

Author Contributor

oh thanks!

->disableOriginalConstructor()
->getMock()
;
$classMetadata->reflFields = array('name' => $refl);
$em->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue($classMetadata))
;

return $em;
}

protected function createMetadataFactoryMock($metadata)
{
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
Expand All @@ -69,7 +105,7 @@ protected function createValidatorFactory($uniqueValidator)
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) {
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
Expand All @@ -83,7 +119,12 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
$uniqueValidator = new UniqueEntityValidator($registry);

$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);
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
Expand Down Expand Up @@ -194,6 +235,23 @@ public function testValidateUniquenessAfterConsideringMultipleQueryResults()
$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
*/
Expand Down
Expand Up @@ -24,6 +24,7 @@ class UniqueEntity extends Constraint
public $message = 'This value is already used.';
public $service = 'doctrine.orm.validator.unique';
public $em = null;
public $repositoryMethod = 'findBy';
public $fields = array();
public $errorPath = null;

Expand Down
Expand Up @@ -100,7 +100,7 @@ public function validate($entity, Constraint $constraint)
}

$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
* element. Rewinding should have no ill effect if $result is another
Expand Down

0 comments on commit 2a6c222

Please sign in to comment.