8000 merged branch docteurklein/unique-entity-validator-with-custom-reposi… · Lumbendil/symfony@9f157a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f157a1

Browse files
committed
merged branch docteurklein/unique-entity-validator-with-custom-repository-method (PR symfony#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.
2 parents 4bde2aa + 4eb54a0 commit 9f157a1

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ CHANGELOG
99
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
1010
* DoctrineType now caches its choice lists in order to improve performance
1111
* DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set
12+
* UniqueEntity validation constraint now accepts a "repositoryMethod" option that will be used to check for uniqueness instead of the default "findBy"

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,50 @@ protected function createRegistryMock($entityManagerName, $em)
4747
return $registry;
4848
}
4949

50+
protected function createRepositoryMock()
51+
{
52+
$repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')
53+
->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
54+
->getMock()
55+
;
56+
57+
return $repository;
58+
}
59+
60+
protected function createEntityManagerMock($repositoryMock)
61+
{
62+
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
63+
->getMock()
64+
;
65+
$em->expects($this->any())
66+
->method('getRepository')
67+
->will($this->returnValue($repositoryMock))
68+
;
69+
70+
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
71+
$classMetadata
72+
->expects($this->any())
73+
->method('hasField')
74+
->will($this->returnValue(true))
75+
;
76+
$refl = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionProperty')
77+
->disableOriginalConstructor()
78+
->getMock()
79+
;
80+
$refl
81+
->expects($this->any())
82+
->method('getValue')
83+
->will($this->returnValue(true))
84+
;
85+
$classMetadata->reflFields = array('name' => $refl);
86+
$em->expects($this->any())
87+
->method('getClassMetadata')
88+
->will($this->returnValue($classMetadata))
89+
;
90+
91+
return $em;
92+
}
93+
5094
protected function createMetadataFactoryMock($metadata)
5195
{
5296
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
@@ -69,7 +113,7 @@ protected function createValidatorFactory($uniqueValidator)
69113
return $validatorFactory;
70114
}
71115

72-
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null)
116+
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy')
73117
{
74118
if (!$validateClass) {
75119
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
@@ -83,7 +127,13 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
83127
$uniqueValidator = new UniqueEntityValidator($registry);
84128

85129
$metadata = new ClassMetadata($validateClass);
86-
$metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName, 'errorPath' => $errorPath)));
130+
$constraint = new UniqueEntity(array(
131+
'fields' => $uniqueFields,
132+
'em' => $entityManagerName,
133+
'errorPath' => $errorPath,
134+
'repositoryMethod' => $repositoryMethod
135+
));
136+
$metadata->addConstraint($constraint);
87137

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

247+
public function testValidateUniquenessUsingCustomRepositoryMethod()
248+
{
249+
$entityManagerName = 'foo';
250+
$repository = $this->createRepositoryMock();
251+
$repository->expects($this->once())
252+
->method('findByCustom')
253+
->will($this->returnValue(array()))
254+
;
255+
$em = $this->createEntityManagerMock($repository);
256+
$validator = $this->createValidator($entityManagerName, $em, null, array(), null, 'findByCustom');
257+
258+
$entity1 = new SingleIdentEntity(1, 'foo');
259+
260+
$violationsList = $validator->validate($entity1);
261+
$this->assertEquals(0, $violationsList->count(), 'Violation is using custom repository method.');
262+
}
263+
197264
/**
198265
* @group GH-1635
199266
*/

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class UniqueEntity extends Constraint
2424
public $message = 'This value is already used.';
2525
public $service = 'doctrine.orm.validator.unique';
2626
public $em = null;
27+
public $repositoryMethod = 'findBy';
2728
public $fields = array();
2829
public $errorPath = null;
2930

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function validate($entity, Constraint $constraint)
100100
}
101101

102102
$repository = $em->getRepository($className);
103-
$result = $repository->findBy($criteria);
103+
$result = $repository->{$constraint->repositoryMethod}($criteria);
104104

105105
/* If the result is a MongoCursor, it must be advanced to the first
106106
* element. Rewinding should have no ill effect if $result is another

0 commit comments

Comments
 (0)
0