From 6205f335954d00fa01992fe324a7eefbc954449e Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 5 Feb 2019 16:55:33 +0100 Subject: [PATCH] Closes #3511 --- ChangeLog-8.0.md | 1 + src/Framework/Assert.php | 18 ++++++++++++-- tests/unit/Framework/AssertTest.php | 38 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/ChangeLog-8.0.md b/ChangeLog-8.0.md index e2dd435c2cc..c47a59301a0 100644 --- a/ChangeLog-8.0.md +++ b/ChangeLog-8.0.md @@ -7,6 +7,7 @@ All notable changes of the PHPUnit 8.0 release series are documented in this fil ### Fixed * Fixed [#3508](https://github.com/sebastianbergmann/phpunit/pull/3508): `TypeError` in `Fileloader` when trying to load nonexistent file +* Fixed [#3511](https://github.com/sebastianbergmann/phpunit/issues/3511): Asserting that an object is contained in an `iterable` while using `==` instead of `===` is no longer possible ## [8.0.1] - 2019-02-03 diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index 508ae5b4aa5..57f9b9f8981 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -188,7 +188,7 @@ public static function assertContains($needle, $haystack, string $message = '', } if ($checkForObjectIdentity !== true) { - self::createWarning('The optional $checkForObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9.'); + self::createWarning('The optional $checkForObjectIdentity parameter of assertContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertContainsEquals() instead.'); } if ($checkForNonObjectIdentity !== false) { @@ -229,6 +229,13 @@ public static function assertContains($needle, $haystack, string $message = '', static::assertThat($haystack, $constraint, $message); } + public static function assertContainsEquals($needle, iterable $haystack, string $message = ''): void + { + $constraint = new TraversableContains($needle, false, false); + + static::assertThat($haystack, $constraint, $message); + } + /** * Asserts that a haystack that is stored in a static attribute of a class * or an attribute of an object contains a needle. @@ -272,7 +279,7 @@ public static function assertNotContains($needle, $haystack, string $message = ' } if ($checkForObjectIdentity !== true) { - self::createWarning('The optional $checkForObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.'); + self::createWarning('The optional $checkForObjectIdentity parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertNotContainsEquals() instead.'); } if ($checkForNonObjectIdentity !== false) { @@ -317,6 +324,13 @@ public static function assertNotContains($needle, $haystack, string $message = ' static::assertThat($haystack, $constraint, $message); } + public static function assertNotContainsEquals($needle, iterable $haystack, string $message = ''): void + { + $constraint = new LogicalNot(new TraversableContains($needle, false, false)); + + static::assertThat($haystack, $constraint, $message); + } + /** * Asserts that a haystack that is stored in a static attribute of a class * or an attribute of an object does not contain a needle. diff --git a/tests/unit/Framework/AssertTest.php b/tests/unit/Framework/AssertTest.php index 8a6c2035109..fd9470e30a6 100644 --- a/tests/unit/Framework/AssertTest.php +++ b/tests/unit/Framework/AssertTest.php @@ -2192,6 +2192,44 @@ public function testIterableNotContainsSameObjectCanBeAsserted(): void $this->fail(); } + public function testIterableContainsEqualObjectCanBeAsserted(): void + { + $a = new \stdClass; + $a->foo = 'bar'; + + $b = new \stdClass; + $b->foo = 'baz'; + + $this->assertContainsEquals($a, [$a]); + + try { + $this->assertContainsEquals($b, [$a]); + } catch (AssertionFailedError $e) { + return; + } + + $this->fail(); + } + + public function testIterableNotContainsEqualObjectCanBeAsserted(): void + { + $a = new \stdClass; + $a->foo = 'bar'; + + $b = new \stdClass; + $b->foo = 'baz'; + + $this->assertNotContainsEquals($b, [$a]); + + try { + $this->assertNotContainsEquals($a, [$a]); + } catch (AssertionFailedError $e) { + return; + } + + $this->fail(); + } + protected function sameValues(): array { $object = new \SampleClass(4, 8, 15);