Skip to content

Simplify failure description returned by PHPMatcherConstraint #226

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/PHPUnit/PHPMatcherConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions tests/PHPUnit/PHPMatcherAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand All @@ -45,10 +41,8 @@ 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:
Failed asserting that Value "bar" does not match pattern "@integer@" at path: "[foo]"
Backtrace:
#1 Matcher Coduo\PHPMatcher\Matcher matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern
#2 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (all) matching value "{"foo":"bar"}" with "{"foo":"@integer@"}" pattern
#3 Matcher Coduo\PHPMatcher\Matcher\ChainMatcher (scalars) can match pattern "{"foo":"@integer@"}"
Expand Down Expand Up @@ -150,7 +144,7 @@ public function test_it_creates_a_constraint_for_stubs() : void
* #...
* #35 Matcher Coduo\PHPMatcher\Matcher error: integer "42" is not a valid string.
*/
$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(.*)/");
$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'])
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/PHPMatcherConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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@');

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPUnit/PHPMatcherTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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']));
}

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'])
Expand Down