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

Better respect PHP native array key handling for assertArrayIs*ToArrayOnlyConsideringListOfKeys() #5716

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
38 changes: 22 additions & 16 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
*/
namespace PHPUnit\Framework;

use function array_keys;
use function class_exists;
use function count;
use function file_get_contents;
use function in_array;
use function interface_exists;
use function is_bool;
use ArrayAccess;
Expand Down Expand Up @@ -84,19 +82,23 @@ abstract class Assert
*/
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
foreach (array_keys($expected) as $key) {
if (!in_array($key, $keysToBeConsidered, true)) {
unset($expected[$key]);
$filteredExpected = [];

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

foreach (array_keys($actual) as $key) {
if (!in_array($key, $keysToBeConsidered, true)) {
unset($actual[$key]);
$filteredActual = [];

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

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

/**
Expand Down Expand Up @@ -126,19 +128,23 @@ final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $
*/
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
foreach (array_keys($expected) as $key) {
if (!in_array($key, $keysToBeConsidered, true)) {
unset($expected[$key]);
$filteredExpected = [];

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

foreach (array_keys($actual) as $key) {
if (!in_array($key, $keysToBeConsidered, true)) {
unset($actual[$key]);
$filteredActual = [];

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

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

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,58 @@ public function testAssertArrayIsIdenticalToArrayIgnoringListOfKeys(): void
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['b']);
}

public function testAssertArrayIsEqualToArrayOnlyConsideringListOfKeysInterpretsKeysSameAsPHP(): void
{
// Effective keys: int 0, int 1, int 2, string '3.0'.
$expected = [0 => 1, '1' => 2, 2.0 => 3, '3.0' => 4];
$actual = [0 => 1, '1' => 2, 2.0 => 2, '3.0' => 4];

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

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

$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys($expected, $actual, ['1', 2.0, '3.0']);
}

public function testAssertArrayIsEqualToArrayIgnoringListOfKeysInterpretsKeysSameAsPHP(): void
{
// Effective keys: int 0, int 1, int 2, string '3.0'.
$expected = [0 => 1, '1' => 2, 2.0 => 3, '3.0' => 4];
$actual = [0 => 1, '1' => 2, 2.0 => 2, '3.0' => 4];

$this->assertArrayIsEqualToArrayIgnoringListOfKeys($expected, $actual, [2.0]);

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

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

public function testAssertArrayIsIdenticalToArrayOnlyConsideringListOfKeysInterpretsKeysSameAsPHP(): void
{
// Effective keys: int 0, int 1, int 2, string '3.0'.
$expected = [0 => 1, '1' => 2, 2.0 => 3, '3.0' => 4];
$actual = [0 => 1, '1' => 2, 2.0 => 2, '3.0' => 4];

$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, [0, '1', '3.0']);

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

$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, ['1', 2.0, '3.0']);
}

public function testAssertArrayIsIdenticalToArrayIgnoringListOfKeysInterpretsKeysSameAsPHP(): void
{
// Effective keys: int 0, int 1, int 2, string '3.0'.
$expected = [0 => 1, '1' => 2, 2.0 => 3, '3.0' => 4];
$actual = [0 => 1, '1' => 2, 2.0 => 2, '3.0' => 4];

$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, [2.0]);

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

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

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