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

Actual size is wrong in assertCount with generators #4229

Closed
spaceemotion opened this issue May 15, 2020 · 3 comments
Closed

Actual size is wrong in assertCount with generators #4229

spaceemotion opened this issue May 15, 2020 · 3 comments
Labels
feature/assertion Issues related to assertions and expectations type/bug Something is broken

Comments

@spaceemotion
Copy link

spaceemotion commented May 15, 2020

Q A
PHPUnit version 9.1.4
PHP version 7.4.0
Installation Method Composer

Summary

The original sample at #3302 (comment)

   public function testGenerator()
    {
        $generator = static function () {
            yield 1;
            yield 2;
            yield 3;
        };

        static::assertCount(3, $generator());
        static::assertCount(4, $generator());
    }

no longer seems to work when trying to assert the count of four. The error message spits out the following instead:

Failed asserting that actual size 0 matches expected size 4

Current behavior

I initially came across this in my own code, but found the example linked above. My generator definitely holds some values, and is being iterated in the assertCount() check, but the error message is just wrong.

How to reproduce

Just execute the example linked above.

Expected behavior

Something along the lines of:

Failed asserting that actual size 3 matches expected size 4

@spaceemotion spaceemotion added the type/bug Something is broken label May 15, 2020
@sebastianbergmann sebastianbergmann added the feature/assertion Issues related to assertions and expectations label May 15, 2020
@amenning
Copy link

amenning commented Jun 7, 2020

I found this was because the generator cannot be cloned and can only be iterated over once. The actual count was correctly determined in the matches function. However, then the failureDescription attempts to iterate over the generator a second time to provide the failure details, resulting in the null count for the message.

One solution could be to store the actual count result inside the matches method for use in the failureDescription to provide the expected behavior.

@amenning
Copy link

amenning commented Jun 7, 2020

#4271 is a potential solution PR

@sebastianbergmann
Copy link
Owner

Superseded by #4567 and #4568.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/assertion Issues related to assertions and expectations type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants