toMatchArray/Object wrong field fix

The functions toMatchArray and toMatchObject indicate that the wrong field is mismatching from the second loop on because the 'message' is overwritten and taken into the following loop. This patch creates a $second_message for the second test (value test) to keep the error message correct.
This commit is contained in:
AFS
2025-02-25 15:34:37 +01:00
committed by GitHub
parent 66ceb64faa
commit 1ad30a97b3

View File

@ -780,17 +780,16 @@ final class Expectation
} }
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$message = '';
Assert::assertArrayHasKey($key, $valueAsArray, $message); Assert::assertArrayHasKey($key, $valueAsArray, $message);
if ($message === '') { $second_message = $message !== '' ? $message : sprintf(
$message = sprintf(
'Failed asserting that an array has a key %s with the value %s.', 'Failed asserting that an array has a key %s with the value %s.',
$this->export($key), $this->export($key),
$this->export($valueAsArray[$key]), $this->export($valueAsArray[$key]),
); );
}
Assert::assertEquals($value, $valueAsArray[$key], $message); Assert::assertEquals($value, $valueAsArray[$key], $second_message);
} }
return $this; return $this;
@ -815,15 +814,14 @@ final class Expectation
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
$propertyValue = $this->value->{$property}; $propertyValue = $this->value->{$property};
if ($message === '') { $second_message = $message !== '' ? $message : sprintf(
$message = sprintf(
'Failed asserting that an object has a property %s with the value %s.', 'Failed asserting that an object has a property %s with the value %s.',
$this->export($property), $this->export($property),
$this->export($propertyValue), $this->export($propertyValue),
); );
} }
Assert::assertEquals($value, $propertyValue, $message); Assert::assertEquals($value, $propertyValue, $second_message);
} }
return $this; return $this;