CommandLine.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. require __DIR__ . '/../../vendor/autoload.php';
  22. trait CommandLine {
  23. /** @var int return code of last command */
  24. private $lastCode;
  25. /** @var string stdout of last command */
  26. private $lastStdOut;
  27. /** @var string stderr of last command */
  28. private $lastStdErr;
  29. /** @var string */
  30. protected $ocPath = '../..';
  31. /**
  32. * Invokes an OCC command
  33. *
  34. * @param []string $args OCC command, the part behind "occ". For example: "files:transfer-ownership"
  35. * @return int exit code
  36. */
  37. public function runOcc($args = []) {
  38. $args = array_map(function($arg) {
  39. return escapeshellarg($arg);
  40. }, $args);
  41. $args[] = '--no-ansi';
  42. $args = implode(' ', $args);
  43. $descriptor = [
  44. 0 => ['pipe', 'r'],
  45. 1 => ['pipe', 'w'],
  46. 2 => ['pipe', 'w'],
  47. ];
  48. $process = proc_open('php console.php ' . $args, $descriptor, $pipes, $this->ocPath);
  49. $this->lastStdOut = stream_get_contents($pipes[1]);
  50. $this->lastStdErr = stream_get_contents($pipes[2]);
  51. $this->lastCode = proc_close($process);
  52. // Clean opcode cache
  53. $client = new GuzzleHttp\Client();
  54. $request = $client->createRequest('GET', 'http://localhost:8080/apps/testing/clean_opcode_cache.php');
  55. $client->send($request);
  56. return $this->lastCode;
  57. }
  58. /**
  59. * @Given /^invoking occ with "([^"]*)"$/
  60. */
  61. public function invokingTheCommand($cmd) {
  62. $args = explode(' ', $cmd);
  63. $this->runOcc($args);
  64. }
  65. /**
  66. * Find exception texts in stderr
  67. */
  68. public function findExceptions() {
  69. $exceptions = [];
  70. $captureNext = false;
  71. // the exception text usually appears after an "[Exception"] row
  72. foreach (explode("\n", $this->lastStdErr) as $line) {
  73. if (preg_match('/\[Exception\]/', $line)) {
  74. $captureNext = true;
  75. continue;
  76. }
  77. if ($captureNext) {
  78. $exceptions[] = trim($line);
  79. $captureNext = false;
  80. }
  81. }
  82. return $exceptions;
  83. }
  84. /**
  85. * Finds all lines containing the given text
  86. *
  87. * @param string $input stdout or stderr output
  88. * @param string $text text to search for
  89. * @return array array of lines that matched
  90. */
  91. public function findLines($input, $text) {
  92. $results = [];
  93. // the exception text usually appears after an "[Exception"] row
  94. foreach (explode("\n", $input) as $line) {
  95. if (strpos($line, $text) >= 0) {
  96. $results[] = $line;
  97. }
  98. }
  99. return $results;
  100. }
  101. /**
  102. * @Then /^the command was successful$/
  103. */
  104. public function theCommandWasSuccessful() {
  105. $exceptions = $this->findExceptions();
  106. if ($this->lastCode !== 0) {
  107. $msg = 'The command was not successful, exit code was ' . $this->lastCode . '.';
  108. if (!empty($exceptions)) {
  109. $msg .= ' Exceptions: ' . implode(', ', $exceptions);
  110. }
  111. throw new \Exception($msg);
  112. } else if (!empty($exceptions)) {
  113. $msg = 'The command was successful but triggered exceptions: ' . implode(', ', $exceptions);
  114. throw new \Exception($msg);
  115. }
  116. }
  117. /**
  118. * @Then /^the command failed with exit code ([0-9]+)$/
  119. */
  120. public function theCommandFailedWithExitCode($exitCode) {
  121. if ($this->lastCode !== (int)$exitCode) {
  122. throw new \Exception('The command was expected to fail with exit code ' . $exitCode . ' but got ' . $this->lastCode);
  123. }
  124. }
  125. /**
  126. * @Then /^the command failed with exception text "([^"]*)"$/
  127. */
  128. public function theCommandFailedWithException($exceptionText) {
  129. $exceptions = $this->findExceptions();
  130. if (empty($exceptions)) {
  131. throw new \Exception('The command did not throw any exceptions');
  132. }
  133. if (!in_array($exceptionText, $exceptions)) {
  134. throw new \Exception('The command did not throw any exception with the text "' . $exceptionText . '"');
  135. }
  136. }
  137. /**
  138. * @Then /^the command output contains the text "([^"]*)"$/
  139. */
  140. public function theCommandOutputContainsTheText($text) {
  141. $lines = $this->findLines($this->lastStdOut, $text);
  142. if (empty($lines)) {
  143. throw new \Exception('The command did not output the expected text on stdout "' . $exceptionText . '"');
  144. }
  145. }
  146. /**
  147. * @Then /^the command error output contains the text "([^"]*)"$/
  148. */
  149. public function theCommandErrorOutputContainsTheText($text) {
  150. $lines = $this->findLines($this->lastStdErr, $text);
  151. if (empty($lines)) {
  152. throw new \Exception('The command did not output the expected text on stderr "' . $exceptionText . '"');
  153. }
  154. }
  155. }