Skip to content

Commit 963efc7

Browse files
authored
Do not return any error when matching was successful (#162)
* Do not return any error when matching was successful * CS Fixes * Fix failing tests on old php version * Ops, forgot to remove one void return type :P
1 parent fde5b29 commit 963efc7

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/Matcher.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ public function __construct(ValueMatcher $matcher)
1717

1818
public function match($value, $pattern) : bool
1919
{
20-
return $this->matcher->match($value, $pattern);
20+
$result = $this->matcher->match($value, $pattern);
21+
22+
if ($result === true) {
23+
$this->matcher->clearError();
24+
}
25+
26+
return $result;
2127
}
2228

2329
/**

src/Matcher/Matcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function match($value, $pattern) : bool
2727

2828
return false;
2929
}
30+
31+
public function clearError()
32+
{
33+
$this->error = null;
34+
}
3035
}

src/Matcher/ValueMatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function canMatch($pattern) : bool;
2222
* @return null|string
2323
*/
2424
public function getError();
25+
26+
/**
27+
* Clear last error
28+
*/
29+
public function clearError();
2530
}

tests/Matcher/JsonMatcherTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ public function test_error_when_json_pattern_is_invalid()
126126
$this->assertEquals($this->matcher->getError(), 'Invalid given JSON of pattern. Syntax error, malformed JSON');
127127
}
128128

129+
/**
130+
* Solves https://github.com/coduo/php-matcher/issues/156
131+
*/
132+
public function test_empty_error_after_successful_match()
133+
{
134+
$this->assertTrue($this->matcher->match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}'));
135+
$this->assertNull($this->matcher->getError());
136+
}
137+
129138
public function test_error_when_json_value_is_invalid()
130139
{
131140
$value = '{"test": "value",}';

tests/MatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function test_matcher_with_array_value()
6262
'data' => '@wildcard@',
6363
];
6464

65-
$this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError());
65+
$this->assertTrue($this->matcher->match($value, $expectation), (string) $this->matcher->getError());
6666
$this->assertTrue(PHPMatcher::match($value, $expectation, $error), (string) $error);
6767
}
6868

0 commit comments

Comments
 (0)