From 9faa89fb0cf267f4ea145c30f47f65e3f3e0cf27 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 2 Jul 2019 09:30:14 +0200 Subject: [PATCH] Closes #3743 --- ChangeLog-7.5.md | 7 +++++++ src/Framework/Constraint/Count.php | 4 ++++ src/Framework/Constraint/IsEmpty.php | 4 ++++ tests/unit/Framework/Constraint/CountTest.php | 10 ++++++++++ tests/unit/Framework/Constraint/IsEmptyTest.php | 10 ++++++++++ 5 files changed, 35 insertions(+) diff --git a/ChangeLog-7.5.md b/ChangeLog-7.5.md index 738cfdb505d..bb4fee75cc6 100644 --- a/ChangeLog-7.5.md +++ b/ChangeLog-7.5.md @@ -2,6 +2,12 @@ All notable changes of the PHPUnit 7.5 release series are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [7.5.14] - 2019-MM-DD + +### Fixed + +* Fixed [#3743](https://github.com/sebastianbergmann/phpunit/issues/3743): `EmptyIterator` instances are not handled correctly by `Count` and `IsEmpty` constraints + ## [7.5.13] - 2019-06-19 ### Fixed @@ -126,6 +132,7 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil * Fixed [#3429](https://github.com/sebastianbergmann/phpunit/pull/3429): Inefficient loop in `getHookMethods()` * Fixed [#3437](https://github.com/sebastianbergmann/phpunit/pull/3437): JUnit logger skips PHPT tests +[7.5.14]: https://github.com/sebastianbergmann/phpunit/compare/7.5.13...7.5.14 [7.5.13]: https://github.com/sebastianbergmann/phpunit/compare/7.5.12...7.5.13 [7.5.12]: https://github.com/sebastianbergmann/phpunit/compare/7.5.11...7.5.12 [7.5.11]: https://github.com/sebastianbergmann/phpunit/compare/7.5.10...7.5.11 diff --git a/src/Framework/Constraint/Count.php b/src/Framework/Constraint/Count.php index 7aeb2d72fcc..42873b82d9d 100644 --- a/src/Framework/Constraint/Count.php +++ b/src/Framework/Constraint/Count.php @@ -51,6 +51,10 @@ protected function matches($other): bool */ protected function getCountOf($other): ?int { + if ($other instanceof \EmptyIterator) { + return 0; + } + if ($other instanceof Countable || \is_array($other)) { return \count($other); } diff --git a/src/Framework/Constraint/IsEmpty.php b/src/Framework/Constraint/IsEmpty.php index 0eb89ce63e3..1ffe41700e3 100644 --- a/src/Framework/Constraint/IsEmpty.php +++ b/src/Framework/Constraint/IsEmpty.php @@ -32,6 +32,10 @@ public function toString(): string */ protected function matches($other): bool { + if ($other instanceof \EmptyIterator) { + return true; + } + if ($other instanceof Countable) { return \count($other) === 0; } diff --git a/tests/unit/Framework/Constraint/CountTest.php b/tests/unit/Framework/Constraint/CountTest.php index 5656939e4d9..ccb58176b50 100644 --- a/tests/unit/Framework/Constraint/CountTest.php +++ b/tests/unit/Framework/Constraint/CountTest.php @@ -142,4 +142,14 @@ public function testCountTraversable(): void $this->assertNotInstanceOf(\IteratorAggregate::class, $datePeriod); $this->assertTrue($countConstraint->evaluate($datePeriod, '', true)); } + + /** + * @ticket https://github.com/sebastianbergmann/phpunit/issues/3743 + */ + public function test_EmptyIterator_is_handled_correctly(): void + { + $constraint = new Count(0); + + $this->assertTrue($constraint->evaluate(new \EmptyIterator, '', true)); + } } diff --git a/tests/unit/Framework/Constraint/IsEmptyTest.php b/tests/unit/Framework/Constraint/IsEmptyTest.php index a0db508070d..96a3d8219ce 100644 --- a/tests/unit/Framework/Constraint/IsEmptyTest.php +++ b/tests/unit/Framework/Constraint/IsEmptyTest.php @@ -64,4 +64,14 @@ public function testConstraintIsEmpty2(): void $this->fail(); } + + /** + * @ticket https://github.com/sebastianbergmann/phpunit/issues/3743 + */ + public function test_EmptyIterator_is_handled_correctly(): void + { + $constraint = new IsEmpty; + + $this->assertTrue($constraint->evaluate(new \EmptyIterator, '', true)); + } }