CommandLine.php 4.9 KB

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