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

Filter using jq #57

Merged
merged 1 commit into from
Mar 14, 2024
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
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