Skip to content

Commit

Permalink
Merge branch '4.4' into 5.2
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fabpot committed Feb 3, 2021
2 parents 218f5db + 879cf0f commit c348e59
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
81 changes: 81 additions & 0 deletions Tests/Validator/Constraints/UniqueEntityValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,85 @@ public function testValidateUniquenessCause()
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}

/**
* @dataProvider resultWithEmptyIterator
*/
public function testValidateUniquenessWithEmptyIterator($entity, $result)
{
$constraint = new UniqueEntity([
'message' => 'myMessage',
'fields' => ['name'],
'em' => self::EM_NAME,
'repositoryMethod' => 'findByCustom',
]);

$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->method('findByCustom')
->willReturn($result)
;
$this->em = $this->createEntityManagerMock($repository);
$this->registry = $this->createRegistryMock($this->em);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

$this->validator->validate($entity, $constraint);

$this->assertNoViolation();
}

public function resultWithEmptyIterator(): array
{
$entity = new SingleIntIdEntity(1, 'foo');

return [
[$entity, new class() implements \Iterator {
public function current()
{
return null;
}

public function valid(): bool
{
return false;
}

public function next()
{
}

public function key()
{
}

public function rewind()
{
}
}],
[$entity, new class() implements \Iterator {
public function current()
{
return false;
}

public function valid(): bool
{
return false;
}

public function next()
{
}

public function key()
{
}

public function rewind()
{
}
}],
];
}
}
3 changes: 1 addition & 2 deletions Validator/Constraints/UniqueEntityValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public function validate($entity, Constraint $constraint)
if ($result instanceof \Countable && 1 < \count($result)) {
$result = [$result->current(), $result->current()];
} else {
$result = $result->current();
$result = null === $result ? [] : [$result];
$result = $result->valid() && null !== $result->current() ? [$result->current()] : [];
}
} elseif (\is_array($result)) {
reset($result);
Expand Down

0 comments on commit c348e59

Please sign in to comment.