Skip to content

Commit

Permalink
Merge pull request #65 from LibreSign/feature/match-initial-state-usi…
Browse files Browse the repository at this point in the history
…ng-jsonquery

Match initial state using jsonquery
  • Loading branch information
vitormattos authored Apr 10, 2024
2 parents 70c81d4 + 3c00b40 commit 96829df
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
28 changes: 22 additions & 6 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -191,3 +193,17 @@ Feature: Test this extension
"orange"
]
"""

Scenario: Test initial state using jq
When set the response to:
"""
<html>
<body>
<input type="hidden" id="initial-state-appid-json-array" value="WyJvcmFuZ2UiXQ==">
</body>
</html>
"""
And sending "POST" to "/"
Then the response should contain the initial state "appid-json-array" json that match with:
| key | value |
| (jq).[0] | orange |
34 changes: 31 additions & 3 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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(
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 96829df

Please sign in to comment.