From 44be5f0f0909f015d76ad3378c18c116989251e6 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Fri, 22 Mar 2024 12:48:41 -0300 Subject: [PATCH] Add alias when fetch field from response Signed-off-by: Vitor Mattos --- README.md | 6 ++++-- features/test.feature | 4 ++-- src/NextcloudApiContext.php | 7 +++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd3ccfd..707c855 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ To send a value as array, you can set a json string and the json string will be ```gherkin When sending "post" to ocs "/apps/libresign/api/v1/request-signature" | status | 1 | - | file | {"base64":""} | + | file | {"base64":""} | ``` ## Parse response using jq @@ -112,7 +112,9 @@ 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: +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` ```php protected function parseText(string $text): string { $patterns = [ diff --git a/features/test.feature b/features/test.feature index 9632b9a..8ce3fb5 100644 --- a/features/test.feature +++ b/features/test.feature @@ -76,9 +76,9 @@ Feature: Test this extension } """ And sending "POST" to "/" - And fetch field "data.0.foo" from prevous JSON response + And fetch field "(foo)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=" + And sending "POST" to "/?foo=" | field | | Scenario: Test initial state with string diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index 564379e..7b6d1f6 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -316,12 +316,19 @@ private function validateAsJsonQuery(string $expected, string $actual): void { public function fetchFieldFromPreviousJsonResponse(string $path): void { $this->response->getBody()->seek(0); $responseArray = json_decode($this->response->getBody()->getContents(), true); + if (preg_match('/(?\([^)]*\))(?.*)/', $path, $matches)) { + $alias = $matches['alias']; + $path = $matches['patch']; + } $keys = explode('.', $path); $value = $responseArray; foreach ($keys as $key) { Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found.'); $value = $value[$key]; } + if (isset($alias)) { + $this->fields[$alias] = $value; + } $this->fields[$path] = $value; }