Skip to content
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

Fix Repeat expander in JSON Context #151

Merged
merged 6 commits into from
Apr 26, 2019
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
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