Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ vendor/bin/behat -dl
```

```gherkin
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 /^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 response should contain the initial state :name json that match with:
When the following :appId app config is set
Given as user :user
Given user :user exists
Given sending :verb to :url
Given the response should be a JSON array with the following mandatory values
Given /^set the display name of user "([^"]*)" to "([^"]*)"$/
Given /^set the email of user "([^"]*)" to "([^"]*)"$/
Given sending :verb to ocs :url
Given the response should have a status code :code
Given fetch field :path from prevous JSON response
Given the response should contain the initial state :name with the following values:
Given the response should contain the initial state :name json that match with:
Given the following :appId app config is set
```

## Tips
Expand All @@ -69,7 +69,7 @@ To send a json value as string, prefix the json string with (string)

**Example**:
```gherkin
When sending "post" to ocs "/apps/provisioning_api/api/v1/config/apps/appname/propertyname"
Given sending "post" to ocs "/apps/provisioning_api/api/v1/config/apps/appname/propertyname"
| value | (string){"enabled":true} |
```

Expand All @@ -78,7 +78,7 @@ To send a value as array, you can set a json string and the json string will be

**Example**:
```gherkin
When sending "post" to ocs "/apps/libresign/api/v1/request-signature"
Given sending "post" to ocs "/apps/libresign/api/v1/request-signature"
| status | 1 |
| file | {"base64":""} |
```
Expand Down Expand Up @@ -125,7 +125,7 @@ You can use [jq](https://jqlang.github.io/jq/manual/) expression casting to chec
Example:

```gherkin
When set the response to:
Given set the response to:
"""
{
"Foo": {
Expand Down
5 changes: 2 additions & 3 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Step\Given;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response as MockWebServerResponse;
Expand Down Expand Up @@ -78,9 +79,7 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
}
}

/**
* @when set the response to:
*/
#[Given('set the response to:')]
public function setTheResponseTo(PyStringNode $response): void {
// Mock response to be equal to body of request
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
Expand Down
65 changes: 20 additions & 45 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Hook\AfterScenario;
use Behat\Hook\BeforeScenario;
use Behat\Hook\BeforeSuite;
use Behat\Step\Given;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use DOMDocument;
use Exception;
Expand Down Expand Up @@ -48,9 +52,7 @@ public function __construct(?array $parameters = []) {
}
}

/**
* @BeforeSuite
*/
#[BeforeSuite()]
public static function beforeSuite(BeforeSuiteScope $scope):void {
$whoami = (string) exec('whoami');
if (get_current_user() !== $whoami) {
Expand All @@ -63,25 +65,17 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
}
}

/**
* @BeforeScenario
*/
#[BeforeScenario()]
public function setUp(): void {
$this->createdUsers = [];
}

/**
* @When as user :user
* @param string $user
*/
#[Given('as user :user')]
public function setCurrentUser(string $user): void {
$this->currentUser = $user;
}

/**
* @When user :user exists
* @param string $user
*/
#[Given('user :user exists')]
public function assureUserExists(string $user): void {
$response = $this->userExists($user);
if ($response->getStatusCode() !== 200) {
Expand Down Expand Up @@ -121,7 +115,7 @@ protected function createUser(string $user): void {
$this->setCurrentUser($currentUser);
}

/** @When /^set the display name of user "([^"]*)" to "([^"]*)"$/ */
#[Given('/^set the display name of user "([^"]*)" to "([^"]*)"$/')]
public function setUserDisplayName(string $user, ?string $displayName = null): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
Expand All @@ -133,7 +127,7 @@ public function setUserDisplayName(string $user, ?string $displayName = null): v
$this->setCurrentUser($currentUser);
}

/** @When /^set the email of user "([^"]*)" to "([^"]*)"$/ */
#[Given('/^set the email of user "([^"]*)" to "([^"]*)"$/')]
public function setUserEmail(string $user, string $email): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
Expand All @@ -145,24 +139,24 @@ public function setUserEmail(string $user, string $email): void {
}

/**
* @When sending :verb to ocs :url
* @param string $verb
* @param string $url
* @param TableNode|array|null $body
*/
#[Given('sending :verb to ocs :url')]
public function sendOCSRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
$url = '/ocs/v2.php' . $url;
$headers['OCS-ApiRequest'] = 'true';
$this->sendRequest($verb, $url, $body, $headers, $options);
}

/**
* @When sending :verb to :url
* @param string $verb
* @param string $url
* @param TableNode|array|null $body
* @param array $headers
*/
#[Given('sending :verb to :url')]
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
if (!str_starts_with($url, '/')) {
$url = '/' . $url;
Expand Down Expand Up @@ -238,30 +232,23 @@ protected function getUserCookieJar(string $user): CookieJar {
return $this->cookieJars[$user];
}

/**
* @param ResponseInterface $response
* @param int $statusCode
* @param string $message
*/
protected function assertStatusCode(ResponseInterface $response, int $statusCode, string $message = ''): void {
Assert::assertEquals($statusCode, $response->getStatusCode(), $message);
}

/**
* @When the response should have a status code :code
* @param string $code
* @throws \InvalidArgumentException
*/
public function theResponseShouldHaveStatusCode($code): void {
#[Given('the response should have a status code :code')]
public function theResponseShouldHaveStatusCode(string $code): void {
$currentCode = $this->response->getStatusCode();
Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents());
}

/**
* @When the response should be a JSON array with the following mandatory values
* @param TableNode $table
* @throws \InvalidArgumentException
*/
#[Given('the response should be a JSON array with the following mandatory values')]
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$this->response->getBody()->seek(0);
$expectedValues = $table->getColumnsHash();
Expand Down Expand Up @@ -335,9 +322,7 @@ 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
*/
#[Given('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);
Expand All @@ -359,9 +344,7 @@ public function fetchFieldFromPreviousJsonResponse(string $path): void {
$this->fields[$path] = $value;
}

/**
* @When the response should contain the initial state :name with the following values:
*/
#[Given('the response should contain the initial state :name with the following values:')]
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, PyStringNode $expected): void {
$this->response->getBody()->seek(0);
$html = $this->response->getBody()->getContents();
Expand Down Expand Up @@ -390,9 +373,7 @@ public function theResponseShouldContainTheInitialStateWithTheFollowingValues(st
}
}

/**
* @When the response should contain the initial state :name json that match with:
*/
#[Given('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();
Expand All @@ -413,11 +394,7 @@ public function theResponseShouldContainTheInitialStateJsonThatMatchWith(string
$this->jsonStringMatchWith($actual, $expectedValues);
}

/**
* @When the following :appId app config is set
*
* @param TableNode $formData
*/
#[Given('the following :appId app config is set')]
public function setAppConfig(string $appId, TableNode $formData): void {
$currentUser = $this->currentUser;
$this->setCurrentUser('admin');
Expand Down Expand Up @@ -462,9 +439,7 @@ protected function parseText(string $text): string {
return $text;
}

/**
* @AfterScenario
*/
#[AfterScenario()]
public function tearDown(): void {
foreach ($this->createdUsers as $user) {
$this->deleteUser($user);
Expand Down
Loading