CommandLine.php 4.5 KB

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