5 use Symfony\Component\Process\Process;
6 use Symfony\Component\Process\Exception\ProcessTimedOutException;
8 abstract class CommandUnishTestCase extends UnishTestCase {
11 const EXIT_SUCCESS = 0;
13 const UNISH_EXITCODE_USER_ABORT = 75; // Same as DRUSH_EXITCODE_USER_ABORT
16 * Code coverage data collected during a single test.
20 protected $coverage_data = array();
23 * Process of last executed command.
30 * Default timeout for commands.
34 private $defaultTimeout = 60;
37 * Timeout for command.
39 * Reset to $defaultTimeout after executing a command.
43 protected $timeout = 60;
46 * Default idle timeout for commands.
50 private $defaultIdleTimeout = 15;
53 * Idle timeouts for commands.
55 * Reset to $defaultIdleTimeout after executing a command.
59 protected $idleTimeout = 15;
62 * Accessor for the last output, trimmed.
65 * Trimmed output as text.
69 function getOutput() {
70 return trim($this->getOutputRaw());
74 * Accessor for the last output, non-trimmed.
81 function getOutputRaw() {
82 return $this->process ? $this->process->getOutput() : '';
86 * Accessor for the last output, rtrimmed and split on newlines.
89 * Output as array of lines.
93 function getOutputAsList() {
94 return array_map('rtrim', explode("\n", $this->getOutput()));
98 * Accessor for the last stderr output, trimmed.
101 * Trimmed stderr as text.
105 function getErrorOutput() {
106 return trim($this->getErrorOutputRaw());
110 * Accessor for the last stderr output, non-trimmed.
113 * Raw stderr as text.
117 function getErrorOutputRaw() {
118 return $this->process ? $this->process->getErrorOutput() : '';
122 * Accessor for the last stderr output, rtrimmed and split on newlines.
125 * Stderr as array of lines.
129 function getErrorOutputAsList() {
130 return array_map('rtrim', explode("\n", $this->getErrorOutput()));
134 * Accessor for the last output, decoded from json.
137 * Optionally return only a top level element from the json object.
142 function getOutputFromJSON($key = NULL) {
143 $json = json_decode($this->getOutput());
145 $json = $json->{$key}; // http://stackoverflow.com/questions/2925044/hyphens-in-keys-of-object
151 * Actually runs the command.
153 * @param string $command
154 * The actual command line to run.
155 * @param integer $expected_return
156 * The return code to expect
158 * The directory to run the command in.
160 * @todo: Not fully implemented yet. Inheriting environment is hard - http://stackoverflow.com/questions/3780866/why-is-my-env-empty.
162 * Extra environment variables.
163 * @param string $input
164 * A string representing the STDIN that is piped to the command.
166 * Exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
168 function execute($command, $expected_return = self::EXIT_SUCCESS, $cd = NULL, $env = NULL, $input = NULL) {
172 // Apply the environment variables we need for our test to the head of the
173 // command (excludes Windows). Process does have an $env argument, but it replaces the entire
174 // environment with the one given. This *could* be used for ensuring the
175 // test ran with a clean environment, but it also makes tests fail hard on
176 // Travis, for unknown reasons.
177 // @see https://github.com/drush-ops/drush/pull/646
179 if($env && !$this->is_windows()) {
180 foreach ($env as $env_name => $env_value) {
181 $prefix .= $env_name . '=' . self::escapeshellarg($env_value) . ' ';
184 $this->log("Executing: $command", 'warning');
187 // Process uses a default timeout of 60 seconds, set it to 0 (none).
188 $this->process = new Process($command, $cd, NULL, $input, 0);
189 if (!getenv('UNISH_NO_TIMEOUTS')) {
190 $this->process->setTimeout($this->timeout)
191 ->setIdleTimeout($this->idleTimeout);
193 $return = $this->process->run();
194 if ($expected_return !== $return) {
195 $message = 'Unexpected exit code ' . $return . ' (expected ' . $expected_return . ") for command:\n" . $command;
196 throw new UnishProcessFailedError($message, $this->process);
198 // Reset timeouts to default.
199 $this->timeout = $this->defaultTimeout;
200 $this->idleTimeout = $this->defaultIdleTimeout;
203 catch (ProcessTimedOutException $e) {
204 if ($e->isGeneralTimeout()) {
205 $message = 'Command runtime exceeded ' . $this->timeout . " seconds:\n" . $command;
208 $message = 'Command had no output for ' . $this->idleTimeout . " seconds:\n" . $command;
210 throw new UnishProcessFailedError($message, $this->process);
215 * Invoke drush in via execute().
218 * A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
222 * An associative array containing options.
223 * @param $site_specification
224 * A site alias or site specification. Include the '@' at start of a site alias.
226 * A directory to change into before executing.
227 * @param $expected_return
228 * The expected exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
230 * Any code to append to the command. For example, redirection like 2>&1.
232 * Environment variables to pass along to the subprocess. @todo - not used.
236 function drush($command, array $args = array(), array $options = array(), $site_specification = NULL, $cd = NULL, $expected_return = self::EXIT_SUCCESS, $suffix = NULL, $env = array()) {
237 $global_option_list = array('simulate', 'root', 'uri', 'include', 'config', 'alias-path', 'ssh-options', 'backend');
238 $hide_stderr = FALSE;
239 $cmd[] = UNISH_DRUSH;
241 // Insert global options.
242 foreach ($options as $key => $value) {
243 if (in_array($key, $global_option_list)) {
244 unset($options[$key]);
245 if ($key == 'backend') {
249 if (!isset($value)) {
253 $cmd[] = "--$key=" . self::escapeshellarg($value);
258 if ($level = $this->log_level()) {
259 $cmd[] = '--' . $level;
261 $cmd[] = "--nocolor";
263 // Insert code coverage argument before command, in order for it to be
264 // parsed as a global option. This matters for commands like ssh and rsync
265 // where options after the command are passed along to external commands.
266 $result = $this->getTestResultObject();
267 if ($result->getCollectCodeCoverageInformation()) {
268 $coverage_file = tempnam(UNISH_TMP, 'drush_coverage');
269 if ($coverage_file) {
270 $cmd[] = "--drush-coverage=" . $coverage_file;
274 // Insert site specification and drush command.
275 $cmd[] = empty($site_specification) ? NULL : self::escapeshellarg($site_specification);
278 // Insert drush command arguments.
279 foreach ($args as $arg) {
280 $cmd[] = self::escapeshellarg($arg);
282 // insert drush command options
283 foreach ($options as $key => $value) {
284 if (!isset($value)) {
288 $cmd[] = "--$key=" . self::escapeshellarg($value);
294 $cmd[] = '2>' . $this->bit_bucket();
296 $exec = array_filter($cmd, 'strlen'); // Remove NULLs
297 // Set sendmail_path to 'true' to disable any outgoing emails
298 // that tests might cause Drupal to send.
300 $php_options = (array_key_exists('PHP_OPTIONS', $env)) ? $env['PHP_OPTIONS'] . " " : "";
301 // @todo The PHP Options below are not yet honored by execute(). See .travis.yml for an alternative way.
302 $env['PHP_OPTIONS'] = "${php_options}-d sendmail_path='true'";
303 $return = $this->execute(implode(' ', $exec), $expected_return, $cd, $env);
305 // Save code coverage information.
306 if (!empty($coverage_file)) {
307 $data = unserialize(file_get_contents($coverage_file));
308 unlink($coverage_file);
309 // Save for appending after the test finishes.
310 $this->coverage_data[] = $data;
317 * Override the run method, so we can add in our code coverage data after the
320 * We have to collect all coverage data, merge them and append them as one, to
321 * avoid having phpUnit duplicating the test function as many times as drush
324 * Runs the test case and collects the results in a TestResult object.
325 * If no TestResult object is passed a new one will be created.
327 * @param PHPUnit_Framework_TestResult $result
328 * @return PHPUnit_Framework_TestResult
329 * @throws PHPUnit_Framework_Exception
331 public function run(\PHPUnit_Framework_TestResult $result = NULL) {
332 $result = parent::run($result);
334 foreach ($this->coverage_data as $merge_data) {
335 foreach ($merge_data as $file => $lines) {
336 if (!isset($data[$file])) {
337 $data[$file] = $lines;
340 foreach ($lines as $num => $executed) {
341 if (!isset($data[$file][$num])) {
342 $data[$file][$num] = $executed;
345 $data[$file][$num] = ($executed == 1 ? $executed : $data[$file][$num]);
352 // Reset coverage data.
353 $this->coverage_data = array();
355 $result->getCodeCoverage()->append($data, $this);
361 * A slightly less functional copy of drush_backend_parse_output().
363 function parse_backend_output($string) {
364 $regex = sprintf(UNISH_BACKEND_OUTPUT_DELIMITER, '(.*)');
365 preg_match("/$regex/s", $string, $match);
366 if (isset($match[1])) {
367 // we have our JSON encoded string
369 // remove the match we just made and any non printing characters
370 $string = trim(str_replace(sprintf(UNISH_BACKEND_OUTPUT_DELIMITER, $match[1]), '', $string));
373 if (!empty($output)) {
374 $data = json_decode($output, TRUE);
375 if (is_array($data)) {
383 * Ensure that an expected log message appears in the Drush log.
385 * $this->drush('command', array(), array('backend' => NULL));
386 * $parsed = $this->parse_backend_output($this->getOutput());
387 * $this->assertLogHasMessage($parsed['log'], "Expected message", 'debug')
389 * @param $log Parsed log entries from backend invoke
390 * @param $message The expected message that must be contained in
391 * some log entry's 'message' field. Substrings will match.
392 * @param $logType The type of log message to look for; all other
393 * types are ignored. If FALSE (the default), then all log types
396 function assertLogHasMessage($log, $message, $logType = FALSE) {
397 foreach ($log as $entry) {
398 if (!$logType || ($entry['type'] == $logType)) {
399 if (strpos($entry['message'], $message) !== FALSE) {
404 $this->fail("Could not find expected message in log: " . $message);
407 function drush_major_version() {
410 if (!isset($major)) {
411 $this->drush('version', array('drush_version'), array('pipe' => NULL));
412 $version = trim($this->getOutput());
413 list($major) = explode('.', $version);