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

Fix for assertCount expected size for Generator #3304

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Framework/Constraint/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Count extends Constraint
*/
private $expectedCount;

/**
* @var null|int
*/
private $countOfGenerator;

public function __construct(int $expected)
{
parent::__construct();
Expand Down Expand Up @@ -93,11 +98,13 @@ protected function getCountOf($other): ?int
*/
protected function getCountOfGenerator(Generator $generator): int
{
for ($count = 0; $generator->valid(); $generator->next()) {
++$count;
if ($this->countOfGenerator === null) {
for ($this->countOfGenerator = 0; $generator->valid(); $generator->next()) {
++$this->countOfGenerator;
}
}

return $count;
return $this->countOfGenerator;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/_files/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Test;

use PHPUnit\Framework\TestCase;

class GeneratorTest extends TestCase
{
public function testGenerator()
{
$generator = static function () {
yield 1;

yield 2;

yield 3;
};

static::assertCount(4, $generator());
}
}
26 changes: 26 additions & 0 deletions tests/end-to-end/generator-failing-assert-count.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
phpunit BankAccountTest ../../_files/GeneratorTest.php
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][3] = __DIR__ . '/../_files/GeneratorTest.php';

require __DIR__ . '/../bootstrap.php';
PHPUnit\TextUI\Command::main();
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

F 1 / 1 (100%)

Time: %s, Memory: %s

There was 1 failure:

1) Test\GeneratorTest::testGenerator
Failed asserting that actual size 3 matches expected size 4.

%sGeneratorTest.php:%i

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