CommandLine.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 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. return $this->lastCode;
  53. }
  54. /**
  55. * @Given /^invoking occ with "([^"]*)"$/
  56. */
  57. public function invokingTheCommand($cmd) {
  58. $args = explode(' ', $cmd);
  59. $this->runOcc($args);
  60. }
  61. /**
  62. * Find exception texts in stderr
  63. */
  64. public function findExceptions() {
  65. $exceptions = [];
  66. $captureNext = false;
  67. // the exception text usually appears after an "[Exception"] row
  68. foreach (explode("\n", $this->lastStdErr) as $line) {
  69. if (preg_match('/\[Exception\]/', $line)) {
  70. $captureNext = true;
  71. continue;
  72. }
  73. if ($captureNext) {
  74. $exceptions[] = trim($line);
  75. $captureNext = false;
  76. }
  77. }
  78. return $exceptions;
  79. }
  80. /**
  81. * Finds all lines containing the given text
  82. *
  83. * @param string $input stdout or stderr output
  84. * @param string $text text to search for
  85. * @return array array of lines that matched
  86. */
  87. public function findLines($input, $text) {
  88. $results = [];
  89. // the exception text usually appears after an "[Exception"] row
  90. foreach (explode("\n", $input) as $line) {
  91. if (strpos($line, $text) >= 0) {
  92. $results[] = $line;
  93. }
  94. }
  95. return $results;
  96. }
  97. /**
  98. * @Then /^the command was successful$/
  99. */
  100. public function theCommandWasSuccessful() {
  101. $exceptions = $this->findExceptions();
  102. if ($this->lastCode !== 0) {
  103. $msg = 'The command was not successful, exit code was ' . $this->lastCode . '.';
  104. if (!empty($exceptions)) {
  105. $msg .= ' Exceptions: ' . implode(', ', $exceptions);
  106. }
  107. throw new \Exception($msg);
  108. } else if (!empty($exceptions)) {
  109. $msg = 'The command was successful but triggered exceptions: ' . implode(', ', $exceptions);
  110. throw new \Exception($msg);
  111. }
  112. }
  113. /**
  114. * @Then /^the command failed with exit code ([0-9]+)$/
  115. */
  116. public function theCommandFailedWithExitCode($exitCode) {
  117. if ($this->lastCode !== (int)$exitCode) {
  118. throw new \Exception('The command was expected to fail with exit code ' . $exitCode . ' but got ' . $this->lastCode);
  119. }
  120. }
  121. /**
  122. * @Then /^the command failed with exception text "([^"]*)"$/
  123. */
  124. public function theCommandFailedWithException($exceptionText) {
  125. $exceptions = $this->findExceptions();
  126. if (empty($exceptions)) {
  127. throw new \Exception('The command did not throw any exceptions');
  128. }
  129. if (!in_array($exceptionText, $exceptions)) {
  130. throw new \Exception('The command did not throw any exception with the text "' . $exceptionText . '"');
  131. }
  132. }
  133. /**
  134. * @Then /^the command output contains the text "([^"]*)"$/
  135. */
  136. public function theCommandOutputContainsTheText($text) {
  137. $lines = $this->findLines($this->lastStdOut, $text);
  138. if (empty($lines)) {
  139. throw new \Exception('The command did not output the expected text on stdout "' . $exceptionText . '"');
  140. }
  141. }
  142. /**
  143. * @Then /^the command error output contains the text "([^"]*)"$/
  144. */
  145. public function theCommandErrorOutputContainsTheText($text) {
  146. $lines = $this->findLines($this->lastStdErr, $text);
  147. if (empty($lines)) {
  148. throw new \Exception('The command did not output the expected text on stderr "' . $exceptionText . '"');
  149. }
  150. }
  151. }