Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys() does not correctly handle array order #5729

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/Framework/Assert.php
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
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