Skip to content

Commit

Permalink
Closes #3511
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 5, 2019
1 parent 0e5d4c2 commit 6205f33
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog-8.0.md
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions src/Framework/Assert.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Framework/AssertTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 6205f33

Please sign in to comment.