Skip to content
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

Count assertion has incorrect failure description when used with non-rewindable traversables #5008

Closed
SamMousa opened this issue Jul 6, 2022 · 1 comment

Comments

@SamMousa
Copy link

SamMousa commented Jul 6, 2022

Q A
PHPUnit version 9.5.21
PHP version 8.1.7
Installation Method Composer

I have chosen not to include the output of composer info |sort since it's not relevant to this issue.

Summary

Failure description for count assertion is wrong for generators.

Current behavior

The function getCountOfGenerator correctly counts the number of elements in a generator. However, generators can only be iterated over once. When the match fails, failureDescription() is called:

protected function failureDescription($other): string
    {
        return sprintf(
            'actual size %d matches expected size %d',
            (int) $this->getCountOf($other),
            $this->expectedCount
        );
    }

Which does another iteration, but since the pointer is already at the end this always yields 0.

How to reproduce

$gen = function() {
            for ($i = 1; $i <= 10; $i++) {
                yield $i;
            }
        };
        self::assertCount(5, $gen());

Output

Failed asserting that actual size 0 matches expected size 5.

Expected behavior

Failed asserting that actual size 10 matches expected size 5.

Solution

Looking at the implementation of the Count assertion, it feels a little overcomplicated to me.

It has all kinds of special cases that are not needed (anymore?):

protected function getCountOf($other): ?int
    {
        if ($other instanceof Countable || is_array($other)) {
            return count($other);
        }

        // This seems like an unneeded optimization. 
        if ($other instanceof EmptyIterator) {
            return 0;
        }

        // The outside if clause is not needed.
        if ($other instanceof Traversable) {
            while ($other instanceof IteratorAggregate) {
                try {
                    $other = $other->getIterator();
                } catch (\Exception $e) {
                    throw new Exception(
                        $e->getMessage(),
                        $e->getCode(),
                        $e
                    );
                }
            }

            $iterator = $other;

            // Generators are traversable, so `iterator_count` works on them.
            if ($iterator instanceof Generator) {
                return $this->getCountOfGenerator($iterator);
            }

            if (!$iterator instanceof Iterator) {
                return iterator_count($iterator);
            }

            $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;
        }

        return null;
    }

I'll work on a PR to fix this issue and simplify the code a bit.

@SamMousa SamMousa added the type/bug Something is broken label Jul 6, 2022
@SamMousa
Copy link
Author

SamMousa commented Jul 6, 2022

Hmm, duplicate of #4271

@SamMousa SamMousa closed this as completed Jul 6, 2022
@sebastianbergmann sebastianbergmann removed the type/bug Something is broken label Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants