Skip to content

Commit

Permalink
Implement assertArrayIsEqualToArrayOnlyConsideringListOfKeys() and as…
Browse files Browse the repository at this point in the history
…sertArrayIsIdenticalToArrayOnlyConsideringListOfKeys()
  • Loading branch information
sebastianbergmann committed Dec 12, 2023
1 parent 48cad35 commit a505e63
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 16 deletions.
56 changes: 54 additions & 2 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,31 @@ 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.
*
Expand All @@ -80,7 +107,7 @@ abstract class Assert
* @throws Exception
* @throws ExpectationFailedException
*/
final public static function assertArrayIsEqualToArrayIgnoringKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
foreach ($keysToBeIgnored as $key) {
unset($expected[$key], $actual[$key]);
Expand All @@ -89,6 +116,31 @@ final public static function assertArrayIsEqualToArrayIgnoringKeys(array $expect
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.
*
Expand All @@ -97,7 +149,7 @@ final public static function assertArrayIsEqualToArrayIgnoringKeys(array $expect
* @throws Exception
* @throws ExpectationFailedException
*/
final public static function assertArrayIsIdenticalToArrayIgnoringKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
foreach ($keysToBeIgnored as $key) {
unset($expected[$key], $actual[$key]);
Expand Down
54 changes: 46 additions & 8 deletions src/Framework/Assert/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,26 @@
use PHPUnit\Util\Xml\XmlException;
use Throwable;

if (!function_exists('PHPUnit\Framework\assertArrayIsEqualToArrayIgnoringKeys')) {
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.
*
Expand All @@ -80,15 +99,34 @@
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsEqualToArrayIgnoringKeys
* @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 assertArrayIsEqualToArrayIgnoringKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
{
Assert::assertArrayIsEqualToArrayIgnoringKeys(...func_get_args());
Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayIgnoringKeys')) {
if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayIgnoringListOfKeys')) {
/**
* Asserts that two arrays are equal while ignoring a list of keys.
*
Expand All @@ -99,11 +137,11 @@ function assertArrayIsEqualToArrayIgnoringKeys(array $expected, array $actual, a
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertArrayIsIdenticalToArrayIgnoringKeys
* @see Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys
*/
function assertArrayIsIdenticalToArrayIgnoringKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
{
Assert::assertArrayIsIdenticalToArrayIgnoringKeys(...func_get_args());
Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys(...func_get_args());
}
}

Expand Down
36 changes: 30 additions & 6 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,52 @@ public function testAssertContainsOnlyInstancesOf(): void
$this->assertContainsOnlyInstancesOf(Book::class, $test2);
}

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

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

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

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

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

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

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

$this->assertArrayIsIdenticalToArrayIgnoringKeys($expected, $actual, ['b']);
$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
Expand Down

0 comments on commit a505e63

Please sign in to comment.