Skip to content

Commit c348e59

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: Streamline dataproviders fix validator when we have a false current element [Mime] Fix case-sensitive handling in Headers::isUniqueHeader() [yaml] Delelte unused comparison operation
2 parents 218f5db + 879cf0f commit c348e59

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,85 @@ public function testValidateUniquenessCause()
831831
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
832832
->assertRaised();
833833
}
834+
835+
/**
836+
* @dataProvider resultWithEmptyIterator
837+
*/
838+
public function testValidateUniquenessWithEmptyIterator($entity, $result)
839+
{
840+
$constraint = new UniqueEntity([
841+
'message' => 'myMessage',
842+
'fields' => ['name'],
843+
'em' => self::EM_NAME,
844+
'repositoryMethod' => 'findByCustom',
845+
]);
846+
847+
$repository = $this->createRepositoryMock();
848+
$repository->expects($this->once())
849+
->method('findByCustom')
850+
->willReturn($result)
851+
;
852+
$this->em = $this->createEntityManagerMock($repository);
853+
$this->registry = $this->createRegistryMock($this->em);
854+
$this->validator = $this->createValidator();
855+
$this->validator->initialize($this->context);
856+
857+
$this->validator->validate($entity, $constraint);
858+
859+
$this->assertNoViolation();
860+
}
861+
862+
public function resultWithEmptyIterator(): array
863+
{
864+
$entity = new SingleIntIdEntity(1, 'foo');
865+
866+
return [
867+
[$entity, new class() implements \Iterator {
868+
public function current()
869+
{
870+
return null;
871+
}
872+
873+
public function valid(): bool
874+
{
875+
return false;
876+
}
877+
878+
public function next()
879+
{
880+
}
881+
882+
public function key()
883+
{
884+
}
885+
886+
public function rewind()
887+
{
888+
}
889+
}],
890+
[$entity, new class() implements \Iterator {
891+
public function current()
892+
{
893+
return false;
894+
}
895+
896+
public function valid(): bool
897+
{
898+
return false;
899+
}
900+
901+
public function next()
902+
{
903+
}
904+
905+
public function key()
906+
{
907+
}
908+
909+
public function rewind()
910+
{
911+
}
912+
}],
913+
];
914+
}
834915
}

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)