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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"php": ">=8.2",
"guzzlehttp/guzzle": "^7.10 || ^8.0",
"phpunit/phpunit": "*",
"behat/behat": "^3.29",
"behat/behat": "^3.29 || ^4.0",
"libresign/behat-builtin-extension": "^0.7.1",
"estahn/json-query-wrapper": "*"
"symfony/process": "^6.4 || ^7.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.9",
Expand Down
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="6.5.0@38fc8444edf0cebc9205296ee6e30e906ade783b">
<file src="src/NextcloudApiContext.php">
<ForbiddenCode>
<code><![CDATA[`which jq`]]></code>
<code><![CDATA[`which jq`]]></code>
</ForbiddenCode>
</file>
</files>
34 changes: 22 additions & 12 deletions src/NextcloudApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
use PhpBuiltin\RunServerListener;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

/**
* Defines application features from the specific context.
*/
class NextcloudApiContext implements Context {
protected string $testPassword = '123456';
protected string $adminPassword = 'admin';
protected string $baseUrl;
protected RunServerListener $server;
protected string $baseUrl = '';
protected ?RunServerListener $server = null;
protected string $currentUser = '';
protected array $fields = [];
protected static array $environments = [];
Expand All @@ -47,8 +49,6 @@ class NextcloudApiContext implements Context {
protected array $requestOptions = [];

public function __construct(?array $parameters = []) {
$this->server = RunServerListener::getInstance();
$this->baseUrl = RunServerListener::getServerRoot();
$this->response = new Response();
$this->cookieJars = [];
if (isset($parameters['test_password'])) {
Expand Down Expand Up @@ -223,6 +223,11 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
if (strpos($url, '/ocs/v2.php') === false) {
$url = '/index.php' . $url;
}
if ($this->baseUrl === '') {
// Behat 4 may instantiate contexts before the server listener exists.
$this->server = RunServerListener::getInstance();
$this->baseUrl = RunServerListener::getServerRoot();
}
if (str_ends_with($this->baseUrl, '/')) {
$this->baseUrl = rtrim($this->baseUrl, '/');
}
Expand Down Expand Up @@ -443,15 +448,20 @@ private function testAndGetActualValue(array $value, string $json): string {
* @return mixed
*/
private function evalJsonQuery(string $jsonQuery, string $target) {
Assert::assertNotEmpty(`which jq`, 'Is necessary install the jq command to use jq');
$jq = \JsonQueryWrapper\JsonQueryFactory::createWith($target);
return $jq->run($jsonQuery);
return $this->runJsonQuery($target, $jsonQuery);
}

private function runJsonQuery(string $json, string $filter): mixed {
Assert::assertNotEmpty((new ExecutableFinder())->find('jq'), 'Is necessary install the jq command to use jq');
$process = new Process(['jq', '-M', $filter], null, null, $json);
$process->mustRun();
$output = trim($process->getOutput());
$decoded = json_decode($output, true);
return json_last_error() === JSON_ERROR_NONE ? $decoded : $output;
}

private function validateAsJsonQuery(string $expected, string $actual): void {
Assert::assertNotEmpty(`which jq`, 'Is necessary install the jq command to use jq');
$jq = \JsonQueryWrapper\JsonQueryFactory::createWith($actual);
$result = $jq->run($expected);
$result = $this->runJsonQuery($actual, $expected);
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
}

Expand Down Expand Up @@ -682,13 +692,13 @@ public static function createAnEnvironmentWithValueToBeUsedByOccCommand(string $
self::$environments[$name] = $value;
}

#[Given('/^wait for ([0-9]+) (second|seconds)$/')]
#[Given('/^wait for ([0-9]+) seconds?$/')]
public function waitForXSecond(int $seconds): void {
$this->startWaitFor = $seconds;
sleep($seconds);
}

#[Given('/^past ([0-9]+) (second|seconds) since wait step$/')]
#[Given('/^past ([0-9]+) seconds? since wait step$/')]
public function pastXSecondsSinceWaitStep(int $seconds): void {
$currentTime = time();
$startTime = $currentTime - $this->startWaitFor;
Expand Down
Loading