diff --git a/src/PHPUnit/PHPMatcherConstraint.php b/src/PHPUnit/PHPMatcherConstraint.php index 15f02f3d..be296f22 100644 --- a/src/PHPUnit/PHPMatcherConstraint.php +++ b/src/PHPUnit/PHPMatcherConstraint.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\PHPUnit; use Coduo\PHPMatcher\Backtrace; +use Coduo\PHPMatcher\Backtrace\VoidBacktrace; use Coduo\PHPMatcher\PHPMatcher; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Util\Json; @@ -42,10 +43,13 @@ public function toString() : string protected function failureDescription($other) : string { - return parent::failureDescription($other) - . "\nPattern: " . $this->exporter()->export($this->pattern) - . "\nError: " . $this->matcher->error() - . "\nBacktrace: \n" . $this->matcher->backtrace(); + $errorDescription = $this->matcher->error() ?: 'Value does not match given pattern'; + $backtrace = $this->matcher->backtrace(); + + return $backtrace instanceof VoidBacktrace + ? $errorDescription + : $errorDescription + . "\nBacktrace:\n" . $this->matcher->backtrace(); } protected function matches($value) : bool diff --git a/tests/PHPUnit/PHPMatcherAssertionsTest.php b/tests/PHPUnit/PHPMatcherAssertionsTest.php index 956bc12c..553d9cec 100644 --- a/tests/PHPUnit/PHPMatcherAssertionsTest.php +++ b/tests/PHPUnit/PHPMatcherAssertionsTest.php @@ -25,11 +25,7 @@ public function test_it_throws_an_expectation_failed_exception_if_a_value_does_n } catch (\Exception $e) { $this->assertSame( <<<'ERROR' -Failed asserting that '{"foo":"bar"}' matches given pattern. -Pattern: '{"foo": "@integer@"}' -Error: Value "bar" does not match pattern "@integer@" at path: "[foo]" -Backtrace: -Empty. +Failed asserting that Value "bar" does not match pattern "@integer@" at path: "[foo]". ERROR, $e->getMessage() ); @@ -45,10 +41,8 @@ public function test_it_throws_an_expectation_failed_exception_if_a_value_does_n } catch (\Exception $e) { $this->assertSame( <<expectExceptionMessageMatches("/Expectation failed for method name is \"getTitle\" when invoked zero or more times\nParameter 0 for invocation stdClass::getTitle\(42\) does not match expected value.\nFailed asserting that 42 matches given pattern.\nPattern: '@string@'\nError: integer \"42\" is not a valid string.\nBacktrace: \n(.*)/"); + $this->expectExceptionMessageMatches("/Expectation failed for method name is \"getTitle\" when invoked zero or more times\nParameter 0 for invocation stdClass::getTitle\(42\) does not match expected value.\nFailed asserting that integer \"42\" is not a valid string../"); $mock = $this->getMockBuilder('stdClass') ->setMethods(['getTitle']) diff --git a/tests/PHPUnit/PHPMatcherConstraintTest.php b/tests/PHPUnit/PHPMatcherConstraintTest.php index b639f9ab..bf18cf89 100644 --- a/tests/PHPUnit/PHPMatcherConstraintTest.php +++ b/tests/PHPUnit/PHPMatcherConstraintTest.php @@ -34,7 +34,7 @@ public function test_it_returns_false_if_a_value_does_not_match_the_pattern() : public function test_it_sets_a_failure_description_if_not_given() : void { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/'); + $this->expectExceptionMessageMatches('/Failed asserting that integer "42" is not a valid string../'); $constraint = new PHPMatcherConstraint('@string@'); diff --git a/tests/PHPUnit/PHPMatcherTestCaseTest.php b/tests/PHPUnit/PHPMatcherTestCaseTest.php index 152aeb36..7b7b231d 100644 --- a/tests/PHPUnit/PHPMatcherTestCaseTest.php +++ b/tests/PHPUnit/PHPMatcherTestCaseTest.php @@ -17,7 +17,7 @@ public function test_it_asserts_if_a_value_matches_the_pattern() : void public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern() : void { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessageMatches("/Failed asserting that '{\"foo\":\"bar\"}' matches given pattern(.*)/"); + $this->expectExceptionMessageMatches('/Failed asserting that Value "bar" does not match pattern "@integer@" at path: "\\[foo\\]"./'); $this->assertMatchesPattern('{"foo": "@integer@"}', \json_encode(['foo' => 'bar'])); } @@ -25,7 +25,7 @@ public function test_it_throws_an_expectation_failed_exception_if_a_value_does_n public function test_it_creates_a_constraint_for_stubs() : void { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/'); + $this->expectExceptionMessageMatches('/Failed asserting that integer "42" is not a valid string../'); $mock = $this->getMockBuilder('stdClass') ->setMethods(['getTitle'])