[Bridge/Doctrine] fix count() notice on PHP 7.2

This commit is contained in:
gpenverne 2018-04-06 11:56:21 +02:00 committed by Nicolas Grekas
parent 923417122a
commit 715373fea6
2 changed files with 38 additions and 2 deletions

View File

@ -517,4 +517,32 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
$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();
}
}

View File

@ -133,15 +133,23 @@ class UniqueEntityValidator extends ConstraintValidator
*/
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);
}
} 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;
}