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

Assertions for comparing arrays while ignoring a specified list of keys #5600

Closed
wants to merge 2 commits into from
Closed
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
86 changes: 86 additions & 0 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
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 @@ -72,6 +74,90 @@ abstract class Assert
{
private static int $count = 0;

/**
* Asserts that two arrays are equal while only considering a list of keys.
*
* @psalm-param list<array-key> $keysToBeConsidered
*
* @throws Exception
* @throws ExpectationFailedException
*/
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]);
}
}

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

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

/**
* Asserts that two arrays are equal while ignoring a list of keys.
*
* @psalm-param list<array-key> $keysToBeIgnored
*
* @throws Exception
* @throws ExpectationFailedException
*/
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
foreach ($keysToBeIgnored as $key) {
unset($expected[$key], $actual[$key]);
}

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

/**
* Asserts that two arrays are identical while only considering a list of keys.
*
* @psalm-param list<array-key> $keysToBeConsidered
*
* @throws Exception
* @throws ExpectationFailedException
*/
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]);
}
}

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

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

/**
* Asserts that two arrays are equal while ignoring a list of keys.
*
* @psalm-param list<array-key> $keysToBeIgnored
*
* @throws Exception
* @throws ExpectationFailedException
*/
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
foreach ($keysToBeIgnored as $key) {
unset($expected[$key], $actual[$key]);
}

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

/**
* Asserts that an array has a specified key.
*
Expand Down
76 changes: 76 additions & 0 deletions src/Framework/Assert/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,82 @@
use PHPUnit\Util\Xml\XmlException;
use Throwable;

if (!function_exists('PHPUnit\Framework\assertArrayIsEqualToArrayOnlyConsideringListOfKeys')) {
/**
* Asserts that two arrays are equal while only considering a list of keys.
*
* @psalm-param list<array-key> $keysToBeConsidered
*
* @throws Exception
* @throws ExpectationFailedException
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsEqualToArrayOnlyConsideringListOfKeys
*/
function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
Assert::assertArrayIsEqualToArrayOnlyConsideringListOfKeys(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\assertArrayIsEqualToArrayIgnoringListOfKeys')) {
/**
* Asserts that two arrays are equal while ignoring a list of keys.
*
* @psalm-param list<array-key> $keysToBeIgnored
*
* @throws Exception
* @throws ExpectationFailedException
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsEqualToArrayIgnoringListOfKeys
*/
function assertArrayIsEqualToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
Assert::assertArrayIsEqualToArrayIgnoringListOfKeys(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys')) {
/**
* Asserts that two arrays are identical while only considering a list of keys.
*
* @psalm-param list<array-key> $keysToBeConsidered
*
* @throws Exception
* @throws ExpectationFailedException
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys
*/
function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayIgnoringListOfKeys')) {
/**
* Asserts that two arrays are equal while ignoring a list of keys.
*
* @psalm-param list<array-key> $keysToBeIgnored
*
* @throws Exception
* @throws ExpectationFailedException
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys
*/
function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\assertArrayHasKey')) {
/**
* Asserts that an array has a specified key.
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,54 @@ public function testAssertContainsOnlyInstancesOf(): void
$this->assertContainsOnlyInstancesOf(Book::class, $test2);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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