From fb7ccb85dddacf613e5b047a546171019aa06585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Gon=C3=A7alves?= Date: Tue, 15 Sep 2026 17:26:17 +0100 Subject: [PATCH] Fix exception message assertions on PHPUnit 13.2 and later ConfigurationValuesAreInvalidConstraint passed the caught InvalidConfigurationException object to PHPUnit's exception message constraints. Up to PHPUnit 13.1 those constraints string-cast whatever they were given, so passing the exception happened to work, because Throwable::__toString() contains the message. PHPUnit 13.2 (sebastianbergmann/phpunit#6559) tightened ExceptionMessageIsOrContains, ExceptionMessageIs and ExceptionMessageMatchesRegularExpression to accept only the message string, so every assertion carrying an expected message now fails with "Failed asserting that exception message '' contains '...'", whatever the actual message is. Four tests in this suite are affected. Pass the message instead, which is what PHPUnit itself feeds these constraints. This behaves identically on PHPUnit 10.5 through 13.1, where the constraints string-cast their input, and it also repairs the empty expected message branch, which could never match while an object was passed. Co-Authored-By: Claude Opus 5 (1M context) --- ...onfigurationValuesAreInvalidConstraint.php | 2 +- ...gurationValuesAreInvalidConstraintTest.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/PhpUnit/ConfigurationValuesAreInvalidConstraint.php b/PhpUnit/ConfigurationValuesAreInvalidConstraint.php index 14714f4..849995e 100644 --- a/PhpUnit/ConfigurationValuesAreInvalidConstraint.php +++ b/PhpUnit/ConfigurationValuesAreInvalidConstraint.php @@ -65,7 +65,7 @@ private function evaluateException(\Exception $exception, $description, $returnR } return $this->createPhpUnitConstraint() - ->evaluate($exception, $description, $returnResult); + ->evaluate($exception->getMessage(), $description, $returnResult); } private function createPhpUnitConstraint() diff --git a/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php b/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php index 9ae1237..5edce39 100644 --- a/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php +++ b/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php @@ -6,6 +6,7 @@ use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\AlwaysValidConfiguration; use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; class ConfigurationValuesAreInvalidConstraintTest extends TestCase @@ -82,6 +83,25 @@ public function if_configuration_values_are_invalid_it_matches_when_exception_me $this->assertTrue($constraint->evaluate([[]], '', true)); } + #[Test] + public function the_failure_message_contains_the_actual_exception_message() + { + $constraint = new ConfigurationValuesAreInvalidConstraint( + new ConfigurationWithRequiredValue(), + 'expected message which will not be part of the actual message' + ); + + try { + $constraint->evaluate([[]]); + } catch (ExpectationFailedException $exception) { + $this->assertStringContainsString('required_value', $exception->getMessage()); + + return; + } + + $this->fail('Expected an ExpectationFailedException to be thrown'); + } + #[Test] public function to_string_returns_a_message() {