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

Skip to content

fix validator when we have false returned by the current element of the iterator #40060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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()
{
}
}],
];
}
}
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