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

assertCount throws an Exception for a decorated Generator Iterator #3347

Closed
guilliamxavier opened this issue Oct 13, 2018 · 2 comments
Closed

Comments

@guilliamxavier
Copy link
Contributor

guilliamxavier commented Oct 13, 2018

Q A
PHPUnit version 6.5.13 / 7.4.3
PHP version 7.0.31 / 7.1.22 / 7.2.10
Installation Method (not relevant)

Given

(1) the following example class (in a vertically-compacted coding style):

/**
 * Wraps an Iterator: function calls are recorded, then forwarded to the wrapped iterator.
 */
class CallRecordingIterator implements \Iterator
{
    private $wrapped;
    private $recordedCalls = [];

    public function __construct(\Iterator $wrapped) { $this->wrapped = $wrapped; }

    private function recordCall($name) { $this->recordedCalls[] = $name; }

    public function rewind()  { $this->recordCall('rewind');         $this->wrapped->rewind(); }
    public function valid()   { $this->recordCall('valid');   return $this->wrapped->valid(); }
    public function key()     { $this->recordCall('key');     return $this->wrapped->key(); }
    public function current() { $this->recordCall('current'); return $this->wrapped->current(); }
    public function next()    { $this->recordCall('next');           $this->wrapped->next(); }

    public function getRecordedCalls() { return $this->recordedCalls; }
}

(2) a utility function:

function makeGenerator(array $array): \Generator
{
    foreach ($array as $key => $value) {
        yield $key => $value;
    }
}

(3) that the following test passes:

class MyTest extends \PHPUnit\Framework\TestCase
{
    public function testOk()
    {
        $generator = makeGenerator([1, 2]);
        $this->assertCount(2, $generator);
    }
}

then the (expected-to-pass) test:

    public function testIssue()
    {
        $generator = makeGenerator([1, 2]);
        $iterator = new CallRecordingIterator($generator);
        $this->assertCount(2, $iterator);
    }

results in 1 error:

Exception: Cannot rewind a generator that was already run

at phpunit/src/Framework/Constraint/Count.php:79 (same line for 6.5.13 and 7.4.3).

(A possible fix is to wrap the call to $iterator->rewind(); in a try-block [with the catch (\Exception $e) doing a return $count; or just swallow])

Note: the slightly-related pending PR #3316 keeps this behavior unchanged (the line is now here)

@stale
Copy link

stale bot commented Dec 24, 2018

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 24, 2018
@stale
Copy link

stale bot commented Dec 31, 2018

This issue has been automatically closed because it has not had activity since it was marked as stale. Thank you for your contributions.

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

1 participant