[Bridge/Doctrine] fix count() notice on PHP 7.2
This commit is contained in:
parent
923417122a
commit
715373fea6
@ -517,4 +517,32 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
|
|
||||||
$this->validator->validate($entity, $constraint);
|
$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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,15 +133,23 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||||||
*/
|
*/
|
||||||
if ($result instanceof \Iterator) {
|
if ($result instanceof \Iterator) {
|
||||||
$result->rewind();
|
$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);
|
reset($result);
|
||||||
|
} else {
|
||||||
|
$result = null === $result ? array() : array($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If no entity matched the query criteria or a single entity matched,
|
/* If no entity matched the query criteria or a single entity matched,
|
||||||
* which is the same as the entity being validated, the criteria is
|
* which is the same as the entity being validated, the criteria is
|
||||||
* unique.
|
* 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user