Skip to content

Commit

Permalink
bug #40060 fix validator when we have false returned by the current e…
Browse files Browse the repository at this point in the history
…lement 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
  • Loading branch information
Nyholm committed Feb 2, 2021
2 parents 90e8254 + a9e9359 commit 8bd81b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
Expand Up @@ -793,4 +793,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()
{
}
}],
];
}
}
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 8bd81b5

Please sign in to comment.