Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bd81b5

Browse files
committed
bug #40060 fix validator when we have false returned by the current element of the iterator (FabienSalles)
This PR was merged into the 4.4 branch. Discussion ---------- fix validator when we have false returned by the current element of the iterator | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40057 | License | MIT Commits ------- a9e9359 fix validator when we have a false current element
2 parents 90e8254 + a9e9359 commit 8bd81b5

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,4 +793,85 @@ public function testValidateUniquenessCause()
793793
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
794794
->assertRaised();
795795
}
796+
797+
/**
798+
* @dataProvider resultWithEmptyIterator
799+
*/
800+
public function testValidateUniquenessWithEmptyIterator($entity, $result)
801+
{
802+
$constraint = new UniqueEntity([
803+
'message' => 'myMessage',
804+
'fields' => ['name'],
805+
'em' => self::EM_NAME,
806+
'repositoryMethod' => 'findByCustom',
807+
]);
808+
809+
$repository = $this->createRepositoryMock();
810+
$repository->expects($this->once())
811+
->method('findByCustom')
812+
->willReturn($result)
813+
;
814+
$this->em = $this->createEntityManagerMock($repository);
815+
$this->registry = $this->createRegistryMock($this->em);
816+
$this->validator = $this->createValidator();
817+
$this->validator->initialize($this->context);
818+
819+
$this->validator->validate($entity, $constraint);
820+
821+
$this->assertNoViolation();
822+
}
823+
824+
public function resultWithEmptyIterator(): array
825+
{
826+
$entity = new SingleIntIdEntity(1, 'foo');
827+
828+
return [
829+
[$entity, new class() implements \Iterator {
830+
public function current()
831+
{
832+
return null;
833+
}
834+
835+
public function valid(): bool
836+
{
837+
return false;
838+
}
839+
840+
public function next()
841+
{
842+
}
843+
844+
public function key()
845+
{
846+
}
847+
848+
public function rewind()
849+
{
850+
}
851+
}],
852+
[$entity, new class() implements \Iterator {
853+
public function current()
854+
{
855+
return false;
856+
}
857+
858+
public function valid(): bool
859+
{
860+
return false;
861+
}
862+
863+
public function next()
864+
{
865+
}
866+
867+
public function key()
868+
{
869+
}
870+
871+
public function rewind()
872+
{
873+
}
874+
}],
875+
];
876+
}
796877
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ public function validate($entity, Constraint $constraint)
147147
if ($result instanceof \Countable && 1 < \count($result)) {
148148
$result = [$result->current(), $result->current()];
149149
} else {
150-
$result = $result->current();
151-
$result = null === $result ? [] : [$result];
150+
$result = $result->valid() && null !== $result->current() ? [$result->current()] : [];
152151
}
153152
} elseif (\is_array($result)) {
154153
reset($result);

0 commit comments

Comments
 (0)