Skip to content

Commit 144a2b9

Browse files
committedMar 1, 2021
Simplify failure description returned by PHPMatcherConstraint
1 parent 8d08946 commit 144a2b9

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed
 

‎src/PHPUnit/PHPMatcherConstraint.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Coduo\PHPMatcher\PHPUnit;
66

77
use Coduo\PHPMatcher\Backtrace;
8+
use Coduo\PHPMatcher\Backtrace\VoidBacktrace;
89
use Coduo\PHPMatcher\PHPMatcher;
910
use PHPUnit\Framework\Constraint\Constraint;
1011
use PHPUnit\Util\Json;
@@ -42,10 +43,13 @@ public function toString() : string
4243

4344
protected function failureDescription($other) : string
4445
{
45-
return parent::failureDescription($other)
46-
. "\nPattern: " . $this->exporter()->export($this->pattern)
47-
. "\nError: " . $this->matcher->error()
48-
. "\nBacktrace: \n" . $this->matcher->backtrace();
46+
$errorDescription = $this->matcher->error() ?: 'Value does not match given pattern';
47+
$backtrace = $this->matcher->backtrace();
48+
49+
return $backtrace instanceof VoidBacktrace
50+
? $errorDescription
51+
: $errorDescription
52+
. "\nBacktrace:\n" . $this->matcher->backtrace();
4953
}
5054

5155
protected function matches($value) : bool

‎tests/PHPUnit/PHPMatcherAssertionsTest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ public function test_it_throws_an_expectation_failed_exception_if_a_value_does_n
2525
} catch (\Exception $e) {
2626
$this->assertSame(
2727
<<<'ERROR'
28-
Failed asserting that '{"foo":"bar"}' matches given pattern.
29-
Pattern: '{"foo": "@integer@"}'
30-
Error: Value "bar" does not match pattern "@integer@" at path: "[foo]"
31-
Backtrace:
32-
Empty.
28+
Failed asserting that Value "bar" does not match pattern "@integer@" at path: "[foo]".
3329
ERROR,
3430
$e->getMessage()
3531
);
@@ -45,10 +41,8 @@ public function test_it_throws_an_expectation_failed_exception_if_a_value_does_n
4541
} catch (\Exception $e) {
4642
$this->assertSame(
4743
<<<ERROR
48-
Failed asserting that '{"foo":"bar"}' matches given pattern.
49-
Pattern: '{"foo": "@integer@"}'
50-
Error: Value "bar" does not match pattern "@integer@" at path: "[foo]"
51-
Backtrace:
44+
Failed asserting that Value "bar" does not match pattern "@integer@" at path: "[foo]"
45+
Backtrace:
5246
#1 Matcher Coduo\PHPMatcher\Matcher matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern
5347
#2 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (all) matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern
5448
#3 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (scalars) can match pattern "{"foo":"@integer@"}"
@@ -150,7 +144,7 @@ public function test_it_creates_a_constraint_for_stubs() : void
150144
* #...
151145
* #35 Matcher Coduo\PHPMatcher\Matcher error: integer "42" is not a valid string.
152146
*/
153-
$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 42 matches given pattern.\nPattern: '@string@'\nError: integer \"42\" is not a valid string.\nBacktrace: \n(.*)/");
147+
$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../");
154148

155149
$mock = $this->getMockBuilder('stdClass')
156150
->setMethods(['getTitle'])

‎tests/PHPUnit/PHPMatcherConstraintTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function test_it_returns_false_if_a_value_does_not_match_the_pattern() :
3434
public function test_it_sets_a_failure_description_if_not_given() : void
3535
{
3636
$this->expectException(AssertionFailedError::class);
37-
$this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/');
37+
$this->expectExceptionMessageMatches('/Failed asserting that integer "42" is not a valid string../');
3838

3939
$constraint = new PHPMatcherConstraint('@string@');
4040

‎tests/PHPUnit/PHPMatcherTestCaseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public function test_it_asserts_if_a_value_matches_the_pattern() : void
1717
public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern() : void
1818
{
1919
$this->expectException(AssertionFailedError::class);
20-
$this->expectExceptionMessageMatches("/Failed asserting that '{\"foo\":\"bar\"}' matches given pattern(.*)/");
20+
$this->expectExceptionMessageMatches('/Failed asserting that Value "bar" does not match pattern "@integer@" at path: "\\[foo\\]"./');
2121

2222
$this->assertMatchesPattern('{"foo": "@integer@"}', \json_encode(['foo' => 'bar']));
2323
}
2424

2525
public function test_it_creates_a_constraint_for_stubs() : void
2626
{
2727
$this->expectException(AssertionFailedError::class);
28-
$this->expectExceptionMessageMatches('/Failed asserting that 42 matches given pattern(.*)/');
28+
$this->expectExceptionMessageMatches('/Failed asserting that integer "42" is not a valid string../');
2929

3030
$mock = $this->getMockBuilder('stdClass')
3131
->setMethods(['getTitle'])

0 commit comments

Comments
 (0)