Skip to content

Commit 3c19fdb

Browse files
authored
Merge pull request #109 from LibreSign/refactor/behat-attributes-override
refactor: migrate behat contexts to attributes
2 parents 5a2c0e4 + b2d1b1c commit 3c19fdb

4 files changed

Lines changed: 38 additions & 83 deletions

File tree

features/bootstrap/FeatureContext.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Behat\Gherkin\Node\PyStringNode;
44
use Behat\Gherkin\Node\TableNode;
5+
use Behat\Step\Given;
56
use donatj\MockWebServer\MockWebServer;
67
use donatj\MockWebServer\RequestInfo;
78
use donatj\MockWebServer\Response as MockWebServerResponse;
@@ -22,17 +23,17 @@ public function __construct(?array $parameters = []) {
2223

2324
/**
2425
* @inheritDoc
25-
* @psalm-suppress MissingOverrideAttribute
2626
*/
27+
#[\Override]
2728
public function setCurrentUser(string $user): void {
2829
parent::setCurrentUser($user);
2930
Assert::assertEquals($this->currentUser, $user);
3031
}
3132

3233
/**
3334
* @inheritDoc
34-
* @psalm-suppress MissingOverrideAttribute
3535
*/
36+
#[\Override]
3637
public function assureUserExists(string $user): void {
3738
parent::assureUserExists($user);
3839
$lastRequest = $this->getLastREquest();
@@ -57,17 +58,16 @@ private function getLastRequest(): RequestInfo {
5758
* When whe run the test suit of this repository at GitHub Actions, is
5859
* necessary to consider that we haven't Nextcloud installed and mock
5960
* the real path of files.
60-
* @psalm-suppress MissingOverrideAttribute
6161
*/
62+
#[\Override]
6263
public static function findParentDirContainingFile(string $filename): string {
6364
return __DIR__;
6465
}
6566

6667
/**
6768
* @inheritDoc
68-
* @param TableNode|PyStringNode|array|null $body
69-
* @psalm-suppress MissingOverrideAttribute
7069
*/
70+
#[\Override]
7171
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
7272
parent::sendRequest($verb, $url, $body, $headers, $options);
7373
$lastRequest = $this->getLastRequest();
@@ -131,9 +131,7 @@ private function hasNestedPayload(array $payload): bool {
131131
return false;
132132
}
133133

134-
/**
135-
* @Given set the response to:
136-
*/
134+
#[Given('set the response to:')]
137135
public function setTheResponseTo(PyStringNode $response): void {
138136
// Mock response to be equal to body of request
139137
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
@@ -143,8 +141,8 @@ public function setTheResponseTo(PyStringNode $response): void {
143141

144142
/**
145143
* @inheritDoc
146-
* @psalm-suppress MissingOverrideAttribute
147144
*/
145+
#[\Override]
148146
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
149147
$lastRequest = $this->getLastRequest();
150148
$parsedInput = $this->getParsedInputFromRequest($lastRequest);

features/test.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Feature: Test this extension
6464
}
6565
"""
6666
Then the response should be a JSON array with the following mandatory values
67-
| key | value |
68-
| (jq).status.nested | true |
67+
| key | value |
68+
| (jq).status.nested | true |
6969

7070
Scenario: Test response of POST is json
7171
When set the response to:

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
33
errorLevel="2"
4-
ensureOverrideAttribute="false"
54
findUnusedBaselineEntry="true"
65
findUnusedCode="false"
76
resolveFromConfigFile="true"

src/NextcloudApiContext.php

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
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;
812
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
913
use DOMDocument;
1014
use Exception;
@@ -53,9 +57,7 @@ public function __construct(?array $parameters = []) {
5357
}
5458
}
5559

56-
/**
57-
* @BeforeSuite
58-
*/
60+
#[BeforeSuite()]
5961
public static function beforeSuite(BeforeSuiteScope $scope):void {
6062
$whoami = (string) exec('whoami');
6163
if (get_current_user() !== $whoami) {
@@ -68,24 +70,18 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
6870
}
6971
}
7072

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

79-
/**
80-
* @Given as user :user
81-
*/
79+
#[Given('as user :user')]
8280
public function setCurrentUser(string $user): void {
8381
$this->currentUser = $user;
8482
}
8583

86-
/**
87-
* @Given user :user exists
88-
*/
84+
#[Given('user :user exists')]
8985
public function assureUserExists(string $user): void {
9086
$response = $this->userExists($user);
9187
if ($response->getStatusCode() !== 200) {
@@ -98,9 +94,7 @@ public function assureUserExists(string $user): void {
9894
}
9995
}
10096

101-
/**
102-
* @Given guest :guest exists
103-
*/
97+
#[Given('guest :guest exists')]
10498
public function assureGuestExists(string $guest): void {
10599
$response = $this->userExists($guest);
106100
if ($response->getStatusCode() !== 200) {
@@ -140,9 +134,7 @@ protected function createUser(string $user): void {
140134
$this->setCurrentUser($currentUser);
141135
}
142136

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

157-
/**
158-
* @Given /^set the email of user "([^"]*)" to "([^"]*)"$/
159-
*/
149+
#[Given('/^set the email of user "([^"]*)" to "([^"]*)"$/')]
160150
public function setUserEmail(string $user, string $email): void {
161151
$currentUser = $this->currentUser;
162152
$this->setCurrentUser('admin');
@@ -171,8 +161,8 @@ public function setUserEmail(string $user, string $email): void {
171161
* @param string $verb
172162
* @param string $url
173163
* @param TableNode|array|null $body
174-
* @Given sending :verb to ocs :url
175164
*/
165+
#[Given('sending :verb to ocs :url')]
176166
public function sendOCSRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
177167
$url = '/ocs/v2.php' . $url;
178168
$headers['OCS-ApiRequest'] = 'true';
@@ -184,8 +174,8 @@ public function sendOCSRequest(string $verb, string $url, $body = null, array $h
184174
* @param string $url
185175
* @param TableNode|PyStringNode|array|null $body
186176
* @param array $headers
187-
* @Given sending :verb to :url
188177
*/
178+
#[Given('sending :verb to :url')]
189179
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
190180
if (!str_starts_with($url, '/')) {
191181
$url = '/' . $url;
@@ -273,9 +263,7 @@ private function normalizePayloadForRequest(string $verb, array $options): array
273263
return $options;
274264
}
275265

276-
/**
277-
* @Given /^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/
278-
*/
266+
#[Given('/^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/')]
279267
public function setTheCustomHttpHeaderAsValueToNextRequest(string $header, string $value):void {
280268
if (empty($value)) {
281269
unset($this->customHeaders[$header]);
@@ -325,9 +313,7 @@ protected function assertStatusCode(ResponseInterface $response, int $statusCode
325313
/**
326314
* @throws \InvalidArgumentException
327315
*/
328-
/**
329-
* @Given the response should have a status code :code
330-
*/
316+
#[Given('the response should have a status code :code')]
331317
public function theResponseShouldHaveStatusCode(string $code): void {
332318
$currentCode = $this->response->getStatusCode();
333319
Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents());
@@ -336,9 +322,7 @@ public function theResponseShouldHaveStatusCode(string $code): void {
336322
/**
337323
* @throws \InvalidArgumentException
338324
*/
339-
/**
340-
* @Given the response should be a JSON array with the following mandatory values
341-
*/
325+
#[Given('the response should be a JSON array with the following mandatory values')]
342326
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
343327
$this->response->getBody()->seek(0);
344328
$expectedValues = $table->getColumnsHash();
@@ -412,9 +396,7 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
412396
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
413397
}
414398

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

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

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

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

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

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

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

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

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

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

662-
/**
663-
* @Given /^wait for ([0-9]+) (second|seconds)$/
664-
*/
626+
#[Given('/^wait for ([0-9]+) (second|seconds)$/')]
665627
public function waitForXSecond(int $seconds): void {
666628
$this->startWaitFor = $seconds;
667629
sleep($seconds);
668630
}
669631

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

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

0 commit comments

Comments
 (0)