Skip to content

PHPUnit fails to infer zero count for an EmptyIterator instance. #3743

@sanmai

Description

@sanmai
Q A
PHPUnit version 8.2.3
PHP version 7.3.4
Installation Method Composer

This assertion should not fail:

$this->assertCount(0, new \EmptyIterator());

But it fails with an error:

1) Example::testEmptyIterator
BadMethodCallException: Accessing the key of an EmptyIterator

On the other hand this assertion works without a hitch:

$this->assertSame(0, iterator_count(new \EmptyIterator()));

These seems to be the problematic lines:

$key = $iterator->key();
$count = \iterator_count($iterator);
// Manually rewind $iterator to previous key, since iterator_count
// moves pointer.
if ($key !== null) {
$iterator->rewind();
while ($iterator->valid() && $key !== $iterator->key()) {
$iterator->next();
}
}
return $count;

A Travis CI build with this failure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething is broken

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions