Skip to content

Commit

Permalink
Merge branch '11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 10, 2024
2 parents e6e96bd + a6eedd5 commit 3b2a455
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace PHPUnit\Framework;

use function array_combine;
use function array_intersect_key;
use function class_exists;
use function count;
use function file_get_contents;
Expand Down Expand Up @@ -128,23 +130,11 @@ final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $
*/
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
$filteredExpected = [];

foreach ($keysToBeConsidered as $key) {
if (isset($expected[$key])) {
$filteredExpected[$key] = $expected[$key];
}
}

$filteredActual = [];

foreach ($keysToBeConsidered as $key) {
if (isset($actual[$key])) {
$filteredActual[$key] = $actual[$key];
}
}
$keysToBeConsidered = array_combine($keysToBeConsidered, $keysToBeConsidered);
$expected = array_intersect_key($expected, $keysToBeConsidered);
$actual = array_intersect_key($actual, $keysToBeConsidered);

static::assertSame($filteredExpected, $filteredActual, $message);
static::assertSame($expected, $actual, $message);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,30 @@ public function testAssertArrayIsIdenticalToArrayIgnoringListOfKeysInterpretsKey
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['1']);
}

public function testAssertArrayIsEqualButNotIdenticalToArrayOnlyConsideringListOfKeys(): void
{
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
$actual = [0 => 1, 1 => 3, 'a' => 'b', 'b' => 'b'];

$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);

$this->expectException(AssertionFailedError::class);

$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);
}

public function testAssertArrayIsEqualButNotIdenticalToArrayIgnoringListOfKeys(): void
{
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
$actual = [0 => 1, 1 => 3, 'a' => 'b', 'b' => 'b'];

$this->assertArrayIsEqualToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);

$this->expectException(AssertionFailedError::class);

$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);
}

public function testAssertArrayHasIntegerKey(): void
{
$this->assertArrayHasKey(0, ['foo']);
Expand Down

0 comments on commit 3b2a455

Please sign in to comment.