function drush_test_xml_results

6.x test.drush.inc drush_test_xml_results($test_id, $dir, $info)
4.x test.drush.inc drush_test_xml_results($test_id, $dir)
5.x test.drush.inc drush_test_xml_results($test_id, $dir)
1 call to drush_test_xml_results()
simpletest_drush_run_test in commands/core/test.drush.inc
Run a single test and display any failure messages.

File

commands/core/test.drush.inc, line 203
Simpletest module drush integration.

Code

function drush_test_xml_results($test_id, $dir) {
  $dir = is_string($dir) ? $dir : '.';

  $results_map = array(
    'pass' => 'Pass',
    'fail' => 'Fail',
    'exception' => 'Exception',
  );

  if (drush_drupal_major_version() >= 7) {
    $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  }
  else {
    $result = db_query("SELECT * FROM {simpletest} WHERE test_id = %d ORDER BY test_class, message_id", $test_id);
    $results = array();
    while ($row = db_fetch_object($result)) {
      $results[] = $row;
    }
  }

  $test_class = '';
  $xml_files = array();

  foreach ($results as $result) {
    if (isset($results_map[$result->status])) {
      if ($result->test_class != $test_class) {
        // Display test class every time results are for new test class.
        if (isset($xml_files[$test_class])) {
          file_put_contents($dir . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
          unset($xml_files[$test_class]);
        }
        $test_class = $result->test_class;
        if (!isset($xml_files[$test_class])) {
          $doc = new DomDocument('1.0');
          $root = $doc->createElement('testsuite');
          $root = $doc->appendChild($root);
          $xml_files[$test_class] = array(
            'doc' => $doc,
            'suite' => $root,
          );
        }
      }
      // Save the result into the XML:
      $case = $xml_files[$test_class]['doc']->createElement('testcase');
      $case->setAttribute('classname', $test_class);
      list($class, $name) = explode('->', $result->function, 2);
      $case->setAttribute('name', $name);

      if ($result->status == 'fail') {
        $fail = $xml_files[$test_class]['doc']->createElement('failure');
        $fail->setAttribute('type', 'failure');
        $fail->setAttribute('message', $result->message_group);
        $text = $xml_files[$test_class]['doc']->createTextNode($result->message);
        $fail->appendChild($text);
        $case->appendChild($fail);
      }

      $xml_files[$test_class]['suite']->appendChild($case);
    }
  }

  // Save the last one:
  if (isset($xml_files[$test_class])) {
    file_put_contents($dir . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
    unset($xml_files[$test_class]);
  }
}