Skip to content

Commit

Permalink
Merge pull request #59 from LibreSign/feature/get-path-from-json-resp…
Browse files Browse the repository at this point in the history
…onse

Get path from json response
  • Loading branch information
vitormattos authored Mar 22, 2024
2 parents f194953 + f63fee2 commit 172923c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ When as user :user
When user :user exists
When sending :verb to :url
When the response should be a JSON array with the following mandatory values
When the response should contain the initial state :name with the following values:
When /^set the display name of user "([^"]*)" to "([^"]*)"$/
When /^set the email of user "([^"]*)" to "([^"]*)"$/
When sending :verb to ocs :url
When the response should have a status code :code
When fetch field :path from prevous JSON response
When the response should contain the initial state :name with the following values:
When the following :appId app config is set
```

Expand Down Expand Up @@ -105,8 +106,13 @@ Then the response should be a JSON array with the following mandatory values
| (jq).Foo.Bar | 33 |
```

## Parse initial state
If you need to parse the initial state to use placeholder or get any value from current initial state, implement a method `parseText` like this:
## Parse text

If you need to:
- Get values from a request, store and use in other request
- Parse the response of a request

Implement a method `parseText` like the follow code and remember to call parent method:
```php
protected function parseText(string $text): string {
$patterns = [
Expand All @@ -118,6 +124,8 @@ protected function parseText(string $text): string {
$this->file['uuid'] ?? $this->getFileUuidFromText($text),
];
$text = preg_replace($patterns, $replacements, $text);
$text = parent::parseText($text);
return $text;
}
```
For more information about parseText, check the scenario `Test get field from json response`
1 change: 1 addition & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function sendRequest(string $verb, string $url, $body = null, array $head

// Url
$actual = preg_replace('/^\/index.php/', '', $lastRequest->getRequestUri());
$url = $this->parseText($url);
Assert::assertEquals($url, $actual);

// Headers
Expand Down
17 changes: 17 additions & 0 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ Feature: Test this extension
| (jq).Foo | (jq).Bar == "33" |
| (jq).Foo.Bar | 33 |

Scenario: Test get field from json response
When set the response to:
"""
{
"data": [
{
"foo":"bar"
}
]
}
"""
And sending "POST" to "/"
And fetch field "data.0.foo" from prevous JSON response
# After fetch the field, you can use the value of field like this:
And sending "POST" to "/?foo=<data.0.foo>"
| field | <data.0.foo> |

Scenario: Test initial state with string
When set the response to:
"""
Expand Down
46 changes: 45 additions & 1 deletion src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class NextcloudApiContext implements Context {
protected string $baseUrl;
protected RunServerListener $server;
protected string $currentUser = '';
protected array $fields = [];
/**
* @var string[]
*/
Expand Down Expand Up @@ -180,8 +181,8 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
}

try {
$this->requestOptions = $options;
list($fullUrl, $options) = $this->beforeRequest($fullUrl, $options);
$this->requestOptions = $options;
$this->response = $client->{$verb}($fullUrl, $options);
} catch (ClientException $ex) {
$this->response = $ex->getResponse();
Expand All @@ -191,6 +192,8 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
}

protected function beforeRequest(string $fullUrl, array $options): array {
$options = $this->parseFormParams($options);
$fullUrl = $this->parseText($fullUrl);
return [$fullUrl, $options];
}

Expand Down Expand Up @@ -307,6 +310,21 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
}

/**
* @When fetch field :path from prevous JSON response
*/
public function fetchFieldFromPreviousJsonResponse(string $path): void {
$this->response->getBody()->seek(0);
$responseArray = json_decode($this->response->getBody()->getContents(), true);
$keys = explode('.', $path);
$value = $responseArray;
foreach ($keys as $key) {
Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found.');
$value = $value[$key];
}
$this->fields[$path] = $value;
}

/**
* @When the response should contain the initial state :name with the following values:
*/
Expand Down Expand Up @@ -354,7 +372,33 @@ public function setAppConfig(string $appId, TableNode $formData): void {
$this->setCurrentUser($currentUser);
}

protected function parseFormParams(array $options): array {
if (!empty($options['form_params'])) {
$this->parseTextRcursive($options['form_params']);
}
return $options;
}

private function parseTextRcursive(array &$array): array {
array_walk_recursive($array, function (mixed &$value) {
if (is_string($value)) {
$value = $this->parseText($value);
} elseif ($value instanceof \stdClass) {
$value = (array) $value;
$value = json_decode(json_encode($this->parseTextRcursive($value)));
}
});
return $array;
}

protected function parseText(string $text): string {
$patterns = [];
$replacements = [];
foreach ($this->fields as $key => $value) {
$patterns[] = '/<' . $key . '>/';
$replacements[] = $value;
}
$text = preg_replace($patterns, $replacements, $text);
return $text;
}

Expand Down
28 changes: 14 additions & 14 deletions vendor-bin/psalm/composer.lock

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

0 comments on commit 172923c

Please sign in to comment.