From 1ad30a97b3df2dbf79819e70f80f7d8242504111 Mon Sep 17 00:00:00 2001 From: AFS Date: Tue, 25 Feb 2025 15:34:37 +0100 Subject: [PATCH] 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. --- src/Mixins/Expectation.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index f802bc11..3a20c672 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -780,17 +780,16 @@ final class Expectation } foreach ($array as $key => $value) { + $message = ''; Assert::assertArrayHasKey($key, $valueAsArray, $message); - if ($message === '') { - $message = sprintf( - 'Failed asserting that an array has a key %s with the value %s.', - $this->export($key), - $this->export($valueAsArray[$key]), - ); - } + $second_message = $message !== '' ? $message : sprintf( + 'Failed asserting that an array has a key %s with the value %s.', + $this->export($key), + $this->export($valueAsArray[$key]), + ); - Assert::assertEquals($value, $valueAsArray[$key], $message); + Assert::assertEquals($value, $valueAsArray[$key], $second_message); } return $this; @@ -815,15 +814,14 @@ final class Expectation /* @phpstan-ignore-next-line */ $propertyValue = $this->value->{$property}; - if ($message === '') { - $message = sprintf( + $second_message = $message !== '' ? $message : sprintf( 'Failed asserting that an object has a property %s with the value %s.', $this->export($property), $this->export($propertyValue), ); } - Assert::assertEquals($value, $propertyValue, $message); + Assert::assertEquals($value, $propertyValue, $second_message); } return $this;