Skip to content

Commit

Permalink
Cover with more scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed May 29, 2023
1 parent 619fded commit 8d22991
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ vendor/bin/behat -dl
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 email of user "([^"]*)" to "([^"]*)"$/
When sending :verb to ocs :url
When the response should have a status code :code
When the response should be a JSON array with the following mandatory values
When the following :appId app config is set
```

Expand All @@ -81,3 +81,20 @@ When sending "post" to ocs "/apps/libresign/api/v1/request-signature"
```

By this way you will receive on your controller method 2 values, status as integer and file as array.

## 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:
```php
protected function parseText(string $text): string {
$patterns = [
'/<SIGN_UUID>/',
'/<FILE_UUID>/',
];
$replacements = [
$this->signer['sign_uuid'] ?? null,
$this->file['uuid'] ?? $this->getFileUuidFromText($text),
];
$text = preg_replace($patterns, $replacements, $text);
return $text;
}
```
14 changes: 14 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response as MockWebServerResponse;
use GuzzleHttp\Psr7\Response;
use Libresign\NextcloudBehat\NextcloudApiContext;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -75,6 +77,18 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
}
}

/**
* @inheritDoc
*/
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$lastRequest = $this->getLastRequest();
// Mock response to be equal to body of request
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
json_encode($lastRequest->getParsedInput())
));
parent::theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues($table);
}

/**
* @inheritDoc
*/
Expand Down
56 changes: 47 additions & 9 deletions features/test.feature
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
Feature: Test this extension
Scenario: Check if all steps of this extension is working fine
Scenario: Test user
Given as user "test"
And user test exists
And sending "POST" to "/"
Then user test exists

Scenario: Test POST with success
Given sending "POST" to "/"
| status | 1 |
And the response should contain the initial state "appid-string" with the following values:

Scenario: Test response of POST is numeric
When sending "POST" to "/"
| status | 1 |
Then the response should be a JSON array with the following mandatory values
| status | 1 |

Scenario: Test response of POST is string
When sending "POST" to "/"
| status | "string" |
Then the response should be a JSON array with the following mandatory values
| status | "string" |

Scenario: Test response of POST is boolean
When sending "POST" to "/"
| status | true |
Then the response should be a JSON array with the following mandatory values
| status | true |

Scenario: Test response of POST is json
When sending "POST" to "/"
| status | (string){"string": "test"} |
Then the response should be a JSON array with the following mandatory values
| status | {"string": "test"} |

Scenario: Test initial state with string
Then the response should contain the initial state "appid-string" with the following values:
"""
default
"""
And the response should contain the initial state "appid-string" with the following values:

Scenario: Test initial state with boolean
Then the response should contain the initial state "appid-string" with the following values:
"""
true
"""
And the response should contain the initial state "appid-string" with the following values:

Scenario: Test initial state with null
Then the response should contain the initial state "appid-string" with the following values:
"""
null
"""
And the response should contain the initial state "appid-string" with the following values:

Scenario: Test initial state with empty
Then the response should contain the initial state "appid-string" with the following values:
"""
"""
And the response should contain the initial state "appid-json-object" with the following values:

Scenario: Test initial state with json
Then the response should contain the initial state "appid-json-object" with the following values:
"""
{
"fruit": "orange"
}
"""
And the response should contain the initial state "appid-json-array" with the following values:

Scenario: Test initial state with array
Then the response should contain the initial state "appid-json-array" with the following values:
"""
[
"orange"
Expand Down
6 changes: 5 additions & 1 deletion src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(Tab
$expectedValues = $table->getColumnsHash();
$realResponseArray = json_decode($this->response->getBody()->getContents(), true);
foreach ($expectedValues as $value) {
if ($this->isJson($realResponseArray[$value['key']]) || is_bool($realResponseArray[$value['key']])) {
if (is_bool($realResponseArray[$value['key']])
|| is_iterable($realResponseArray[$value['key']])
|| is_numeric($realResponseArray[$value['key']])
|| (is_string($realResponseArray[$value['key']]) && $this->isJson($realResponseArray[$value['key']]))
) {
$actualJson = json_encode($realResponseArray[$value['key']]);
Assert::assertJsonStringEqualsJsonString($value['value'], $actualJson, 'Key: ' . $value['key']);
continue;
Expand Down
12 changes: 6 additions & 6 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 8d22991

Please sign in to comment.