Skip to content

Commit

Permalink
Merge pull request #57 from LibreSign/feature/filter-using-jq
Browse files Browse the repository at this point in the history
Filter using jq
  • Loading branch information
vitormattos authored Mar 14, 2024
2 parents 173dda2 + 9e2c04e commit a78f0b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ When set the response to:
"""
And sending "POST" to "/"
Then the response should be a JSON array with the following mandatory values
| key | value |
| Foo | (jq).Bar == "33" |
| key | value |
| Foo | (jq).Bar == "33" |
| (jq).Foo | {"Bar":"33"} |
| (jq).Foo | (jq).Bar == "33" |
| (jq).Foo.Bar | 33 |
```

## Parse initial state
Expand Down
7 changes: 5 additions & 2 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ Feature: Test this extension
"""
And sending "POST" to "/"
Then the response should be a JSON array with the following mandatory values
| key | value |
| Foo | (jq).Bar == "33" |
| key | value |
| Foo | (jq).Bar == "33" |
| (jq).Foo | {"Bar":"33"} |
| (jq).Foo | (jq).Bar == "33" |
| (jq).Foo.Bar | 33 |

Scenario: Test initial state with string
When set the response to:
Expand Down
56 changes: 41 additions & 15 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,34 +245,60 @@ public function theResponseShouldHaveStatusCode($code): void {
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$this->response->getBody()->seek(0);
$expectedValues = $table->getColumnsHash();
$realResponseArray = json_decode($this->response->getBody()->getContents(), true);
$json = $this->response->getBody()->getContents();
$this->response->getBody()->seek(0);
Assert::assertIsArray($realResponseArray, 'The response is not a JSON array: ' . $this->response->getBody()->getContents());
foreach ($expectedValues as $value) {
Assert::assertArrayHasKey(
$value['key'],
$realResponseArray,
'Not found: "' . $value['key'] . '" at array: ' . json_encode($realResponseArray)
);
$actual = $realResponseArray[$value['key']];
$actual = $this->testAndGetActualValue($value, $json);
// Test actual value
if (str_starts_with($value['value'], '(jq)')) {
$expected = substr($value['value'], 4);
$this->validateAsJsonQuery($expected, json_encode($actual));
$this->validateAsJsonQuery($expected, $actual);
continue;
}
if (is_bool($realResponseArray[$value['key']])
|| is_iterable($realResponseArray[$value['key']])
|| is_numeric($realResponseArray[$value['key']])
|| (is_string($realResponseArray[$value['key']]) && $this->isJson($realResponseArray[$value['key']]))
) {
$actual = json_encode($actual);
if ($this->isJson($actual)) {
Assert::assertJsonStringEqualsJsonString($value['value'], $actual, 'Key: ' . $value['key']);
continue;
}
Assert::assertEquals($value['value'], $actual, 'Key: ' . $value['key']);
}
}

private function testAndGetActualValue(array $value, string $json): string {
$realResponseArray = json_decode($json, true);
Assert::assertIsArray($realResponseArray, 'The response is not a JSON array: ' . $json);
if (str_starts_with($value['key'], '(jq)')) {
$actual = $this->evalJsonQuery(
substr($value['key'], 4),
$json
);
Assert::assertNotEmpty($actual,
"The follow JsonQuery returned empty value: " . $value['key']
);
if (!is_string($actual)) {
$actual = json_encode($actual);
}
return $actual;
}
Assert::assertArrayHasKey(
$value['key'],
$realResponseArray,
'Not found: "' . $value['key'] . '" at array: ' . json_encode($realResponseArray)
);
$actual = $realResponseArray[$value['key']];
if (!is_string($actual)) {
$actual = json_encode($actual);
}
return $actual;
}

/**
* @return mixed
*/
private function evalJsonQuery(string $jsonQuery, string $target) {
$jq = \JsonQueryWrapper\JsonQueryFactory::createWith($target);
return $jq->run($jsonQuery);
}

private function validateAsJsonQuery(string $expected, string $actual): void {
if (!`which jq`) {
throw new \InvalidArgumentException('Is necessary install the jq command to use jq');
Expand Down

0 comments on commit a78f0b4

Please sign in to comment.