diff --git a/README.md b/README.md index c4e181d..c647282 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ 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 response should contain the initial state :name json that match with: When the following :appId app config is set ``` diff --git a/features/test.feature b/features/test.feature index 7520ecc..9483028 100644 --- a/features/test.feature +++ b/features/test.feature @@ -52,17 +52,19 @@ Feature: Test this extension """ { "Foo": { - "Bar": "33" + "Bar": "33", + "Foo": false } } """ And sending "POST" to "/" Then the response should be a JSON array with the following mandatory values - | key | value | - | Foo | (jq).Bar == "33" | - | (jq).Foo | {"Bar":"33"} | - | (jq).Foo | (jq).Bar == "33" | - | (jq).Foo.Bar | 33 | + | key | value | + | Foo | (jq).Bar == "33" | + | (jq).Foo | {"Bar":"33","Foo":false} | + | (jq).Foo | (jq).Bar == "33" | + | (jq).Foo.Bar | 33 | + | (jq).Foo.Foo | false | Scenario: Test get field from json response When set the response to: @@ -191,3 +193,17 @@ Feature: Test this extension "orange" ] """ + + Scenario: Test initial state using jq + When set the response to: + """ + +
+ + + + """ + And sending "POST" to "/" + Then the response should contain the initial state "appid-json-array" json that match with: + | key | value | + | (jq).[0] | orange | diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index b7e3180..e50d985 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -250,6 +250,11 @@ public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(Tab $expectedValues = $table->getColumnsHash(); $json = $this->response->getBody()->getContents(); $this->response->getBody()->seek(0); + $this->jsonStringMatchWith($json, $expectedValues); + } + + private function jsonStringMatchWith(string $json, array $expectedValues): void { + Assert::assertJson($json); foreach ($expectedValues as $value) { $value['key'] = $this->parseText($value['key']); $value['value'] = $this->parseText($value['value']); @@ -276,12 +281,12 @@ private function testAndGetActualValue(array $value, string $json): string { substr($value['key'], 4), $json ); - Assert::assertNotEmpty($actual, - "The follow JsonQuery returned empty value: {$value['key']} Json: $json" - ); if (!is_string($actual)) { $actual = json_encode($actual); } + Assert::assertNotEmpty($actual, + "The follow JsonQuery returned empty value: {$value['key']} Json: $json" + ); return $actual; } Assert::assertArrayHasKey( @@ -365,6 +370,29 @@ public function theResponseShouldContainTheInitialStateWithTheFollowingValues(st } } + /** + * @When the response should contain the initial state :name json that match with: + */ + public function theResponseShouldContainTheInitialStateJsonThatMatchWith(string $name, TableNode $table): void { + $this->response->getBody()->seek(0); + $html = $this->response->getBody()->getContents(); + $dom = new DOMDocument(); + // https://www.php.net/manual/en/domdocument.loadhtml.php#95463 + libxml_use_internal_errors(true); + if (empty($html) || !$dom->loadHTML($html)) { + throw new \Exception('The response is not HTML'); + } + $element = $dom->getElementById('initial-state-' . $name); + if (!$element) { + throw new \Exception('Initial state not found: '. $name); + } + $base64 = $element->getAttribute('value'); + $actual = base64_decode($base64); + $actual = $this->parseText($actual); + $expectedValues = $table->getColumnsHash(); + $this->jsonStringMatchWith($actual, $expectedValues); + } + /** * @When the following :appId app config is set *