Skip to content

Commit

Permalink
merged branch docteurklein/unique-entity-validator-with-custom-reposi…
Browse files Browse the repository at this point in the history
…tory-method (PR #4979)

Commits
-------

4eb54a0 update CHANGELOG
db9ea09 [Doctrine] [Bridge] fix repositoryMethod test
2a6c222 Add a customRepository option to the uniqueEntity validator

Discussion
----------

[Doctrine] [Bridge] Add a "repositoryMethod" option to the uniqueEntity validator

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: ~
Todo: ~
License of the code: MIT
Documentation PR: ~

This allows to configure the repository method used to verify uniqueness of entity.
Before, it was always using `findBy`.

---------------------------------------------------------------------------

by fabpot at 2012-07-20T05:35:28Z

Can you add a note in the CHANGELOG?

---------------------------------------------------------------------------

by docteurklein at 2012-07-20T07:17:08Z

@fabpot done.
  • Loading branch information
fabpot committed Jul 20, 2012
2 parents 4bde2aa + 4eb54a0 commit 9f157a1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Expand Up @@ -9,3 +9,4 @@ CHANGELOG
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
* DoctrineType now caches its choice lists in order to improve performance
* DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set
* UniqueEntity validation constraint now accepts a "repositoryMethod" option that will be used to check for uniqueness instead of the default "findBy"
Expand Up @@ -47,6 +47,50 @@ protected function createRegistryMock($entityManagerName, $em)
return $registry;
}

protected function createRepositoryMock()
{
$repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')
->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
->getMock()
;

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()
;
$refl
->expects($this->any())
->method('getValue')
->will($this->returnValue(true))
;
$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 +113,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 +127,13 @@ 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)));
$constraint = new UniqueEntity(array(
'fields' => $uniqueFields,
'em' => $entityManagerName,
'errorPath' => $errorPath,
'repositoryMethod' => $repositoryMethod
));
$metadata->addConstraint($constraint);

$metadataFactory = $this->createMetadataFactoryMock($metadata);
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
Expand Down Expand Up @@ -194,6 +244,23 @@ public function testValidateUniquenessAfterConsideringMultipleQueryResults()
$this->assertEquals(1, $violationsList->count(), 'Violation found on entity with conflicting entity existing in the database.');
}

public function testValidateUniquenessUsingCustomRepositoryMethod()
{
$entityManagerName = 'foo';
$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->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 9f157a1

Please sign in to comment.