Skip to content

Commit a4337fe

Browse files
committed
refactor: switch behat context hooks to docblocks
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent f773a4f commit a4337fe

1 file changed

Lines changed: 71 additions & 29 deletions

File tree

src/NextcloudApiContext.php

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
use Behat\Behat\Context\Context;
66
use Behat\Gherkin\Node\PyStringNode;
77
use Behat\Gherkin\Node\TableNode;
8-
use Behat\Hook\AfterScenario;
9-
use Behat\Hook\BeforeScenario;
10-
use Behat\Hook\BeforeSuite;
11-
use Behat\Step\Given;
128
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
139
use DOMDocument;
1410
use Exception;
@@ -57,7 +53,9 @@ public function __construct(?array $parameters = []) {
5753
}
5854
}
5955

60-
#[BeforeSuite()]
56+
/**
57+
* @BeforeSuite
58+
*/
6159
public static function beforeSuite(BeforeSuiteScope $scope):void {
6260
$whoami = (string) exec('whoami');
6361
if (get_current_user() !== $whoami) {
@@ -70,18 +68,24 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
7068
}
7169
}
7270

73-
#[BeforeScenario()]
71+
/**
72+
* @BeforeScenario
73+
*/
7474
public static function beforeScenario(): void {
7575
self::$createdUsers = [];
7676
self::$environments = [];
7777
}
7878

79-
#[Given('as user :user')]
79+
/**
80+
* @Given as user :user
81+
*/
8082
public function setCurrentUser(string $user): void {
8183
$this->currentUser = $user;
8284
}
8385

84-
#[Given('user :user exists')]
86+
/**
87+
* @Given user :user exists
88+
*/
8589
public function assureUserExists(string $user): void {
8690
$response = $this->userExists($user);
8791
if ($response->getStatusCode() !== 200) {
@@ -94,7 +98,9 @@ public function assureUserExists(string $user): void {
9498
}
9599
}
96100

97-
#[Given('guest :guest exists')]
101+
/**
102+
* @Given guest :guest exists
103+
*/
98104
public function assureGuestExists(string $guest): void {
99105
$response = $this->userExists($guest);
100106
if ($response->getStatusCode() !== 200) {
@@ -134,7 +140,9 @@ protected function createUser(string $user): void {
134140
$this->setCurrentUser($currentUser);
135141
}
136142

137-
#[Given('/^set the display name of user "([^"]*)" to "([^"]*)"$/')]
143+
/**
144+
* @Given /^set the display name of user "([^"]*)" to "([^"]*)"$/
145+
*/
138146
public function setUserDisplayName(string $user, ?string $displayName = null): void {
139147
$currentUser = $this->currentUser;
140148
$this->setCurrentUser('admin');
@@ -146,7 +154,9 @@ public function setUserDisplayName(string $user, ?string $displayName = null): v
146154
$this->setCurrentUser($currentUser);
147155
}
148156

149-
#[Given('/^set the email of user "([^"]*)" to "([^"]*)"$/')]
157+
/**
158+
* @Given /^set the email of user "([^"]*)" to "([^"]*)"$/
159+
*/
150160
public function setUserEmail(string $user, string $email): void {
151161
$currentUser = $this->currentUser;
152162
$this->setCurrentUser('admin');
@@ -161,8 +171,8 @@ public function setUserEmail(string $user, string $email): void {
161171
* @param string $verb
162172
* @param string $url
163173
* @param TableNode|array|null $body
174+
* @Given sending :verb to ocs :url
164175
*/
165-
#[Given('sending :verb to ocs :url')]
166176
public function sendOCSRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
167177
$url = '/ocs/v2.php' . $url;
168178
$headers['OCS-ApiRequest'] = 'true';
@@ -174,8 +184,8 @@ public function sendOCSRequest(string $verb, string $url, $body = null, array $h
174184
* @param string $url
175185
* @param TableNode|PyStringNode|array|null $body
176186
* @param array $headers
187+
* @Given sending :verb to :url
177188
*/
178-
#[Given('sending :verb to :url')]
179189
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
180190
if (!str_starts_with($url, '/')) {
181191
$url = '/' . $url;
@@ -263,7 +273,9 @@ private function normalizePayloadForRequest(string $verb, array $options): array
263273
return $options;
264274
}
265275

266-
#[Given('/^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/')]
276+
/**
277+
* @Given /^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/
278+
*/
267279
public function setTheCustomHttpHeaderAsValueToNextRequest(string $header, string $value):void {
268280
if (empty($value)) {
269281
unset($this->customHeaders[$header]);
@@ -313,7 +325,9 @@ protected function assertStatusCode(ResponseInterface $response, int $statusCode
313325
/**
314326
* @throws \InvalidArgumentException
315327
*/
316-
#[Given('the response should have a status code :code')]
328+
/**
329+
* @Given the response should have a status code :code
330+
*/
317331
public function theResponseShouldHaveStatusCode(string $code): void {
318332
$currentCode = $this->response->getStatusCode();
319333
Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents());
@@ -322,7 +336,9 @@ public function theResponseShouldHaveStatusCode(string $code): void {
322336
/**
323337
* @throws \InvalidArgumentException
324338
*/
325-
#[Given('the response should be a JSON array with the following mandatory values')]
339+
/**
340+
* @Given the response should be a JSON array with the following mandatory values
341+
*/
326342
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
327343
$this->response->getBody()->seek(0);
328344
$expectedValues = $table->getColumnsHash();
@@ -396,7 +412,9 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
396412
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
397413
}
398414

399-
#[Given('fetch field :path from previous JSON response')]
415+
/**
416+
* @Given fetch field :path from previous JSON response
417+
*/
400418
public function fetchFieldFromPreviousJsonResponse(string $path): void {
401419
$this->response->getBody()->seek(0);
402420
$body = $this->response->getBody()->getContents();
@@ -427,7 +445,9 @@ public function fetchFieldFromPreviousJsonResponse(string $path): void {
427445
$this->fields[$path] = $value;
428446
}
429447

430-
#[Given('the response should contain the initial state :name with the following values:')]
448+
/**
449+
* @Given the response should contain the initial state :name with the following values:
450+
*/
431451
public function theResponseShouldContainTheInitialStateWithTheFollowingValues(string $name, PyStringNode $expected): void {
432452
$this->response->getBody()->seek(0);
433453
$html = $this->response->getBody()->getContents();
@@ -456,7 +476,9 @@ public function theResponseShouldContainTheInitialStateWithTheFollowingValues(st
456476
}
457477
}
458478

459-
#[Given('the response should contain the initial state :name json that match with:')]
479+
/**
480+
* @Given the response should contain the initial state :name json that match with:
481+
*/
460482
public function theResponseShouldContainTheInitialStateJsonThatMatchWith(string $name, TableNode $table): void {
461483
$this->response->getBody()->seek(0);
462484
$html = $this->response->getBody()->getContents();
@@ -477,7 +499,9 @@ public function theResponseShouldContainTheInitialStateJsonThatMatchWith(string
477499
$this->jsonStringMatchWith($actual, $expectedValues);
478500
}
479501

480-
#[Given('the following :appId app config is set')]
502+
/**
503+
* @Given the following :appId app config is set
504+
*/
481505
public function setAppConfig(string $appId, TableNode $formData): void {
482506
$currentUser = $this->currentUser;
483507
$this->setCurrentUser('admin');
@@ -526,7 +550,9 @@ protected function parseText(string $text): string {
526550
return $text;
527551
}
528552

529-
#[Given('/^run the command "(?P<command>(?:[^"]|\\")*)"$/')]
553+
/**
554+
* @Given /^run the command "(?P<command>(?:[^"]|\\")*)"$/
555+
*/
530556
public static function runCommand(string $command): array {
531557
$console = static::findParentDirContainingFile('console.php');
532558
$console .= '/console.php';
@@ -596,47 +622,63 @@ private static function runBashCommand(string $command): array {
596622
];
597623
}
598624

599-
#[Given('the output of the last command should contain the following text:')]
625+
/**
626+
* @Given the output of the last command should contain the following text:
627+
*/
600628
public static function theOutputOfTheLastCommandContains(PyStringNode $text): void {
601629
Assert::assertStringContainsString((string) $text, self::$commandOutput, 'The output of the last command does not contain: ' . (string) $text);
602630
}
603631

604-
#[Given('the output of the last command should be empty')]
632+
/**
633+
* @Given the output of the last command should be empty
634+
*/
605635
public static function theOutputOfTheLastCommandShouldBeEmpty(): void {
606636
Assert::assertEmpty(self::$commandOutput, 'The output of the last command should be empty, but got: ' . self::$commandOutput);
607637
}
608638

609-
#[Given('/^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/')]
639+
/**
640+
* @Given /^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
641+
*/
610642
public static function runCommandWithResultCode(string $command, int $resultCode = 0): void {
611643
$return = self::runCommand($command);
612644
Assert::assertEquals($resultCode, $return['resultCode'], print_r($return, true));
613645
}
614646

615-
#[Given('/^run the bash command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/')]
647+
/**
648+
* @Given /^run the bash command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
649+
*/
616650
public static function runBashCommandWithResultCode(string $command, int $resultCode = 0): void {
617651
$return = self::runBashCommand($command);
618652
Assert::assertEquals($resultCode, $return['resultCode'], print_r($return, true));
619653
}
620654

621-
#[Given('create an environment :name with value :value to be used by occ command')]
655+
/**
656+
* @Given create an environment :name with value :value to be used by occ command
657+
*/
622658
public static function createAnEnvironmentWithValueToBeUsedByOccCommand(string $name, string $value):void {
623659
self::$environments[$name] = $value;
624660
}
625661

626-
#[Given('/^wait for ([0-9]+) (second|seconds)$/')]
662+
/**
663+
* @Given /^wait for ([0-9]+) (second|seconds)$/
664+
*/
627665
public function waitForXSecond(int $seconds): void {
628666
$this->startWaitFor = $seconds;
629667
sleep($seconds);
630668
}
631669

632-
#[Given('/^past ([0-9]+) (second|seconds) since wait step$/')]
670+
/**
671+
* @Given /^past ([0-9]+) (second|seconds) since wait step$/
672+
*/
633673
public function pastXSecondsSinceWaitStep(int $seconds): void {
634674
$currentTime = time();
635675
$startTime = $currentTime - $this->startWaitFor;
636676
Assert::assertGreaterThanOrEqual($startTime, $currentTime, 'The current time is not greater than or equal to the start time.');
637677
}
638678

639-
#[AfterScenario()]
679+
/**
680+
* @AfterScenario
681+
*/
640682
public function tearDown(): void {
641683
self::$environments = [];
642684
foreach (self::$createdUsers as $user) {

0 commit comments

Comments
 (0)