Skip to content
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
8 changes: 7 additions & 1 deletion src/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public function __construct(ValueMatcher $matcher)

public function match($value, $pattern) : bool
{
return $this->matcher->match($value, $pattern);
$result = $this->matcher->match($value, $pattern);

if ($result === true) {
$this->matcher->clearError();
}

return $result;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function match($value, $pattern) : bool

return false;
}

public function clearError()
{
$this->error = null;
}
}
5 changes: 5 additions & 0 deletions src/Matcher/ValueMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function canMatch($pattern) : bool;
* @return null|string
*/
public function getError();

/**
* Clear last error
*/
public function clearError();
}
9 changes: 9 additions & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public function test_error_when_json_pattern_is_invalid()
$this->assertEquals($this->matcher->getError(), 'Invalid given JSON of pattern. Syntax error, malformed JSON');
}

/**
* Solves https://github.com/coduo/php-matcher/issues/156
*/
public function test_empty_error_after_successful_match()
{
$this->assertTrue($this->matcher->match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}'));
$this->assertNull($this->matcher->getError());
}

public function test_error_when_json_value_is_invalid()
{
$value = '{"test": "value",}';
Expand Down
2 changes: 1 addition & 1 deletion tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_matcher_with_array_value()
'data' => '@wildcard@',
];

$this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError());
$this->assertTrue($this->matcher->match($value, $expectation), (string) $this->matcher->getError());
$this->assertTrue(PHPMatcher::match($value, $expectation, $error), (string) $error);
}

Expand Down