00001 <?php
00002
00003
00008 class DrupalReporter extends SimpleReporter {
00009 var $_output_error = '';
00010 var $_character_set;
00011 var $_fails_stack = array(0);
00012 var $_exceptions_stack = array(0);
00013 var $_passes_stack = array(0);
00014 var $_progress_stack = array(0);
00015 var $test_info_stack = array();
00016 var $_output_stack_id = -1;
00017 var $form;
00018 var $form_depth = array();
00019 var $current_field_set = array();
00020 var $content_count = 0;
00021 var $weight = -10;
00022 var $test_stack = array();
00023
00024 function DrupalReporter($character_set = 'ISO-8859-1') {
00025 $this->SimpleReporter();
00026 drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
00027 $this->_character_set = $character_set;
00028 }
00029
00036 function paintHeader($test_name) {
00037
00038 }
00039
00046 function paintFooter($test_name) {
00047 $ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
00048 $class = $ok ? 'simpletest-pass' : 'simpletest-fail';
00049 $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '<strong> exceptions.');
00050 }
00051
00058 function paintPass($message, $group) {
00059 parent::paintPass($message);
00060 if ($group == 'Other') {
00061 $group = t($group);
00062 }
00063 $this->test_stack[] = array(
00064 'data' => array($message, "<strong>[$group]</strong>", t('Pass'), theme('image', 'misc/watchdog-ok.png')),
00065 'class' => 'simpletest-pass',
00066 );
00067 }
00068
00077 function paintFail($message, $group) {
00078 parent::paintFail($message);
00079 if ($group == 'Other') {
00080 $group = t($group);
00081 }
00082 $this->test_stack[] = array(
00083 'data' => array($message, "<strong>[$group]</strong>", t('Fail'), theme('image', 'misc/watchdog-error.png')),
00084 'class' => 'simpletest-fail',
00085 );
00086 }
00087
00088
00094 function paintError($message) {
00095 parent::paintError($message);
00096 $this->test_stack[] = array(
00097 'data' => array($message, '<strong>[PHP]</strong>', t('Exception'), theme('image', 'misc/watchdog-warning.png')),
00098 'class' => 'simpletest-exception',
00099 );
00100 }
00101
00111 function paintGroupStart($test_name, $size, $extra = '') {
00112 $this->_progress_stack[] = $this->_progress;
00113 $this->_progress = 0;
00114 $this->_exceptions_stack[] = $this->_exceptions;
00115 $this->_exceptions = 0;
00116 $this->_fails_stack[] = $this->_fails;
00117 $this->_fails = 0;
00118 $this->_passes_stack[] = $this->_passes;
00119 $this->_passes = 0;
00120 $this->form_depth[] = $test_name;
00121 $this->writeToLastField($this->form, array(
00122 '#type' => 'fieldset',
00123 '#title' => $test_name,
00124 '#weight' => $this->weight++,
00125 ), $this->form_depth);
00126
00127 if (! isset($this->_size)) {
00128 $this->_size = $size;
00129 }
00130
00131 if (($c = count($this->test_info_stack)) > 0) {
00132 $info = $this->test_info_stack[$c - 1];
00133 $this->writeContent('<strong>' . $info['name'] . '</strong>: ' . $info['description'], $this->getParentWeight() );
00134 }
00135
00136 $this->_test_stack[] = $test_name;
00137 }
00138
00139 function paintCaseStart($test_name) {
00140 $this->_progress++;
00141 $this->paintGroupStart($test_name, 1);
00142 }
00143
00144
00152 function paintGroupEnd($test_name) {
00153 array_pop($this->_test_stack);
00154 $ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
00155 $class = $ok ? "simpletest-pass" : "simpletest-fail";
00156 $parent_weight = $this->getParentWeight() - 0.5;
00157
00158 if (($this->_output_stack_id == 2) && ($this->_output_stack[$this->_output_stack_id]['size'] == 1)) {
00159 $this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
00160 $parent_weight = $this->getParentWeight() - 0.5;
00161 $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
00162 array_pop($this->form_depth);
00163 }
00164 else {
00165 $collapsed = $ok ? TRUE : FALSE;
00166 if ($this->getTestCaseProgress()) {
00167 $this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
00168 $use_grouping = FALSE;
00169 }
00170 else {
00171 $use_grouping = TRUE;
00172 }
00173 $write = array('#collapsible' => $use_grouping, '#collapsed' => $collapsed);
00174 $this->writeToLastField($this->form, $write, $this->form_depth);
00175 $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
00176 if (count($this->test_stack) != 0) {
00177 $this->writeContent(theme('table', array(), $this->test_stack));
00178 $this->test_stack = array();
00179 }
00180 array_pop($this->form_depth);
00181 }
00182
00183 $this->_progress += array_pop($this->_progress_stack);
00184 $this->_exceptions += array_pop($this->_exceptions_stack);
00185 $this->_fails += array_pop($this->_fails_stack);
00186 $this->_passes += array_pop($this->_passes_stack);
00187 }
00188
00189 function paintCaseEnd($test_name) {
00190 $this->paintGroupEnd($test_name);
00191 }
00192
00196 function getOutput() {
00197 return drupal_get_form('unit_tests', $this);
00198 }
00199
00203 function writeToLastField(&$form, $attr, $keys) {
00204 while(count($keys) != 0) {
00205 $value = array_shift($keys);
00206 if (isset($form[$value])) {
00207 if (count($keys) == 0) {
00208 $form[$value] += $attr;
00209 }
00210 else {
00211 $this->writeToLastField($form[$value], $attr, $keys);
00212 }
00213 $keys = array();
00214 }
00215 else {
00216 $form[$value] = $attr;
00217 }
00218 }
00219 }
00220
00225 function writeContent($msg, $weight = NULL, $class = 'simpletest') {
00226 if (!$weight) {
00227 $weight = $this->weight++;
00228 }
00229 $write['content' . $this->content_count++] = array(
00230 '#value' => '<div class=' . $class .'>' . $msg . '</div>',
00231 '#weight' => $weight,
00232 );
00233 $this->writeToLastField($this->form, $write, $this->form_depth);
00234 }
00235
00239 function getParentWeight($form = NULL, $keys = NULL ) {
00240 if (!isset($form)) {
00241 $form = $this->form;
00242 }
00243 if (!isset($keys)) {
00244 $keys = $this->form_depth;
00245 }
00246 if(count($keys) != 0) {
00247 $value = array_shift($keys);
00248 return $this->getParentWeight($form[$value], $keys);
00249 }
00250 return $form['#weight'];
00251 }
00252 }
00253
00254 function unit_tests($args, $reporter) {
00255 return $reporter->form['Drupal Unit Tests'];
00256 }
00257 ?>