CommandUnishTestCase.php

  1. 8.0.x tests/Unish/CommandUnishTestCase.php
  2. 7.x tests/Unish/CommandUnishTestCase.php
  3. master tests/Unish/CommandUnishTestCase.php

Namespace

Unish

Classes

File

tests/Unish/CommandUnishTestCase.php
View source
  1. <?php
  2. namespace Unish;
  3. use Symfony\Component\Process\Process;
  4. use Symfony\Component\Process\Exception\ProcessTimedOutException;
  5. abstract class CommandUnishTestCase extends UnishTestCase {
  6. // Unix exit codes.
  7. const EXIT_SUCCESS = 0;
  8. const EXIT_ERROR = 1;
  9. const UNISH_EXITCODE_USER_ABORT = 75; // Same as DRUSH_EXITCODE_USER_ABORT
  10. /**
  11. * Code coverage data collected during a single test.
  12. *
  13. * @var array
  14. */
  15. protected $coverage_data = array();
  16. /**
  17. * Process of last executed command.
  18. *
  19. * @var Process
  20. */
  21. private $process;
  22. /**
  23. * Default timeout for commands.
  24. *
  25. * @var int
  26. */
  27. private $defaultTimeout = 60;
  28. /**
  29. * Timeout for command.
  30. *
  31. * Reset to $defaultTimeout after executing a command.
  32. *
  33. * @var int
  34. */
  35. protected $timeout = 60;
  36. /**
  37. * Default idle timeout for commands.
  38. *
  39. * @var int
  40. */
  41. private $defaultIdleTimeout = 15;
  42. /**
  43. * Idle timeouts for commands.
  44. *
  45. * Reset to $defaultIdleTimeout after executing a command.
  46. *
  47. * @var int
  48. */
  49. protected $idleTimeout = 15;
  50. /**
  51. * Accessor for the last output, trimmed.
  52. *
  53. * @return string
  54. * Trimmed output as text.
  55. *
  56. * @access public
  57. */
  58. function getOutput() {
  59. return trim($this->getOutputRaw());
  60. }
  61. /**
  62. * Accessor for the last output, non-trimmed.
  63. *
  64. * @return string
  65. * Raw output as text.
  66. *
  67. * @access public
  68. */
  69. function getOutputRaw() {
  70. return $this->process ? $this->process->getOutput() : '';
  71. }
  72. /**
  73. * Accessor for the last output, rtrimmed and split on newlines.
  74. *
  75. * @return array
  76. * Output as array of lines.
  77. *
  78. * @access public
  79. */
  80. function getOutputAsList() {
  81. return array_map('rtrim', explode("\n", $this->getOutput()));
  82. }
  83. /**
  84. * Accessor for the last stderr output, trimmed.
  85. *
  86. * @return string
  87. * Trimmed stderr as text.
  88. *
  89. * @access public
  90. */
  91. function getErrorOutput() {
  92. return trim($this->getErrorOutputRaw());
  93. }
  94. /**
  95. * Accessor for the last stderr output, non-trimmed.
  96. *
  97. * @return string
  98. * Raw stderr as text.
  99. *
  100. * @access public
  101. */
  102. function getErrorOutputRaw() {
  103. return $this->process ? $this->process->getErrorOutput() : '';
  104. }
  105. /**
  106. * Accessor for the last stderr output, rtrimmed and split on newlines.
  107. *
  108. * @return array
  109. * Stderr as array of lines.
  110. *
  111. * @access public
  112. */
  113. function getErrorOutputAsList() {
  114. return array_map('rtrim', explode("\n", $this->getErrorOutput()));
  115. }
  116. /**
  117. * Accessor for the last output, decoded from json.
  118. *
  119. * @param string $key
  120. * Optionally return only a top level element from the json object.
  121. *
  122. * @return object
  123. * Decoded object.
  124. */
  125. function getOutputFromJSON($key = NULL) {
  126. $json = json_decode($this->getOutput());
  127. if (isset($key)) {
  128. $json = $json->{$key}; // http://stackoverflow.com/questions/2925044/hyphens-in-keys-of-object
  129. }
  130. return $json;
  131. }
  132. /**
  133. * Actually runs the command.
  134. *
  135. * @param string $command
  136. * The actual command line to run.
  137. * @param integer $expected_return
  138. * The return code to expect
  139. * @param sting cd
  140. * The directory to run the command in.
  141. * @param array $env
  142. * @todo: Not fully implemented yet. Inheriting environment is hard - http://stackoverflow.com/questions/3780866/why-is-my-env-empty.
  143. * @see drush_env().
  144. * Extra environment variables.
  145. * @param string $input
  146. * A string representing the STDIN that is piped to the command.
  147. * @return integer
  148. * Exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
  149. */
  150. function execute($command, $expected_return = self::EXIT_SUCCESS, $cd = NULL, $env = NULL, $input = NULL) {
  151. $return = 1;
  152. $this->tick();
  153. // Apply the environment variables we need for our test to the head of the
  154. // command (excludes Windows). Process does have an $env argument, but it replaces the entire
  155. // environment with the one given. This *could* be used for ensuring the
  156. // test ran with a clean environment, but it also makes tests fail hard on
  157. // Travis, for unknown reasons.
  158. // @see https://github.com/drush-ops/drush/pull/646
  159. $prefix = '';
  160. if($env && !$this->is_windows()) {
  161. foreach ($env as $env_name => $env_value) {
  162. $prefix .= $env_name . '=' . self::escapeshellarg($env_value) . ' ';
  163. }
  164. }
  165. $this->log("Executing: $command", 'warning');
  166. try {
  167. // Process uses a default timeout of 60 seconds, set it to 0 (none).
  168. $this->process = new Process($command, $cd, NULL, $input, 0);
  169. if (!getenv('UNISH_NO_TIMEOUTS')) {
  170. $this->process->setTimeout($this->timeout)
  171. ->setIdleTimeout($this->idleTimeout);
  172. }
  173. $return = $this->process->run();
  174. if ($expected_return !== $return) {
  175. $message = 'Unexpected exit code ' . $return . ' (expected ' . $expected_return . ") for command:\n" . $command;
  176. throw new UnishProcessFailedError($message, $this->process);
  177. }
  178. // Reset timeouts to default.
  179. $this->timeout = $this->defaultTimeout;
  180. $this->idleTimeout = $this->defaultIdleTimeout;
  181. return $return;
  182. }
  183. catch (ProcessTimedOutException $e) {
  184. if ($e->isGeneralTimeout()) {
  185. $message = 'Command runtime exceeded ' . $this->timeout . " seconds:\n" . $command;
  186. }
  187. else {
  188. $message = 'Command had no output for ' . $this->idleTimeout . " seconds:\n" . $command;
  189. }
  190. throw new UnishProcessFailedError($message, $this->process);
  191. }
  192. }
  193. /**
  194. * Invoke drush in via execute().
  195. *
  196. * @param command
  197. * A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
  198. * @param args
  199. * Command arguments.
  200. * @param $options
  201. * An associative array containing options.
  202. * @param $site_specification
  203. * A site alias or site specification. Include the '@' at start of a site alias.
  204. * @param $cd
  205. * A directory to change into before executing.
  206. * @param $expected_return
  207. * The expected exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
  208. * @param $suffix
  209. * Any code to append to the command. For example, redirection like 2>&1.
  210. * @param array $env
  211. * Environment variables to pass along to the subprocess. @todo - not used.
  212. * @return integer
  213. * An exit code.
  214. */
  215. function drush($command, array $args = array(), array $options = array(), $site_specification = NULL, $cd = NULL, $expected_return = self::EXIT_SUCCESS, $suffix = NULL, $env = array()) {
  216. $global_option_list = array('simulate', 'root', 'uri', 'include', 'config', 'alias-path', 'ssh-options', 'backend');
  217. $hide_stderr = FALSE;
  218. $cmd[] = UNISH_DRUSH;
  219. // Insert global options.
  220. foreach ($options as $key => $value) {
  221. if (in_array($key, $global_option_list)) {
  222. unset($options[$key]);
  223. if ($key == 'backend') {
  224. $hide_stderr = TRUE;
  225. $value = NULL;
  226. }
  227. if (!isset($value)) {
  228. $cmd[] = "--$key";
  229. }
  230. else {
  231. $cmd[] = "--$key=" . self::escapeshellarg($value);
  232. }
  233. }
  234. }
  235. if ($level = $this->log_level()) {
  236. $cmd[] = '--' . $level;
  237. }
  238. $cmd[] = "--nocolor";
  239. // Insert code coverage argument before command, in order for it to be
  240. // parsed as a global option. This matters for commands like ssh and rsync
  241. // where options after the command are passed along to external commands.
  242. $result = $this->getTestResultObject();
  243. if ($result->getCollectCodeCoverageInformation()) {
  244. $coverage_file = tempnam(UNISH_TMP, 'drush_coverage');
  245. if ($coverage_file) {
  246. $cmd[] = "--drush-coverage=" . $coverage_file;
  247. }
  248. }
  249. // Insert site specification and drush command.
  250. $cmd[] = empty($site_specification) ? NULL : self::escapeshellarg($site_specification);
  251. $cmd[] = $command;
  252. // Insert drush command arguments.
  253. foreach ($args as $arg) {
  254. $cmd[] = self::escapeshellarg($arg);
  255. }
  256. // insert drush command options
  257. foreach ($options as $key => $value) {
  258. if (!isset($value)) {
  259. $cmd[] = "--$key";
  260. }
  261. else {
  262. $cmd[] = "--$key=" . self::escapeshellarg($value);
  263. }
  264. }
  265. $cmd[] = $suffix;
  266. if ($hide_stderr) {
  267. $cmd[] = '2>' . $this->bit_bucket();
  268. }
  269. $exec = array_filter($cmd, 'strlen'); // Remove NULLs
  270. // Set sendmail_path to 'true' to disable any outgoing emails
  271. // that tests might cause Drupal to send.
  272. $php_options = (array_key_exists('PHP_OPTIONS', $env)) ? $env['PHP_OPTIONS'] . " " : "";
  273. // @todo The PHP Options below are not yet honored by execute(). See .travis.yml for an alternative way.
  274. $env['PHP_OPTIONS'] = "${php_options}-d sendmail_path='true'";
  275. $return = $this->execute(implode(' ', $exec), $expected_return, $cd, $env);
  276. // Save code coverage information.
  277. if (!empty($coverage_file)) {
  278. $data = unserialize(file_get_contents($coverage_file));
  279. unlink($coverage_file);
  280. // Save for appending after the test finishes.
  281. $this->coverage_data[] = $data;
  282. }
  283. return $return;
  284. }
  285. /**
  286. * Override the run method, so we can add in our code coverage data after the
  287. * test has run.
  288. *
  289. * We have to collect all coverage data, merge them and append them as one, to
  290. * avoid having phpUnit duplicating the test function as many times as drush
  291. * has been invoked.
  292. *
  293. * Runs the test case and collects the results in a TestResult object.
  294. * If no TestResult object is passed a new one will be created.
  295. *
  296. * @param PHPUnit_Framework_TestResult $result
  297. * @return PHPUnit_Framework_TestResult
  298. * @throws PHPUnit_Framework_Exception
  299. */
  300. public function run(\PHPUnit_Framework_TestResult $result = NULL) {
  301. $result = parent::run($result);
  302. $data = array();
  303. foreach ($this->coverage_data as $merge_data) {
  304. foreach ($merge_data as $file => $lines) {
  305. if (!isset($data[$file])) {
  306. $data[$file] = $lines;
  307. }
  308. else {
  309. foreach ($lines as $num => $executed) {
  310. if (!isset($data[$file][$num])) {
  311. $data[$file][$num] = $executed;
  312. }
  313. else {
  314. $data[$file][$num] = ($executed == 1 ? $executed : $data[$file][$num]);
  315. }
  316. }
  317. }
  318. }
  319. }
  320. // Reset coverage data.
  321. $this->coverage_data = array();
  322. if (!empty($data)) {
  323. $result->getCodeCoverage()->append($data, $this);
  324. }
  325. return $result;
  326. }
  327. /**
  328. * A slightly less functional copy of drush_backend_parse_output().
  329. */
  330. function parse_backend_output($string) {
  331. $regex = sprintf(UNISH_BACKEND_OUTPUT_DELIMITER, '(.*)');
  332. preg_match("/$regex/s", $string, $match);
  333. if (isset($match[1])) {
  334. // we have our JSON encoded string
  335. $output = $match[1];
  336. // remove the match we just made and any non printing characters
  337. $string = trim(str_replace(sprintf(UNISH_BACKEND_OUTPUT_DELIMITER, $match[1]), '', $string));
  338. }
  339. if (!empty($output)) {
  340. $data = json_decode($output, TRUE);
  341. if (is_array($data)) {
  342. return $data;
  343. }
  344. }
  345. return $string;
  346. }
  347. /**
  348. * Ensure that an expected log message appears in the Drush log.
  349. *
  350. * $this->drush('command', array(), array('backend' => NULL));
  351. * $parsed = $this->parse_backend_output($this->getOutput());
  352. * $this->assertLogHasMessage($parsed['log'], "Expected message", 'debug')
  353. *
  354. * @param $log Parsed log entries from backend invoke
  355. * @param $message The expected message that must be contained in
  356. * some log entry's 'message' field. Substrings will match.
  357. * @param $logType The type of log message to look for; all other
  358. * types are ignored. If FALSE (the default), then all log types
  359. * will be searched.
  360. */
  361. function assertLogHasMessage($log, $message, $logType = FALSE) {
  362. foreach ($log as $entry) {
  363. if (!$logType || ($entry['type'] == $logType)) {
  364. $logMessage = $this->getLogMessage($entry);
  365. if (strpos($logMessage, $message) !== FALSE) {
  366. return TRUE;
  367. }
  368. }
  369. }
  370. $this->fail("Could not find expected message in log: " . $message);
  371. }
  372. protected function getLogMessage($entry) {
  373. return $this->interpolate($entry['message'], $entry);
  374. }
  375. protected function interpolate($message, array $context)
  376. {
  377. // build a replacement array with braces around the context keys
  378. $replace = array();
  379. foreach ($context as $key => $val) {
  380. if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
  381. $replace[sprintf('{%s}', $key)] = $val;
  382. }
  383. }
  384. // interpolate replacement values into the message and return
  385. return strtr($message, $replace);
  386. }
  387. function drush_major_version() {
  388. static $major;
  389. if (!isset($major)) {
  390. $this->drush('version', array('drush_version'), array('pipe' => NULL));
  391. $version = trim($this->getOutput());
  392. list($major) = explode('.', $version);
  393. }
  394. return (int)$major;
  395. }
  396. }