Skip to content

Commit a6eedd5

Browse files
jrfnlsebastianbergmann
authored andcommitted
assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(): bug fix
Follow up on 5716. Sorry, I screwed up. While the fix in 5716 correctly fixes the handling of array keys to be in line with PHP itself, it broke the differentiation between `isEqual` and `isIdentical` as the arrays were now being recreated in the order of the keys passed to the `$keysToBeConsidered` parameter. This is not problematic for the `assertArrayIsEqualToArrayOnlyConsideringListOfKeys()` assertion as the array order is not relevant there. However, it is problematic for the `assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys()` assertion, where the array order is relevant. This commit fixes it. Includes unit test safeguarding the fix. And while the `assertArrayIs*ToArrayIgnoringListOfKeys()` assertions are not affected, I've also included a unit test for the issue for those assertions, just to be on the safe side.
1 parent dcb95b9 commit a6eedd5

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/Framework/Assert.php

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

12+
use function array_combine;
13+
use function array_intersect_key;
1214
use function class_exists;
1315
use function count;
1416
use function file_get_contents;
@@ -128,23 +130,11 @@ final public static function assertArrayIsEqualToArrayIgnoringListOfKeys(array $
128130
*/
129131
final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(array $expected, array $actual, array $keysToBeConsidered, string $message = ''): void
130132
{
131-
$filteredExpected = [];
132-
133-
foreach ($keysToBeConsidered as $key) {
134-
if (isset($expected[$key])) {
135-
$filteredExpected[$key] = $expected[$key];
136-
}
137-
}
138-
139-
$filteredActual = [];
140-
141-
foreach ($keysToBeConsidered as $key) {
142-
if (isset($actual[$key])) {
143-
$filteredActual[$key] = $actual[$key];
144-
}
145-
}
133+
$keysToBeConsidered = array_combine($keysToBeConsidered, $keysToBeConsidered);
134+
$expected = array_intersect_key($expected, $keysToBeConsidered);
135+
$actual = array_intersect_key($actual, $keysToBeConsidered);
146136

147-
static::assertSame($filteredExpected, $filteredActual, $message);
137+
static::assertSame($expected, $actual, $message);
148138
}
149139

150140
/**

tests/unit/Framework/AssertTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ public function testAssertArrayIsIdenticalToArrayIgnoringListOfKeysInterpretsKey
243243
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['1']);
244244
}
245245

246+
public function testAssertArrayIsEqualButNotIdenticalToArrayOnlyConsideringListOfKeys(): void
247+
{
248+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
249+
$actual = [0 => 1, 1 => 3, 'a' => 'b', 'b' => 'b'];
250+
251+
$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);
252+
253+
$this->expectException(AssertionFailedError::class);
254+
255+
$this->assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys($expected, $actual, ['a', 0]);
256+
}
257+
258+
public function testAssertArrayIsEqualButNotIdenticalToArrayIgnoringListOfKeys(): void
259+
{
260+
$expected = ['a' => 'b', 'b' => 'c', 0 => 1, 1 => 2];
261+
$actual = [0 => 1, 1 => 3, 'a' => 'b', 'b' => 'b'];
262+
263+
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);
264+
265+
$this->expectException(AssertionFailedError::class);
266+
267+
$this->assertArrayIsIdenticalToArrayIgnoringListOfKeys($expected, $actual, ['b', 1]);
268+
}
269+
246270
public function testAssertArrayHasIntegerKey(): void
247271
{
248272
$this->assertArrayHasKey(0, ['foo']);

0 commit comments

Comments
 (0)