From f912939cfdb64fc81d5ed01a8f4f0dfc18a8bc7c Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Thu, 31 Jul 2025 13:24:12 -0300 Subject: [PATCH 1/4] chore: replace annotation by attibute Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- src/NextcloudApiContext.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index a4a7577..8c49ac9 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -5,6 +5,9 @@ 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\Testwork\Hook\Scope\BeforeSuiteScope; use DOMDocument; use Exception; @@ -48,9 +51,7 @@ public function __construct(?array $parameters = []) { } } - /** - * @BeforeSuite - */ + #[BeforeSuite()] public static function beforeSuite(BeforeSuiteScope $scope):void { $whoami = (string) exec('whoami'); if (get_current_user() !== $whoami) { @@ -63,9 +64,7 @@ public static function beforeSuite(BeforeSuiteScope $scope):void { } } - /** - * @BeforeScenario - */ + #[BeforeScenario()] public function setUp(): void { $this->createdUsers = []; } @@ -462,9 +461,7 @@ protected function parseText(string $text): string { return $text; } - /** - * @AfterScenario - */ + #[AfterScenario()] public function tearDown(): void { foreach ($this->createdUsers as $user) { $this->deleteUser($user); From 3388c9f117403bfbe210477096bd6f4a70d92192 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Thu, 31 Jul 2025 14:20:45 -0300 Subject: [PATCH 2/4] chore: replace more annotations by attributes Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- features/bootstrap/FeatureContext.php | 5 ++-- src/NextcloudApiContext.php | 41 +++++++++------------------ 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 4a38d25..9c93f9a 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -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; @@ -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( diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index 8c49ac9..167a42f 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -8,6 +8,7 @@ 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; @@ -69,18 +70,12 @@ 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) { @@ -120,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'); @@ -132,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'); @@ -144,11 +139,11 @@ 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'; @@ -156,12 +151,12 @@ public function sendOCSRequest(string $verb, string $url, $body = null, array $h } /** - * @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; @@ -247,20 +242,20 @@ protected function assertStatusCode(ResponseInterface $response, int $statusCode } /** - * @When the response should have a status code :code * @param string $code * @throws \InvalidArgumentException */ + #[Given('the response should have a status code :code')] public function theResponseShouldHaveStatusCode($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(); @@ -334,9 +329,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); @@ -358,9 +351,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(); @@ -389,9 +380,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(); @@ -412,11 +401,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'); From 328d95a35a938ff868ae1bcfbc2a2cbaec04bdc9 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Thu, 31 Jul 2025 14:22:53 -0300 Subject: [PATCH 3/4] chore: replace when by given Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 963c643..dbd1139 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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} | ``` @@ -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":""} | ``` @@ -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": { From 930d2bf55ef88affa0bf7e56c836fc8dddb2758d Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Thu, 31 Jul 2025 14:26:52 -0300 Subject: [PATCH 4/4] chore: remove unecessary annotations Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- src/NextcloudApiContext.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index 167a42f..4d3145a 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -232,27 +232,20 @@ 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); } /** - * @param string $code * @throws \InvalidArgumentException */ #[Given('the response should have a status code :code')] - public function theResponseShouldHaveStatusCode($code): void { + public function theResponseShouldHaveStatusCode(string $code): void { $currentCode = $this->response->getStatusCode(); Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents()); } /** - * @param TableNode $table * @throws \InvalidArgumentException */ #[Given('the response should be a JSON array with the following mandatory values')]