Skip to content

Commit

Permalink
Fix Repeat expander in JSON Context (#151)
Browse files Browse the repository at this point in the history
* Fix Repeat expander in JSON Context

Fixes #150

* Add repeat expanders tests

* Fix repeat test

* Too bad one can't use array universal key inside repeat expander

* Fix JSON in tests
  • Loading branch information
JarJak authored and norberttech committed Apr 26, 2019
1 parent 201abf8 commit 5ff1924
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/Matcher/Pattern/Expander/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ public static function is(string $name) : bool
return self::NAME === $name;
}

public function __construct(string $pattern, bool $isStrict = true)
/**
* @param array|string $pattern array to be matched or json-encoded string
*/
public function __construct($pattern, bool $isStrict = true)
{
if (!\is_string($pattern)) {
throw new \InvalidArgumentException('Repeat pattern must be a string.');
}

$this->pattern = $pattern;
$this->isStrict = $isStrict;
$this->isScalar = true;

$json = \json_decode($pattern, true);

if ($json !== null && \json_last_error() === JSON_ERROR_NONE) {
$this->pattern = $json;
if (\is_string($pattern)) {
$json = \json_decode($pattern, true);
if ($json !== null && \json_last_error() === JSON_ERROR_NONE) {
$this->pattern = $json;
$this->isScalar = false;
}
} elseif (\is_array($pattern)) {
$this->isScalar = false;
} else {
throw new \InvalidArgumentException('Repeat pattern must be a string or an array.');
}
}

Expand Down Expand Up @@ -80,11 +84,6 @@ private function matchScalar(array $values, Matcher $matcher) : bool
return true;
}

/**
* @param array $values
* @param Matcher $matcher
* @return bool
*/
private function matchJson(array $values, Matcher $matcher) : bool
{
$patternKeys = \array_keys($this->pattern);
Expand Down
9 changes: 9 additions & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public static function negativePatterns()
return [
['@string@'],
['["Norbert", '],
['@[email protected]({"name": "@string@", "value": "@array@"})']
];
}

Expand Down Expand Up @@ -215,6 +216,10 @@ public static function positiveMatches()
[
'[{"name": "Norbert","lastName":"Orzechowicz"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'[{"name": "Norbert","@*@":"@*@"},@...@]'
],
[
'[{"name": "Norbert"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'"@[email protected]({\"name\": \"@string@\"})"'
]
];
}
Expand Down Expand Up @@ -242,6 +247,10 @@ public static function negativeMatches()
'{"foo":"foo val","bar":"bar val"}',
'{"foo":"foo val"}'
],
[
'[{"name": "Norbert","lastName":"Orzechowicz"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'"@[email protected]({\"name\": \"@string@\"})"'
],
[
[],
'[]'
Expand Down

0 comments on commit 5ff1924

Please sign in to comment.