class backendCase

  1. 8.0.x tests/backendTest.php backendCase
  2. 6.x tests/backendTest.php backendCase
  3. 7.x tests/backendTest.php backendCase
  4. 4.x tests/backendTest.php backendCase
  5. 5.x tests/backendTest.php backendCase
  6. master tests/backendTest.php backendCase

Hierarchy

Expanded class hierarchy of backendCase

Members

Contains filters are case sensitive
Name Modifierssort ascending Type Description
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::tearDownAfterClass public static function Runs after each test case. Remove sandbox directory.
Drush_TestCase::is_windows public static function
Drush_TestCase::escapeshellarg public static function
Drush_TestCase::_escapeshellarg_windows public static function
backendCase::testOrigin function
backendCase::testTarget function
backendCase::parse function
Drush_TestCase::__construct function
Drush_TestCase::execute function Actually runs the command. Does not trap the error stream output as this need PHP 4.3+.
Drush_TestCase::drush function Invoke drush in via execute().
Drush_TestCase::getOutput function Accessor for the last output.
Drush_TestCase::getOutputAsList function Accessor for the last output.
Drush_TestCase::setUpDrupal function
Drush_TestCase::drush_major_version function
Drush_TestCase::create_node_types_php function
Drush_TestCase::file_aliases function
Drush_TestCase::file_delete_recursive function Same code as drush_delete_dir().
backendCase::DRUSH_BACKEND_OUTPUT_DELIMITER constant
Drush_TestCase::EXIT_SUCCESS constant
Drush_TestCase::EXIT_ERROR constant

File

tests/backendTest.php, line 16

View source
class backendCase extends Drush_TestCase {
  const DRUSH_BACKEND_OUTPUT_DELIMITER = 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END';

  /*
   * Covers the following origin responsibilities.
   *   - A remote host is recognized in site specification.
   *   - Generates expected ssh command.
   *
   * General handling of site aliases will be in sitealiasTest.php.
   */
  function testOrigin() {
    $exec = sprintf('%s %s version --simulate --ssh-options=%s | grep ssh', self::escapeshellarg(UNISH_DRUSH), self::escapeshellarg('user@server/path/to/drupal#sitename'), self::escapeshellarg('-i mysite_dsa'));
    $this->execute($exec);
    // $expected might be different on non unix platforms. We shall see.
    $expected = "proc_open: ssh -i mysite_dsa 'user'@'server' 'drush  --uri='\''sitename'\'' --root='\''/path/to/drupal'\'' --simulate version --backend 2>&1'  2>&1";
    $output = $this->getOutput();
    $this->assertEquals($expected, $output, 'Expected ssh command was built');
  }

  /*
   * Covers the following target responsibilities.
   *   - Interpret stdin as options as per REST API.
   *   - Successfully execute specified command.
   *   - JSON object has expected contents (including errors).
   *   - JSON object is wrapped in expected delimiters.
   */
  function testTarget() {
    $stdin = json_encode(array('filter' => 'sql'));
    $exec = sprintf('echo %s | %s help --backend', self::escapeshellarg($stdin), self::escapeshellarg(UNISH_DRUSH));
    $this->execute($exec);
    $parsed = $this->parse($this->getOutput());
    $this->assertTrue((bool) $parsed, 'Successfully parsed backend output');
    $this->assertArrayHasKey('log', $parsed);
    $this->assertArrayHasKey('output', $parsed);
    $this->assertArrayHasKey('object', $parsed);
    $this->assertEquals(self::EXIT_SUCCESS, $parsed['error_status']);
    // This assertion shows that `help` was called and that stdin options were respected.
    $this->assertStringStartsWith('SQL commands', $parsed['output']);
    $this->assertEquals('Bootstrap to phase 0.', $parsed['log'][0]['message']);

    // Check error propogation by requesting an invalid command (missing Drupal site).
    $exec = sprintf('%s core-cron --backend 2>/dev/null', self::escapeshellarg(UNISH_DRUSH));
    $this->execute($exec, self::EXIT_ERROR);
    $parsed = $this->parse($this->getOutput());
    $this->assertEquals(1, $parsed['error_status']);
    $this->assertArrayHasKey('DRUSH_NO_DRUPAL_ROOT', $parsed['error_log']);
  }

  /*
   * A slightly less functional copy of drush_backend_parse_output().
   */
  function parse($string) {
    $regex = sprintf(self::DRUSH_BACKEND_OUTPUT_DELIMITER, '(.*)');
    preg_match("/$regex/s", $string, $match);
    if ($match[1]) {
      // we have our JSON encoded string
      $output = $match[1];
      // remove the match we just made and any non printing characters
      $string = trim(str_replace(sprintf(self::DRUSH_BACKEND_OUTPUT_DELIMITER, $match[1]), '', $string));
    }

    if ($output) {
      $data = json_decode($output, TRUE);
      if (is_array($data)) {
        return $data;
      }
    }
    return $string;
  }
}