abstract class Drush_CommandTestCase

  1. 6.x tests/drush_testcase.inc Drush_CommandTestCase
  2. 5.x tests/drush_testcase.inc Drush_CommandTestCase

Hierarchy

Expanded class hierarchy of Drush_CommandTestCase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
Drush_CommandTestCase::drush function Invoke drush in via execute().
Drush_CommandTestCase::drush_major_version function
Drush_CommandTestCase::execute function Actually runs the command. Does not trap the error stream output as this need PHP 4.3+.
Drush_CommandTestCase::EXIT_ERROR constant
Drush_CommandTestCase::EXIT_SUCCESS constant
Drush_TestCase::bit_bucket function Borrowed from Drush. Checks operating system and returns supported bit bucket folder.
Drush_TestCase::convert_path function Converts a Windows path (dir1\dir2\dir3) into a Unix path (dir1/dir2/dir3). Also converts a cygwin "drive emulation" path (/cygdrive/c/dir1) into a proper drive path, still with Unix slashes (c:/dir1).
Drush_TestCase::db_driver function
Drush_TestCase::db_url function
Drush_TestCase::directory_cache function
Drush_TestCase::drupalSitewideDirectory function
Drush_TestCase::escapeshellarg public static function
Drush_TestCase::fetchInstallDrupal function
Drush_TestCase::getOutput function Accessor for the last output.
Drush_TestCase::getOutputAsList function Accessor for the last output.
Drush_TestCase::getOutputFromJSON function Accessor for the last output, decoded from json.
Drush_TestCase::get_tar_executable public static function
Drush_TestCase::is_windows public static function
Drush_TestCase::log function Print a log message to the console.
Drush_TestCase::log_level function
Drush_TestCase::randomString public function Helper function to generate a random string of arbitrary length.
Drush_TestCase::setUpBeforeClass public static function Assure that each class starts with an empty sandbox directory and a clean environment - http://drupal.org/node/1103568.
Drush_TestCase::setUpDrupal function
Drush_TestCase::setUpFreshSandBox public static function Remove any pre-existing sandbox, then create a new one.
Drush_TestCase::tearDownAfterClass public static function Runs after all tests in a class are run. Remove sandbox directory.
Drush_TestCase::webroot function
Drush_TestCase::_escapeshellarg_windows public static function
Drush_TestCase::__construct function

File

tests/drush_testcase.inc, line 342
Initialize a sandboxed environment. Starts with call unish_init() at bottom.

View source
abstract class Drush_CommandTestCase extends Drush_TestCase {

  // Unix exit codes.
  const EXIT_SUCCESS = 0;
  const EXIT_ERROR = 1;
  /**
   * An array of Drupal sites that are setup in the drush-sandbox.
   */
  var $sites;

  /**
   * Actually runs the command. Does not trap the error stream output as this
   * need PHP 4.3+.
   *
   * @param string $command
   *   The actual command line to run.
   * @param integer $expected_return
   *   The return code to expect
   * @param array $env
   *   Extra environment variables
   * @return integer
   *   Exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
   */
  function execute($command, $expected_return = self::EXIT_SUCCESS, $env = array()) {
    $this->_output = FALSE;
    $return = 1;
    $this->log("Executing: $command", 'notice');

    // Apply the environment variables we need for our test
    // to the head of the command
    $prefix = '';
    foreach ($env as $env_name => $env_value) {
      $prefix .= $env_name . '=' . self::escapeshellarg($env_value) . ' ';
    }
    exec($prefix . $command, $this->_output, $return);

    $this->assertEquals($expected_return, $return, 'Unexpected exit code: ' . $command);
    return $return;
  }

  /**
   * Invoke drush in via execute().
   *
   * @param command
   *   A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
   * @param args
   *   Command arguments.
   * @param $options
   *   An associative array containing options.
   * @param $site_specification
   *   A site alias or site specification. Include the '@' at start of a site alias.
   * @param $cd
   *   A directory to change into before executing.
   * @param $expected_return
   *   The expected exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
   * @param $suffix
   *   Any code to append to the command. For example, redirection like 2>&1.
   * @return integer
   *   An exit code.
   */
  function drush($command, array $args = array(), array $options = array(), $site_specification = NULL, $cd = NULL, $expected_return = self::EXIT_SUCCESS, $suffix = NULL, $env = array()) {
    $global_option_list = array(
      'simulate',
      'root',
      'uri',
      'include',
      'config',
      'alias-path',
      'ssh-options',
      'backend',
    );
    $hide_stderr = FALSE;
    // insert "cd ... ; drush"
    $cmd[] = $cd ? sprintf('cd %s &&', self::escapeshellarg($cd)) : NULL;
    $cmd[] = UNISH_DRUSH;

    // insert global options
    foreach ($options as $key => $value) {
      if (in_array($key, $global_option_list)) {
        unset($options[$key]);
        if ($key == 'backend') {
          $hide_stderr = TRUE;
          $value = NULL;
        }
        if (!isset($value)) {
          $cmd[] = "--$key";
        }
        else {
          $cmd[] = "--$key=" . self::escapeshellarg($value);
        }
      }
    }

    if ($level = $this->log_level()) {
      $cmd[] = '--' . $level;
    }
    $cmd[] = "--nocolor";

    // insert site specification and drush command
    $cmd[] = empty($site_specification) ? NULL : self::escapeshellarg($site_specification);
    $cmd[] = $command;

    // insert drush command arguments
    foreach ($args as $arg) {
      $cmd[] = self::escapeshellarg($arg);
    }
    // insert drush command options
    foreach ($options as $key => $value) {
      if (!isset($value)) {
        $cmd[] = "--$key";
      }
      else {
        $cmd[] = "--$key=" . self::escapeshellarg($value);
      }
    }
    $cmd[] = $suffix;
    if ($hide_stderr) {
      $cmd[] = '2>/dev/null';
    }
    $exec = array_filter($cmd, 'strlen'); // Remove NULLs
    return $this->execute(implode(' ', $exec), $expected_return, $env);
  }

  function drush_major_version() {
    static $major;

    if (!isset($major)) {
      $this->drush('version', array('drush_version'), array('pipe' => NULL));
      $version = $this->getOutput();
      list($major) = explode('.', $version);
    }
    return $major;
  }
}