Skip to content

Commit 04a892c

Browse files
Closes #5600
1 parent 0fc2a23 commit 04a892c

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

ChangeLog-11.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes of the PHPUnit 11.0 release series are documented in this fi
44

55
## [11.0.0] - 2024-02-02
66

7+
### Added
8+
9+
* [#5600](https://github.com/sebastianbergmann/phpunit/pull/5600): Assertions for comparing arrays while ignoring a specified list of keys
10+
711
### Changed
812

913
* [#5213](https://github.com/sebastianbergmann/phpunit/issues/5213): Make `TestCase` methods `protected` that should have been `protected` all along

src/Framework/Assert.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010
namespace PHPUnit\Framework;
1111

12+
use function array_keys;
1213
use function class_exists;
1314
use function count;
1415
use function file_get_contents;
16+
use function in_array;
1517
use function interface_exists;
1618
use function is_bool;
1719
use ArrayAccess;
@@ -72,6 +74,90 @@ abstract class Assert
7274
{
7375
private static int $count = 0;
7476

77+
/**
78+
* Asserts that two arrays are equal while only considering a list of keys.
79+
*
80+
* @psalm-param non-empty-list<array-key> $keysToBeConsidered
81+
*
82+
* @throws Exception
83+
* @throws ExpectationFailedException
84+
*/
85+
final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
86+
{
87+
foreach (array_keys($expected) as $key) {
88+
if (!in_array($key, $keysToBeConsidered, true)) {
89+
unset($expected[$key]);
90+
}
91+
}
92+
93+
foreach (array_keys($actual) as $key) {
94+
if (!in_array($key, $keysToBeConsidered, true)) {
95+
unset($actual[$key]);
96+
}
97+
}
98+
99+
static::assertEquals($expected, $actual, $message);
100+
}
101+
102+
/**
103+
* Asserts that two arrays are equal while ignoring a list of keys.
104+
*
105+
* @psalm-param non-empty-list<array-key> $keysToBeIgnored
106+
*
107+
* @throws Exception
108+
* @throws ExpectationFailedException
109+
*/
110+
final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
111+
{
112+
foreach ($keysToBeIgnored as $key) {
113+
unset($expected[$key], $actual[$key]);
114+
}
115+
116+
static::assertEquals($expected, $actual, $message);
117+
}
118+
119+
/**
120+
* Asserts that two arrays are identical while only considering a list of keys.
121+
*
122+
* @psalm-param non-empty-list<array-key> $keysToBeConsidered
123+
*
124+
* @throws Exception
125+
* @throws ExpectationFailedException
126+
*/
127+
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
128+
{
129+
foreach (array_keys($expected) as $key) {
130+
if (!in_array($key, $keysToBeConsidered, true)) {
131+
unset($expected[$key]);
132+
}
133+
}
134+
135+
foreach (array_keys($actual) as $key) {
136+
if (!in_array($key, $keysToBeConsidered, true)) {
137+
unset($actual[$key]);
138+
}
139+
}
140+
141+
static::assertSame($expected, $actual, $message);
142+
}
143+
144+
/**
145+
* Asserts that two arrays are equal while ignoring a list of keys.
146+
*
147+
* @psalm-param non-empty-list<array-key> $keysToBeIgnored
148+
*
149+
* @throws Exception
150+
* @throws ExpectationFailedException
151+
*/
152+
final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
153+
{
154+
foreach ($keysToBeIgnored as $key) {
155+
unset($expected[$key], $actual[$key]);
156+
}
157+
158+
static::assertSame($expected, $actual, $message);
159+
}
160+
75161
/**
76162
* Asserts that an array has a specified key.
77163
*

src/Framework/Assert/Functions.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,82 @@
6969
use PHPUnit\Util\Xml\XmlException;
7070
use Throwable;
7171

72+
if (!function_exists('PHPUnit\Framework\assertArrayIsEqualToArrayOnlyConsideringListOfKeys')) {
73+
/**
74+
* Asserts that two arrays are equal while only considering a list of keys.
75+
*
76+
* @psalm-param list<array-key> $keysToBeConsidered
77+
*
78+
* @throws Exception
79+
* @throws ExpectationFailedException
80+
*
81+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
82+
*
83+
* @see Assert::assertArrayIsEqualToArrayOnlyConsideringListOfKeys
84+
*/
85+
function assertArrayIsEqualToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
86+
{
87+
Assert::assertArrayIsEqualToArrayOnlyConsideringListOfKeys(...func_get_args());
88+
}
89+
}
90+
91+
if (!function_exists('PHPUnit\Framework\assertArrayIsEqualToArrayIgnoringListOfKeys')) {
92+
/**
93+
* Asserts that two arrays are equal while ignoring a list of keys.
94+
*
95+
* @psalm-param list<array-key> $keysToBeIgnored
96+
*
97+
* @throws Exception
98+
* @throws ExpectationFailedException
99+
*
100+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
101+
*
102+
* @see Assert::assertArrayIsEqualToArrayIgnoringListOfKeys
103+
*/
104+
function assertArrayIsEqualToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
105+
{
106+
Assert::assertArrayIsEqualToArrayIgnoringListOfKeys(...func_get_args());
107+
}
108+
}
109+
110+
if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys')) {
111+
/**
112+
* Asserts that two arrays are identical while only considering a list of keys.
113+
*
114+
* @psalm-param list<array-key> $keysToBeConsidered
115+
*
116+
* @throws Exception
117+
* @throws ExpectationFailedException
118+
*
119+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
120+
*
121+
* @see Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys
122+
*/
123+
function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
124+
{
125+
Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(...func_get_args());
126+
}
127+
}
128+
129+
if (!function_exists('PHPUnit\Framework\assertArrayIsIdenticalToArrayIgnoringListOfKeys')) {
130+
/**
131+
* Asserts that two arrays are equal while ignoring a list of keys.
132+
*
133+
* @psalm-param list<array-key> $keysToBeIgnored
134+
*
135+
* @throws Exception
136+
* @throws ExpectationFailedException
137+
*
138+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
139+
*
140+
* @see Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys
141+
*/
142+
function assertArrayIsIdenticalToArrayIgnoringListOfKeys(array $expected, array $actual, array $keysToBeIgnored, string $message = ''): void
143+
{
144+
Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys(...func_get_args());
145+
}
146+
}
147+
72148
if (!function_exists('PHPUnit\Framework\assertArrayHasKey')) {
73149
/**
74150
* Asserts that an array has a specified key.

tests/unit/Framework/AssertTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,54 @@ public function testAssertContainsOnlyInstancesOf(): void
143143
$this->assertContainsOnlyInstancesOf(Book::class, $test2);
144144
}
145145

146+
public function testAssertArrayIsEqualToArrayOnlyConsideringListOfKeys(): void
147+
{
148+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
149+
$actual = ['a' => 'b', 'b' => 'b', 0 => 1, 1 => 3];
150+
151+
$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);
152+
153+
$this->expectException(AssertionFailedError::class);
154+
155+
$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys($expected, $actual, ['b']);
156+
}
157+
158+
public function testAssertArrayIsEqualToArrayIgnoringListOfKeys(): void
159+
{
160+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
161+
$actual = ['a' => 'b', 'b' => 'b', 0 => 1, 1 => 3];
162+
163+
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);
164+
165+
$this->expectException(AssertionFailedError::class);
166+
167+
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($expected, $actual, ['b']);
168+
}
169+
170+
public function testAssertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(): void
171+
{
172+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
173+
$actual = ['a' => 'b', 'b' => 'b', 0 => 1, 1 => 3];
174+
175+
$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);
176+
177+
$this->expectException(AssertionFailedError::class);
178+
179+
$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, ['b']);
180+
}
181+
182+
public function testAssertArrayIsIdenticalToArrayIgnoringListOfKeys(): void
183+
{
184+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
185+
$actual = ['a' => 'b', 'b' => 'b', 0 => 1, 1 => 3];
186+
187+
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);
188+
189+
$this->expectException(AssertionFailedError::class);
190+
191+
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['b']);
192+
}
193+
146194
public function testAssertArrayHasIntegerKey(): void
147195
{
148196
$this->assertArrayHasKey(0, ['foo']);

0 commit comments

Comments
 (0)