Skip to content

#58 Added error message if JSON decode failed for both pattern and v… #85

New issue

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

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

Already on GitHub? Sign in to your account

Merged
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
24 changes: 24 additions & 0 deletions src/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function match($value, $pattern)
}

if (!Json::isValid($value)) {
$this->error = sprintf("Invalid given JSON of value. %s", $this->getErrorMessage());
return false;
}

if (!Json::isValidPattern($pattern) ) {
$this->error = sprintf("Invalid given JSON of pattern. %s", $this->getErrorMessage());
return false;
}

Expand All @@ -49,4 +55,22 @@ public function canMatch($pattern)
{
return Json::isValidPattern($pattern);
}

private function getErrorMessage()
{
switch(json_last_error()) {
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default:
return 'Unknown error';
}
}
}
20 changes: 20 additions & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ public function test_error_when_path_in_nested_value_does_not_exist()
$this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
}

public function test_error_when_json_pattern_is_invalid()
{
$value = '{"test": "value"}';
$pattern = '{"test": "@string@",}';

$this->assertFalse($this->matcher->match($value, $pattern));

$this->assertEquals($this->matcher->getError(), 'Invalid given JSON of pattern. Syntax error, malformed JSON');
}

public function test_error_when_json_value_is_invalid()
{
$value = '{"test": "value",}';
$pattern = '{"test": "@string@"}';

$this->assertFalse($this->matcher->match($value, $pattern));

$this->assertEquals($this->matcher->getError(), 'Invalid given JSON of value. Syntax error, malformed JSON');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way to add also tests for JSON_ERROR_DEPTH, JSON_ERROR_STATE_MISMATCH, JSON_ERROR_CTRL_CHAR & JSON_ERROR_UTF8 json errors??

}

public static function positivePatterns()
{
return array(
Expand Down