Skip to content

Commit

Permalink
Fixed json matcher when matching strings including other patterns (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Nov 24, 2019
1 parent 26f53fc commit c7b3eb0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function match($value, $pattern) : bool
return false;
}

$transformedPattern = Json::transformPattern($pattern);
$transformedPattern = Json::isValid($pattern) ? $pattern : Json::transformPattern($pattern);
$match = $this->matcher->match(\json_decode($value, true), \json_decode($transformedPattern, true));

if (!$match) {
$this->error = $this->matcher->getError();
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Assert/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function isValidPattern($value) : bool
return false;
}

return self::isValid(self::transformPattern($value));
return self::isValid($value) || self::isValid(self::transformPattern($value));
}

public static function transformPattern(string $pattern) : string
Expand Down
1 change: 1 addition & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static function positivePatterns()
[\json_encode(['Norbert', 'Michał'])],
[\json_encode(['Norbert', '@string@'])],
[\json_encode('test')],
[\json_encode(['foo' => '/foo/@uuid@/bar'])],
];
}

Expand Down
6 changes: 6 additions & 0 deletions tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public function jsonDataProvider()
"nextPage": "@string@"
}',
],
'matches with txt matcher ' => [
/** @lang JSON */
'{"foo": "/foo/92ff9a6c-7fbd-47e5-b550-90b77e3284c7/bar"}',
/** @lang JSON */
'{"foo": "/foo/@uuid@/bar"}'
],
'matches json values with full text matcher' => [
/** @lang JSON */
'{
Expand Down

0 comments on commit c7b3eb0

Please sign in to comment.