Skip to content

Commit 5980376

Browse files
committed
feat: add more steps to works with commands
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 74625b8 commit 5980376

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ vendor/bin/behat -dl
5151
Given as user :user
5252
Given user :user exists
5353
Given sending :verb to :url
54-
Given set the response to:
5554
Given the response should be a JSON array with the following mandatory values
5655
Given /^set the display name of user "([^"]*)" to "([^"]*)"$/
5756
Given /^set the email of user "([^"]*)" to "([^"]*)"$/
5857
Given sending :verb to ocs :url
5958
Given the response should have a status code :code
60-
Given fetch field :path from previous JSON response
59+
Given fetch field :path from prevous JSON response
6160
Given the response should contain the initial state :name with the following values:
6261
Given the response should contain the initial state :name json that match with:
6362
Given the following :appId app config is set
6463
Given /^run the command "(?P<command>(?:[^"]|\\")*)"$/
64+
Given the output of the last command should contain the following text:
65+
Given the output of the last command should be empty
6566
Given /^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
6667
Given /^run the bash command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
68+
Given create an environment :name with value :value to be used by occ command
6769
```
6870

6971
## Tips
@@ -89,7 +91,7 @@ Given sending "post" to ocs "/apps/libresign/api/v1/request-signature"
8991

9092
### Step: all steps that run commands
9193

92-
Before the command be executd, will replace the follow placeholders:
94+
Before the command is executed, the following placeholders will be replaced:
9395

9496
| Placeholder | Value |
9597
| -------------------- | ---------------------- |

features/test.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,35 @@ Feature: Test this extension
230230

231231
Scenario: Test list app directory with success
232232
When run the bash command "ls <appRootDir>" with result code 0
233+
Then the output of the last command should contain the following text:
234+
"""
235+
appinfo
236+
"""
233237

234238
Scenario: Test list Nextcloud directory with success
235239
When run the bash command "ls <nextcloudRootDir>" with result code 0
236240

237241
Scenario: Test run bash command with success
238242
When run the bash command "true" with result code 0
243+
Then the output of the last command should be empty
239244

240245
Scenario: Test run bash command with error
241246
When run the bash command "false" with result code 1
242247

243248
Scenario: Run occ command with success
244249
When run the command "status" with result code 0
250+
Then the output of the last command should contain the following text:
251+
"""
252+
version:
253+
"""
245254

246255
Scenario: Run occ command with success
247256
When run the command "invalid-command" with result code 1
257+
258+
Scenario: Create an environment with value to be used by occ command
259+
When create an environment "OC_PASS" with value "" to be used by occ command
260+
And run the command "user:add --password-from-env test" with result code 1
261+
Then the output of the last command should contain the following text:
262+
"""
263+
--password-from-env given, but NC_PASS/OC_PASS is empty!
264+
"""

src/NextcloudApiContext.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ class NextcloudApiContext implements Context {
3030
protected RunServerListener $server;
3131
protected string $currentUser = '';
3232
protected array $fields = [];
33+
protected static array $environments = [];
34+
protected static string $commandOutput = '';
35+
3336
/**
3437
* @var string[]
3538
*/
36-
protected array $createdUsers = [];
39+
protected static array $createdUsers = [];
3740
protected ResponseInterface $response;
3841
/** @var CookieJar[] */
3942
protected $cookieJars;
@@ -63,11 +66,13 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
6366
"runuser -u %s -- %s\n\n",
6467
get_current_user(), $whoami, get_current_user(), $command));
6568
}
69+
self::runCommand('config:system:set debug --value true --type boolean');
6670
}
6771

6872
#[BeforeScenario()]
69-
public function setUp(): void {
70-
$this->createdUsers = [];
73+
public function beforeScenario(): void {
74+
self::$createdUsers = [];
75+
self::$environments = [];
7176
}
7277

7378
#[Given('as user :user')]
@@ -110,7 +115,7 @@ protected function createUser(string $user): void {
110115
$this->sendOCSRequest('GET', '/cloud/users' . '/' . $user);
111116
$this->assertStatusCode($this->response, 200, 'Failed to do first login');
112117

113-
$this->createdUsers[] = $user;
118+
self::$createdUsers[] = $user;
114119

115120
$this->setCurrentUser($currentUser);
116121
}
@@ -461,6 +466,9 @@ public static function runCommand(string $command): array {
461466
throw new \Exception('Could not retrieve owner information for UID ' . $fileOwnerUid);
462467
}
463468
$fullCommand = 'php ' . $console . ' ' . $command;
469+
if (!empty(self::$environments)) {
470+
$fullCommand = http_build_query(self::$environments, '', ' ') . ' ' . $fullCommand;
471+
}
464472
if (posix_getuid() !== $owner['uid']) {
465473
$fullCommand = 'runuser -u ' . $owner['name'] . ' -- ' . $fullCommand;
466474
}
@@ -502,13 +510,24 @@ private static function runBashCommand(string $command): array {
502510
}
503511

504512
exec($command, $output, $resultCode);
513+
self::$commandOutput = implode("\n", $output);
505514
return [
506515
'command' => $command,
507516
'output' => $output,
508517
'resultCode' => $resultCode,
509518
];
510519
}
511520

521+
#[Given('the output of the last command should contain the following text:')]
522+
public static function theOutputOfTheLastCommandContains(PyStringNode $text): void {
523+
Assert::assertStringContainsString((string) $text, self::$commandOutput, 'The output of the last command does not contain: ' . $text);
524+
}
525+
526+
#[Given('the output of the last command should be empty')]
527+
public static function theOutputOfTheLastCommandShouldBeEmpty(): void {
528+
Assert::assertEmpty(self::$commandOutput, 'The output of the last command should be empty, but got: ' . self::$commandOutput);
529+
}
530+
512531
#[Given('/^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/')]
513532
public static function runCommandWithResultCode(string $command, int $resultCode = 0): void {
514533
$return = self::runCommand($command);
@@ -521,9 +540,15 @@ public static function runBashCommandWithResultCode(string $command, int $result
521540
Assert::assertEquals($resultCode, $return['resultCode'], print_r($return, true));
522541
}
523542

543+
#[Given('create an environment :name with value :value to be used by occ command')]
544+
public static function createAnEnvironmentWithValueToBeUsedByOccCommand(string $name, string $value):void {
545+
self::$environments[$name] = $value;
546+
}
547+
524548
#[AfterScenario()]
525549
public function tearDown(): void {
526-
foreach ($this->createdUsers as $user) {
550+
self::$environments = [];
551+
foreach (self::$createdUsers as $user) {
527552
$this->deleteUser($user);
528553
}
529554
}
@@ -534,7 +559,7 @@ protected function deleteUser(string $user): ResponseInterface {
534559
$this->sendOCSRequest('DELETE', '/cloud/users/' . $user);
535560
$this->setCurrentUser($currentUser);
536561

537-
unset($this->createdUsers[array_search($user, $this->createdUsers, true)]);
562+
unset(self::$createdUsers[array_search($user, self::$createdUsers, true)]);
538563

539564
return $this->response;
540565
}

0 commit comments

Comments
 (0)