summaryrefslogtreecommitdiffstats
path: root/tests/integration/features/bootstrap/CommandLineTrait.php
blob: 968486a8bd71fb6f0e3883b865433fd7bf088ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Lukas Reschke <lukas@statuscode.ch>
 * @author Robin Appelman <robin@icewind.nl>
 * @author Vincent Petry <pvince81@owncloud.com>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */

use PHPUnit\Framework\Assert;

require __DIR__ . '/../../vendor/autoload.php';

trait CommandLineTrait {
	/** @var int return code of last command */
	private $lastCode;
	/** @var string stdout of last command */
	private $lastStdOut;
	/** @var string stderr of last command */
	private $lastStdErr;

	/** @var string */
	protected $ocPath = '../../../..';

	/**
	 * Invokes an OCC command
	 *
	 * @param []string $args OCC command, the part behind "occ". For example: "files:transfer-ownership"
	 * @param []string $env environment variables
	 * @return int exit code
	 */
	public function runOcc($args = [], $env = null) {
		// Set UTF-8 locale to ensure that escapeshellarg will not strip
		// multibyte characters.
		setlocale(LC_CTYPE, "C.UTF-8");

		$args = array_map(function ($arg) {
			return escapeshellarg($arg);
		}, $args);
		$args[] = '--no-ansi';
		$args = implode(' ', $args);

		$descriptor = [
			0 => ['pipe', 'r'],
			1 => ['pipe', 'w'],
			2 => ['pipe', 'w'],
		];
		$process = proc_open('php console.php ' . $args, $descriptor, $pipes, $this->ocPath, $env);
		$this->lastStdOut = stream_get_contents($pipes[1]);
		$this->lastStdErr = stream_get_contents($pipes[2]);
		$this->lastCode = proc_close($process);

		// Clean opcode cache
		$client = new GuzzleHttp\Client();
		$client->request('GET', 'http://localhost:8080/apps/testing/clean_opcode_cache.php');

		return $this->lastCode;
	}

	/**
	 * @Given /^invoking occ with "([^"]*)"$/
	 */
	public function invokingTheCommand($cmd) {
		if (preg_match('/room-name:(?P<token>\w+)/', $cmd, $matches)) {
			if (array_key_exists($matches['token'], self::$identifierToToken)) {
				$cmd = preg_replace('/room-name:(\w+)/', self::$identifierToToken[$matches['token']], $cmd);
			}
		}
		$args = explode(' ', $cmd);
		$this->runOcc($args);
	}

	public function getLastStdOut(): string {
		return $this->lastStdOut;
	}

	/**
	 * Find exception texts in stderr
	 */
	public function findExceptions() {
		$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() {
		$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 .= ' Exceptions: ' . implode(', ', $exceptions);
			}
			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($exitCode) {
		Assert::assertEquals($exitCode, $this->lastCode, 'The commands exit code did not match');
	}

	/**
	 * @Then /^the command failed with exception text "([^"]*)"$/
	 */
	public function theCommandFailedWithException($exceptionText) {
		$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 "([^"]*)"$/
	 */
	public function theCommandOutputContainsTheText($text) {
		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() {
		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($text) {
		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');
	}
}