Skip to content

Commit

Permalink
Fixes #28: Be more accepting of valid json (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson authored Feb 28, 2019
1 parent 8c2e940 commit 7e76331
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 56 deletions.
22 changes: 11 additions & 11 deletions .scenarios.lock/phpunit5/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 42 additions & 40 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions src/ProcessBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,49 @@ public function getOutputAsJson()
if (empty($output)) {
throw new \InvalidArgumentException('Output is empty.');
}
$output = preg_replace('#^[^{]*#', '', $output);
$output = preg_replace('#[^}]*$#', '', $output);
if (Escape::isWindows()) {
// Doubled double quotes were converted to \\".
// Revert to double quote.
$output = str_replace('\\"', '"', $output);
// Revert of doubled backslashes.
$output = preg_replace('#\\\\{2}#', '\\', $output);
}
if (!$json = json_decode($output, true)) {
$output = $this->removeNonJsonJunk($output);
$json = json_decode($output, true);
if (!isset($json)) {
throw new \InvalidArgumentException('Unable to decode output into JSON.');
}
return $json;
}

/**
* Allow for a certain amount of resiliancy in the output received when
* json is expected.
*
* @param string $data
* @return string
*/
protected function removeNonJsonJunk($data)
{
// Exit early if we have no output.
$data = trim($data);
if (empty($data)) {
return $data;
}
// If the data is a simple quoted string, or an array, then exit.
if ((($data[0] == '"') && ($data[strlen($data) - 1] == '"')) ||
(($data[0] == "[") && ($data[strlen($data) - 1] == "]"))
) {
return $data;
}
// If the json is not a simple string or a simple array, then is must
// be an associative array. We will remove non-json garbage characters
// before and after the enclosing curley-braces.
$data = preg_replace('#^[^{]*#', '', $data);
$data = preg_replace('#[^}]*$#', '', $data);
return $data;
}

/**
* Return a realTime output object.
*
Expand Down
19 changes: 17 additions & 2 deletions tests/SiteProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public function siteProcessJsonTestValues()
'Ignored leading data {"foo":"bar"} Ignored trailing data',
NULL,
],
[
'["a","b","c"]',
'["a", "b", "c"]',
NULL,
],
[
'"string"',
'"string"',
NULL,
],
[
'[]',
'[]',
NULL,
],
];
}

Expand All @@ -248,8 +263,8 @@ public function testSiteProcessJson(
$data,
$os)
{
if (Escape::isWindows() != Escape::isWindows($os)) {
$this->markTestSkipped("OS isn't supported");
if (Escape::isWindows()) {
$this->markTestSkipped("Windows is not working yet. PRs welcome.");
}
$args = ['echo', $data];
$processManager = ProcessManager::createDefault();
Expand Down

0 comments on commit 7e76331

Please sign in to comment.