currentServer === 'REMOTE') { $env['NEXTCLOUD_CONFIG_DIR'] = getenv('NEXTCLOUD_REMOTE_CONFIG_DIR'); $serverRootDir = getenv('NEXTCLOUD_REMOTE_ROOT_DIR'); } else { $env['NEXTCLOUD_CONFIG_DIR'] = getenv('NEXTCLOUD_HOST_CONFIG_DIR'); $serverRootDir = getenv('NEXTCLOUD_HOST_ROOT_DIR'); } $descriptor = [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ]; $process = proc_open('php ' . $serverRootDir . '/console.php ' . $argString, $descriptor, $pipes, $this->ocPath, $env); $this->lastStdOut = stream_get_contents($pipes[1]); $this->lastStdErr = stream_get_contents($pipes[2]); $this->lastCode = proc_close($process); if ($clearOpcodeCache) { // Clean opcode cache $client = new GuzzleHttp\Client(); if ($this->currentServer === 'REMOTE') { $client->request('GET', $this->remoteServerUrl . 'apps/testing/clean_opcode_cache.php'); } else { $client->request('GET', $this->localServerUrl . 'apps/testing/clean_opcode_cache.php'); } } return $this->lastCode; } #[Given('/^invoking occ with "([^"]*)"$/')] public function invokingTheCommand(string $cmd): void { // FIXME this way is deprecated if (preg_match('/room-name:(?P\w+)/', $cmd, $matches)) { if (array_key_exists($matches['token'], self::$identifierToToken)) { $cmd = preg_replace('/room-name:(\w+)/', self::$identifierToToken[$matches['token']], $cmd); } } if (preg_match('/ROOM\((?P\w+)\)/', $cmd, $matches)) { if (array_key_exists($matches['name'], self::$identifierToToken)) { $cmd = preg_replace('/ROOM\((\w+)\)/', self::$identifierToToken[$matches['name']], $cmd); } } if (preg_match('/BOT\((?P\w+)\)/', $cmd, $matches)) { if (array_key_exists($matches['name'], self::$botNameToId)) { $cmd = preg_replace('/BOT\((\w+)\)/', (string)self::$botNameToId[$matches['name']], $cmd); } } $args = explode(' ', $cmd); $this->runOcc($args); } public function getLastStdOut(): string { return $this->lastStdOut; } /** * Find exception texts in stderr */ public function findExceptions(): array { $exceptions = []; $captureNext = false; // the exception text usually appears after an "[Exception"] row foreach (explode("\n", $this->lastStdErr) as $line) { if (preg_match('/\[Exception\]/', $line)) { $captureNext = true; continue; } if ($captureNext) { $exceptions[] = trim($line); $captureNext = false; } } return $exceptions; } #[Then('/^the command was successful$/')] public function theCommandWasSuccessful(): void { $exceptions = $this->findExceptions(); if ($this->lastCode !== 0) { echo $this->lastStdErr; $msg = 'The command was not successful, exit code was ' . $this->lastCode . '.'; if (!empty($exceptions)) { $msg .= "\n" . ' Exceptions: ' . implode(', ', $exceptions); } else { $msg .= "\n" . ' ' . $this->lastStdOut; $msg .= "\n" . ' ' . $this->lastStdErr; } throw new \Exception($msg); } elseif (!empty($exceptions)) { $msg = 'The command was successful but triggered exceptions: ' . implode(', ', $exceptions); throw new \Exception($msg); } } #[Then('/^the command failed with exit code ([0-9]+)$/')] public function theCommandFailedWithExitCode(int $exitCode): void { Assert::assertEquals($exitCode, $this->lastCode, 'The commands exit code did not match: ' . $this->lastStdOut . "\n\n" . $this->lastStdErr); } #[Then('/^the command failed with exception text "([^"]*)"$/')] public function theCommandFailedWithException(string $exceptionText): void { $exceptions = $this->findExceptions(); if (empty($exceptions)) { throw new \Exception('The command did not throw any exceptions'); } if (!in_array($exceptionText, $exceptions)) { throw new \Exception('The command did not throw any exception with the text "' . $exceptionText . '"'); } } #[Then('/^the command output contains the text:$/')] #[Then('/^the command output contains the text "([^"]*)"$/')] #[Then("/^the command output contains the text '([^']*)'\$/")] public function theCommandOutputContainsTheText(string $text): void { if ($this->lastStdOut === '' && $this->lastStdErr !== '') { Assert::assertStringContainsString($text, $this->lastStdErr, 'The command did not output the expected text on stdout'); Assert::assertTrue(false, 'The command did not output the expected text on stdout but stderr'); } Assert::assertStringContainsString($text, $this->lastStdOut, 'The command did not output the expected text on stdout'); } #[Then('/^the command output is empty$/')] public function theCommandOutputIsEmpty(): void { Assert::assertEmpty($this->lastStdOut, 'The command did output unexpected text on stdout'); } #[Then("/^the command output contains the list entry '([^']*)' with value '([^']*)'\$/")] public function theCommandOutputContainsTheListEntry(string $key, string $value): void { if (preg_match('/^"ROOM\(([^"]+)\)"$/', $key, $matches)) { $key = '"' . self::$identifierToToken[$matches[1]] . '"'; } $text = '- ' . $key . ': ' . $value; if ($this->lastStdOut === '' && $this->lastStdErr !== '') { Assert::assertStringContainsString($text, $this->lastStdErr, 'The command did not output the expected text on stdout'); Assert::assertTrue(false, 'The command did not output the expected text on stdout but stderr'); } Assert::assertStringContainsString($text, $this->lastStdOut, 'The command did not output the expected text on stdout'); } #[Then('/^the command error output contains the text "([^"]*)"$/')] public function theCommandErrorOutputContainsTheText(string $text): void { if ($this->lastStdErr === '' && $this->lastStdOut !== '') { Assert::assertStringContainsString($text, $this->lastStdOut, 'The command did not output the expected text on stdout'); Assert::assertTrue(false, 'The command did not output the expected text on stdout but stderr'); } Assert::assertStringContainsString($text, $this->lastStdErr, 'The command did not output the expected text on stderr'); } }