From 1ad30a97b3df2dbf79819e70f80f7d8242504111 Mon Sep 17 00:00:00 2001 From: AFS Date: Tue, 25 Feb 2025 15:34:37 +0100 Subject: [PATCH 1/3] 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; From 0bdaef29e97fbc450ffee1308b1fd4d0665ec570 Mon Sep 17 00:00:00 2001 From: AFS Date: Tue, 25 Feb 2025 15:41:17 +0100 Subject: [PATCH 2/3] Fix lingering } Remove remaining } in toMatchObject. --- src/Mixins/Expectation.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 3a20c672..e3393396 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -815,11 +815,10 @@ final class Expectation $propertyValue = $this->value->{$property}; $second_message = $message !== '' ? $message : sprintf( - 'Failed asserting that an object has a property %s with the value %s.', - $this->export($property), - $this->export($propertyValue), - ); - } + 'Failed asserting that an object has a property %s with the value %s.', + $this->export($property), + $this->export($propertyValue), + ); Assert::assertEquals($value, $propertyValue, $second_message); } From 2c3a53f6cd198bd62b2d0984e3e3f79f7c1d69b7 Mon Sep 17 00:00:00 2001 From: AFS Date: Tue, 25 Feb 2025 15:44:32 +0100 Subject: [PATCH 3/3] Remove reset of message Reset approach was not the right one. --- src/Mixins/Expectation.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index e3393396..7bb7787c 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -780,7 +780,6 @@ final class Expectation } foreach ($array as $key => $value) { - $message = ''; Assert::assertArrayHasKey($key, $valueAsArray, $message); $second_message = $message !== '' ? $message : sprintf(