Skip to content
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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Given /^set the display name of user "([^"]*)" to "([^"]*)"$/
Given /^set the email of user "([^"]*)" to "([^"]*)"$/
Given sending :verb to ocs :url
Given the response should have a status code :code
Given fetch field :path from prevous JSON response
Given fetch field :path from previous JSON response
Given the response should contain the initial state :name with the following values:
Given the response should contain the initial state :name json that match with:
Given the following :appId app config is set
Expand All @@ -83,7 +83,7 @@ Given sending "post" to ocs "/apps/libresign/api/v1/request-signature"
| file | {"base64":""} |
```

### Step: `fetch field :path from prevous JSON response`
### Step: `fetch field :path from previous JSON response`

If the json response is an array, you can fetch specific values using this step. The fetched values is stored to be used by other steps.

Expand Down Expand Up @@ -112,12 +112,22 @@ The alias `price` could be used in a path or body of a request:
}
"""
And sending "POST" to "/"
And fetch field "(foo)data.0.foo" from prevous JSON response
And fetch field "(foo)data.0.foo" from previous JSON response
# After fetch the field, you can use the value of field like this:
And sending "POST" to "/?foo=<foo>"
| field | <data.0.foo> |
```

#### Fetch field using jq

You can assign the return of a jq to your field using a jq like as the follow pattern:

```gherkin
And fetch field "(foo)(jq).value" from previous JSON response
```

This will retrieve a specific value from json response and assign this to your desided field.

## Parse response using jq

You can use [jq](https://jqlang.github.io/jq/manual/) expression casting to check a value in a json response body of a request. To do this you will need to install the jq command.
Expand Down Expand Up @@ -150,7 +160,7 @@ If you need to:

Implement a method `parseText` like the follow code and remember to call parent method.

This methods can works together with `fetch field :path from prevous JSON response`
This methods can works together with `fetch field :path from previous JSON response`
```php
protected function parseText(string $text): string {
$patterns = [
Expand Down
22 changes: 21 additions & 1 deletion features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,27 @@ Feature: Test this extension
}
"""
And sending "POST" to "/"
And fetch field "(FIELD_FOO)data.0.foo" from prevous JSON response
And fetch field "(FIELD_FOO)data.0.foo" from previous JSON response
# After fetch the field, you can use the value of field like this:
And sending "POST" to "/?foo=<FIELD_FOO>"
| field | <data.0.foo> |
Then the response should be a JSON array with the following mandatory values
| key | value |
| data | [{"foo":"<FIELD_FOO>"}] |

Scenario: Test get field from json response using jq
When set the response to:
"""
{
"data": [
{
"foo":"bar"
}
]
}
"""
And sending "POST" to "/"
And fetch field "(FIELD_FOO)(jq).data[0].foo" from previous JSON response
# After fetch the field, you can use the value of field like this:
And sending "POST" to "/?foo=<FIELD_FOO>"
| field | <data.0.foo> |
Expand Down
23 changes: 16 additions & 7 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,28 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
}

#[Given('fetch field :path from prevous JSON response')]
#[Given('fetch field :path from previous JSON response')]
public function fetchFieldFromPreviousJsonResponse(string $path): void {
$this->response->getBody()->seek(0);
$responseArray = json_decode($this->response->getBody()->getContents(), true);
if (preg_match('/(?<alias>\([^)]*\))(?<patch>.*)/', $path, $matches)) {
$body = $this->response->getBody()->getContents();

// Is json query
if (preg_match('/(?<alias>\([^)]*\))\(jq\)(?<path>.*)/', $path, $matches)) {
$this->fields[$matches['alias']] = $this->testAndGetActualValue(
['key' => '(jq)' . $matches['path']],
$body
);
return;
}

// Is array with alias
if (preg_match('/(?<alias>\([^)]*\)){1,}(?<path>.*)/', $path, $matches)) {
$alias = $matches['alias'];
$path = $matches['patch'];
$path = $matches['path'];
}
$keys = explode('.', $path);
$value = $responseArray;
$value = json_decode($body, true);
foreach ($keys as $key) {
$body = json_encode($responseArray);
Assert::assertIsString($body);
Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found at body: ' . $body);
$value = $value[$key];
}
Expand Down
Loading