Skip to content

[Bridge/Doctrine] count(): Parameter must be an array or an object that implements Countable #26831

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
Apr 25, 2018
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 @@ -517,4 +517,32 @@ public function testEntityManagerNullObject()

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

public function testValidateUniquenessOnNullResult()
{
$repository = $this->createRepositoryMock();
$repository
->method('find')
->will($this->returnValue(null))
;

$this->em = $this->createEntityManagerMock($repository);
$this->registry = $this->createRegistryMock($this->em);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name'),
'em' => self::EM_NAME,
));

$entity = new SingleIntIdEntity(1, null);

$this->em->persist($entity);
$this->em->flush();

$this->validator->validate($entity, $constraint);
$this->assertNoViolation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,23 @@ public function validate($entity, Constraint $constraint)
*/
if ($result instanceof \Iterator) {
$result->rewind();
} elseif (is_array($result)) {
if ($result instanceof \Countable && 1 < \count($result)) {
$result = array($result->current(), $result->current());
} else {
$result = $result->current();
$result = null === $result ? array() : array($result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also account for iterators that have more than one result?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear this could consume the iterator, could be an issue with non-rewindable ones

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm indeed, we should rather not break that, might be a good idea to deprecate some of the edge cases we currently try to handle

}
} elseif (\is_array($result)) {
reset($result);
} else {
$result = null === $result ? array() : array($result);
}

/* If no entity matched the query criteria or a single entity matched,
* which is the same as the entity being validated, the criteria is
* unique.
*/
if (0 === count($result) || (1 === count($result) && $entity === ($result instanceof \Iterator ? $result->current() : current($result)))) {
if (!$result || (1 === \count($result) && current($result) === $entity)) {
return;
}

Expand Down