diff --git a/src/Framework/Constraint/Count.php b/src/Framework/Constraint/Count.php index 7aeb2d72fcc..6973d0d0eb4 100644 --- a/src/Framework/Constraint/Count.php +++ b/src/Framework/Constraint/Count.php @@ -22,6 +22,11 @@ class Count extends Constraint */ private $expectedCount; + /** + * @var null|int + */ + private $countOfGenerator; + public function __construct(int $expected) { parent::__construct(); @@ -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; } /** diff --git a/tests/_files/GeneratorTest.php b/tests/_files/GeneratorTest.php new file mode 100644 index 00000000000..12487f759dc --- /dev/null +++ b/tests/_files/GeneratorTest.php @@ -0,0 +1,28 @@ + + * + * 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()); + } +} diff --git a/tests/end-to-end/generator-failing-assert-count.phpt b/tests/end-to-end/generator-failing-assert-count.phpt new file mode 100644 index 00000000000..d51974c9712 --- /dev/null +++ b/tests/end-to-end/generator-failing-assert-count.phpt @@ -0,0 +1,26 @@ +--TEST-- +phpunit BankAccountTest ../../_files/GeneratorTest.php +--FILE-- + +--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.