Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / tests / Unish / CommandUnishTestCase.php
1 <?php
2
3 namespace Unish;
4
5 use Symfony\Component\Process\Process;
6 use Symfony\Component\Process\Exception\ProcessTimedOutException;
7
8 abstract class CommandUnishTestCase extends UnishTestCase {
9
10   // Unix exit codes.
11   const EXIT_SUCCESS  = 0;
12   const EXIT_ERROR = 1;
13   const UNISH_EXITCODE_USER_ABORT = 75; // Same as DRUSH_EXITCODE_USER_ABORT
14
15   /**
16    * Code coverage data collected during a single test.
17    *
18    * @var array
19    */
20   protected $coverage_data = array();
21
22   /**
23    * Process of last executed command.
24    *
25    * @var Process
26    */
27   private $process;
28
29   /**
30    * Default timeout for commands.
31    *
32    * @var int
33    */
34   private $defaultTimeout = 60;
35
36   /**
37    * Timeout for command.
38    *
39    * Reset to $defaultTimeout after executing a command.
40    *
41    * @var int
42    */
43   protected $timeout = 60;
44
45   /**
46    * Default idle timeout for commands.
47    *
48    * @var int
49    */
50   private $defaultIdleTimeout = 15;
51
52   /**
53    * Idle timeouts for commands.
54    *
55    * Reset to $defaultIdleTimeout after executing a command.
56    *
57    * @var int
58    */
59   protected $idleTimeout = 15;
60
61   /**
62    * Accessor for the last output, trimmed.
63    *
64    * @return string
65    *   Trimmed output as text.
66    *
67    * @access public
68    */
69   function getOutput() {
70     return trim($this->getOutputRaw());
71   }
72
73   /**
74    * Accessor for the last output, non-trimmed.
75    *
76    * @return string
77    *   Raw output as text.
78    *
79    * @access public
80    */
81   function getOutputRaw() {
82     return $this->process ? $this->process->getOutput() : '';
83   }
84
85   /**
86    * Accessor for the last output, rtrimmed and split on newlines.
87    *
88    * @return array
89    *   Output as array of lines.
90    *
91    * @access public
92    */
93   function getOutputAsList() {
94     return array_map('rtrim', explode("\n", $this->getOutput()));
95   }
96
97   /**
98    * Accessor for the last stderr output, trimmed.
99    *
100    * @return string
101    *   Trimmed stderr as text.
102    *
103    * @access public
104    */
105   function getErrorOutput() {
106     return trim($this->getErrorOutputRaw());
107   }
108
109   /**
110    * Accessor for the last stderr output, non-trimmed.
111    *
112    * @return string
113    *   Raw stderr as text.
114    *
115    * @access public
116    */
117   function getErrorOutputRaw() {
118     return $this->process ? $this->process->getErrorOutput() : '';
119   }
120
121   /**
122    * Accessor for the last stderr output, rtrimmed and split on newlines.
123    *
124    * @return array
125    *   Stderr as array of lines.
126    *
127    * @access public
128    */
129   function getErrorOutputAsList() {
130     return array_map('rtrim', explode("\n", $this->getErrorOutput()));
131   }
132
133   /**
134    * Accessor for the last output, decoded from json.
135    *
136    * @param string $key
137    *   Optionally return only a top level element from the json object.
138    *
139    * @return object
140    *   Decoded object.
141    */
142   function getOutputFromJSON($key = NULL) {
143     $json = json_decode($this->getOutput());
144     if (isset($key)) {
145       $json = $json->{$key}; // http://stackoverflow.com/questions/2925044/hyphens-in-keys-of-object
146     }
147     return $json;
148   }
149
150   /**
151    * Actually runs the command.
152    *
153    * @param string $command
154    *   The actual command line to run.
155    * @param integer $expected_return
156    *   The return code to expect
157    * @param sting cd
158    *   The directory to run the command in.
159    * @param array $env
160    *  @todo: Not fully implemented yet. Inheriting environment is hard - http://stackoverflow.com/questions/3780866/why-is-my-env-empty.
161    *         @see drush_env().
162    *  Extra environment variables.
163    * @param string $input
164    *   A string representing the STDIN that is piped to the command.
165    * @return integer
166    *   Exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
167    */
168   function execute($command, $expected_return = self::EXIT_SUCCESS, $cd = NULL, $env = NULL, $input = NULL) {
169     $return = 1;
170     $this->tick();
171
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
178     $prefix = '';
179     if($env && !$this->is_windows()) {
180       foreach ($env as $env_name => $env_value) {
181         $prefix .= $env_name . '=' . self::escapeshellarg($env_value) . ' ';
182       }
183     }
184     $this->log("Executing: $command", 'warning');
185
186     try {
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);
192       }
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);
197       }
198       // Reset timeouts to default.
199       $this->timeout = $this->defaultTimeout;
200       $this->idleTimeout = $this->defaultIdleTimeout;
201       return $return;
202     }
203     catch (ProcessTimedOutException $e) {
204       if ($e->isGeneralTimeout()) {
205         $message = 'Command runtime exceeded ' . $this->timeout . " seconds:\n" .  $command;
206       }
207       else {
208         $message = 'Command had no output for ' . $this->idleTimeout . " seconds:\n" .  $command;
209       }
210       throw new UnishProcessFailedError($message, $this->process);
211     }
212   }
213
214   /**
215    * Invoke drush in via execute().
216    *
217    * @param command
218     *   A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
219     * @param args
220     *   Command arguments.
221     * @param $options
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.
225     * @param $cd
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.
229     * @param $suffix
230     *   Any code to append to the command. For example, redirection like 2>&1.
231     * @param array $env
232    *   Environment variables to pass along to the subprocess. @todo - not used.
233     * @return integer
234     *   An exit code.
235     */
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;
240
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') {
246           $hide_stderr = TRUE;
247           $value = NULL;
248         }
249         if (!isset($value)) {
250           $cmd[] = "--$key";
251         }
252         else {
253           $cmd[] = "--$key=" . self::escapeshellarg($value);
254         }
255       }
256     }
257
258     if ($level = $this->log_level()) {
259       $cmd[] = '--' . $level;
260     }
261     $cmd[] = "--nocolor";
262
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;
271       }
272     }
273
274     // Insert site specification and drush command.
275     $cmd[] = empty($site_specification) ? NULL : self::escapeshellarg($site_specification);
276     $cmd[] = $command;
277
278     // Insert drush command arguments.
279     foreach ($args as $arg) {
280       $cmd[] = self::escapeshellarg($arg);
281     }
282     // insert drush command options
283     foreach ($options as $key => $value) {
284       if (!isset($value)) {
285         $cmd[] = "--$key";
286       }
287       else {
288         $cmd[] = "--$key=" . self::escapeshellarg($value);
289       }
290     }
291
292     $cmd[] = $suffix;
293     if ($hide_stderr) {
294       $cmd[] = '2>' . $this->bit_bucket();
295     }
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.
299
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);
304
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;
311     }
312
313     return $return;
314   }
315
316   /**
317    * Override the run method, so we can add in our code coverage data after the
318    * test has run.
319    *
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
322    * has been invoked.
323    *
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.
326    *
327    * @param  PHPUnit_Framework_TestResult $result
328    * @return PHPUnit_Framework_TestResult
329    * @throws PHPUnit_Framework_Exception
330    */
331   public function run(\PHPUnit_Framework_TestResult $result = NULL) {
332     $result = parent::run($result);
333     $data = array();
334     foreach ($this->coverage_data as $merge_data) {
335       foreach ($merge_data as $file => $lines) {
336         if (!isset($data[$file])) {
337           $data[$file] = $lines;
338         }
339         else {
340           foreach ($lines as $num => $executed) {
341             if (!isset($data[$file][$num])) {
342               $data[$file][$num] = $executed;
343             }
344             else {
345               $data[$file][$num] = ($executed == 1 ? $executed : $data[$file][$num]);
346             }
347           }
348         }
349       }
350     }
351
352     // Reset coverage data.
353     $this->coverage_data = array();
354     if (!empty($data)) {
355       $result->getCodeCoverage()->append($data, $this);
356     }
357     return $result;
358   }
359
360   /**
361    * A slightly less functional copy of drush_backend_parse_output().
362    */
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
368       $output = $match[1];
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));
371     }
372
373     if (!empty($output)) {
374       $data = json_decode($output, TRUE);
375       if (is_array($data)) {
376         return $data;
377       }
378     }
379     return $string;
380   }
381
382   /**
383    * Ensure that an expected log message appears in the Drush log.
384    *
385    *     $this->drush('command', array(), array('backend' => NULL));
386    *     $parsed = $this->parse_backend_output($this->getOutput());
387    *     $this->assertLogHasMessage($parsed['log'], "Expected message", 'debug')
388    *
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
394    *   will be searched.
395    */
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) {
400           return TRUE;
401         }
402       }
403     }
404     $this->fail("Could not find expected message in log: " . $message);
405   }
406
407   function drush_major_version() {
408     static $major;
409
410     if (!isset($major)) {
411       $this->drush('version', array('drush_version'), array('pipe' => NULL));
412       $version = trim($this->getOutput());
413       list($major) = explode('.', $version);
414     }
415     return (int)$major;
416   }
417 }