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

PHPUnit and Generators #2149

Closed
live627 opened this issue Apr 29, 2016 · 4 comments
Closed

PHPUnit and Generators #2149

live627 opened this issue Apr 29, 2016 · 4 comments
Assignees
Labels
type/bug Something is broken

Comments

@live627
Copy link

live627 commented Apr 29, 2016

#1174 is still an issue. The offending code seems to be in 4.xx, 5.xx, and master.

The example test in that thread still applies.

@sebastianbergmann
Copy link
Owner

Copied here from #1174:

<?php
class GeneratorTest extends PHPUnit_Framework_TestCase
{
    public static function generator($range)
    {
        foreach (range(0, $range) as $x) {
            yield $x;
        }
    }

    public function testCountingAGeneratorFailsWithPHPWarning()
    {
        $this->assertCount(10, self::generator(10));
    }
}
$ phpunit GeneratorTest
PHPUnit 5.3.2-7-g3e6974d by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 18 ms, Memory: 4.00Mb

There was 1 error:

1) GeneratorTest::testCountingAGeneratorFailsWithPHPWarning
Exception: Cannot rewind a generator that was already run

/home/sb/GeneratorTest.php:13

FAILURES!
Tests: 1, Assertions: 1, Errors: 1.

@sebastianbergmann sebastianbergmann added the type/bug Something is broken label Apr 29, 2016
@keradus
Copy link
Contributor

keradus commented Jun 15, 2016

I encounter this again.
With the following change: keradus/PHP-CS-Fixer@fbbc8c1
the $this->assertSame('somefile.php', $iterator->current()->getFilename()); fails on HHVM.
If I use assertSame instead of assertCount everything is working on HHVM.

@Nessworthy
Copy link
Contributor

Nessworthy commented Feb 20, 2017

Cause is here:
https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/Constraint/Count.php#L69
Generators cannot be rewound, but assertCount will try and rewind it to its original state if it can because it only sees it as an Iterator.

Sooo, possible solutions are to either add in a case for generators so the rewind call is skipped (as previously suggested), or add new assertions which specifically target generators.

Thoughts?

@sebastianbergmann
Copy link
Owner

Closed via 4123b6a.

@sebastianbergmann sebastianbergmann self-assigned this Mar 2, 2017
jmikola added a commit to jmikola/phpunit that referenced this issue May 22, 2017
This allows generators (sebastianbergmann#2149) and internal Traversables (sebastianbergmann#2642) to be properly detected before attempting to restore the Iterator's position after counting (sebastianbergmann#1125).
sebastianbergmann pushed a commit that referenced this issue Jun 1, 2017
This allows generators (#2149) and internal Traversables (#2642) to be properly detected before attempting to restore the Iterator's position after counting (#1125).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

4 participants