Spike PHPCoverage Details: drupal_reporter.php

Line #FrequencySource Line
1 <?php
2 // $Id: drupal_reporter.php,v 1.8 2008/03/23 03:35:38 boombatower Exp $
3 
4 /**
5  * Minimal drupal displayer. Accumulates output to $_output.
6  * Based on HtmlReporter by Marcus Baker
7  */
8 class DrupalReporter extends SimpleReporter {
9   var $_output_error = '';
10   var $_character_set;
11   var $_fails_stack      = array(0);
12   var $_exceptions_stack = array(0);
13   var $_passes_stack     = array(0);
14   var $_progress_stack   = array(0);
15   var $test_info_stack   = array();
16   var $_output_stack_id  = -1;
17   var $form;
18   var $form_depth = array();
19   var $current_field_set = array();
20   var $content_count = 0;
21   var $weight = -10;
22   var $test_stack = array();
23 
24   function DrupalReporter($character_set = 'ISO-8859-1') {
251    $this->SimpleReporter();
261    drupal_add_css(drupal_get_path('module', 'simpletest') .'/simpletest.css');
271    $this->_character_set = $character_set;
28   }
29 
30   /**
31    * Paints the top of the web page setting the
32    * title to the name of the starting test.
33    * @param string $test_name      Name class of test.
34    * @access public
35    **/
36   function paintHeader($test_name) {
37 
38   }
39 
40   /**
41    * Paints the end of the test with a summary of
42    * the passes and failures.
43    * @param string $test_name        Name class of test.
44    * @access public
45    */
46   function paintFooter($test_name) {
47     $ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
48     $class = $ok ? 'simpletest-pass' : 'simpletest-fail';
49     $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '<strong> exceptions.');
50   }
51 
52   /**
53    * Paints the test passes
54    * @param string $message    Failure message displayed in
55    *                           the context of the other tests.
56    * @access public
57    **/
58   function paintPass($message) {
591    parent::paintPass($message);
601    $this->test_stack[] = array(
61       'data' => array($this->_htmlEntities($message), 'OK'),
62       'class' => 'simpletest-pass',
63     );
64     //$this->writeContent($this->_htmlEntities($message). ' OK', NULL, 'simpletest-pass');
65   }
66 
67   /**
68    * Paints the test failure with a breadcrumbs
69    * trail of the nesting test suites below the
70    * top level test.
71    * @param string $message    Failure message displayed in
72    *                           the context of the other tests.
73    * @access public
74    */
75   function paintFail($message) {
76     parent::paintFail($message);
77     $this->test_stack[] = array(
78       'data' => array($this->_htmlEntities($message), 'FAIL'),
79       'class' => 'simpletest-fail',
80     );
81     //$this->writeContent($this->_htmlEntities($message). ' FAIL', NULL, 'simpletest-fail');
82   }
83 
84 
85   /**
86    * Paints a PHP error or exception.
87    * @param string $message        Message is ignored.
88    * @access public
89    **/
90   function paintError($message) {
91     parent::paintError($message);
92     $this->test_stack[] = array(
93       'data' => array($this->_htmlEntities($message), 'EXCEPTION'),
94       'class' => 'simpletest-fail',
95     );
96     //$this->writeContent($this->_htmlEntities($message). ' EXCEPTION', NULL, 'simpletest-fail');
97   }
98 
99   /**
100    * Paints the start of a group test. Will also paint
101    * the page header and footer if this is the
102    * first test. Will stash the size if the first
103    * start.
104    * @param string  $test_name   Name of test that is starting.
105    * @param integer $size       Number of test cases starting.
106    * @access public
107    */
108   function paintGroupStart($test_name, $size, $extra = '') {
1091    $this->_progress_stack[] = $this->_progress;
1101    $this->_progress = 0;
1111    $this->_exceptions_stack[] = $this->_exceptions;
1121    $this->_exceptions = 0;
1131    $this->_fails_stack[] = $this->_fails;
1141    $this->_fails = 0;
1151    $this->_passes_stack[] = $this->_passes;
1161    $this->_passes = 0;
1171    $this->form_depth[] = $test_name;
1181    $this->writeToLastField($this->form, array(
119       '#type' => 'fieldset',
120       '#title' => $test_name,
121       '#weight' => $this->weight++,
122     ), $this->form_depth);
123 
1241    if (! isset($this->_size)) {
1251      $this->_size = $size;
126     }
127 
1281    if (($c = count($this->test_info_stack)) > 0) {
1291      $info = $this->test_info_stack[$c - 1];
1301      $this->writeContent('<strong>' . $info['name'] . '</strong>: ' . $info['description'], $this->getParentWeight() );
131     }
132 
1331    $this->_test_stack[] = $test_name;
134   }
135 
136   function paintCaseStart($test_name) {
1371    $this->_progress++;
1381    $this->paintGroupStart($test_name, 1);
139   }
140 
141 
142   /**
143    * Paints the end of a group test. Will paint the page
144    * footer if the stack of tests has unwound.
145    * @param string $test_name   Name of test that is ending.
146    * @param integer $progress   Number of test cases ending.
147    * @access public
148    */
149   function paintGroupEnd($test_name) {
1501    array_pop($this->_test_stack);
1511    $ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
1521    $class = $ok ? "simpletest-pass" : "simpletest-fail";
1531    $parent_weight = $this->getParentWeight() - 0.5;
154     /* Exception for the top groups, no subgrouping for singles */
1551    if (($this->_output_stack_id == 2) && ($this->_output_stack[$this->_output_stack_id]['size'] == 1)) {
156       $this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
157       $parent_weight = $this->getParentWeight() - 0.5;
158       $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
159       array_pop($this->form_depth);
160     }
161     else {
1621      $collapsed = $ok ? TRUE : FALSE;
1631      if ($this->getTestCaseProgress()) {
1641        $this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
1651        $use_grouping = FALSE;
166       }
167       else {
1681        $use_grouping = TRUE;
169       }
170       $write = array('#collapsible' => $use_grouping, '#collapsed' => $collapsed);
1711      $this->writeToLastField($this->form, $write, $this->form_depth);
1721    $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
1731    if (count($this->test_stack) != 0) {
1741        $this->writeContent(theme('table', array(), $this->test_stack));
1751        $this->test_stack = array();
176       }
1771    array_pop($this->form_depth);
178     }
179 
1801    $this->_progress   += array_pop($this->_progress_stack);
1811    $this->_exceptions += array_pop($this->_exceptions_stack);
1821    $this->_fails      += array_pop($this->_fails_stack);
1831    $this->_passes     += array_pop($this->_passes_stack);
184   }
185 
186   function paintCaseEnd($test_name) {
1871    $this->paintGroupEnd($test_name);
188   }
189 
190   /**
191    * Character set adjusted entity conversion.
192    * @param string $message    Plain text or Unicode message.
193    * @return string            Browser readable message.
194    * @access protected
195    */
196   function _htmlEntities($message) {
1971    return htmlentities($message, ENT_COMPAT, $this->_character_set);
198   }
199 
200   /**
201    * Could be extended to show more headers or whatever?
202    **/
203   function getOutput() {
204     return drupal_get_form('unit_tests', $this);
205   }
206 
207   /**
208    * Recursive function that writes attr to the deepest array
209    */
210   function writeToLastField(&$form, $attr, $keys) {
2111    while(count($keys) != 0) {
2121      $value = array_shift($keys);
2131      if (isset($form[$value])) {
2141        if (count($keys) == 0) {
2151          $form[$value] += $attr;
216         }
217         else {
2181          $this->writeToLastField($form[$value], $attr, $keys);
219         }
220         $keys = array();
221       }
222       else {
2231        $form[$value] = $attr;
224       }
225     }
226   }
227 
228   /**
229    * writes $msg into the deepest fieldset
230    * @param $msg content to write
231    */
232   function writeContent($msg, $weight = NULL, $class = 'simpletest') {
2331    if (!$weight) {
2341      $weight = $this->weight++;
235     }
2361    $write['content'.$this->content_count++] = array(
237       '#value' => '<div class=' . $class .'>' . $msg . '</div>',
238       '#weight' => $weight,
239     );
2401    $this->writeToLastField($this->form, $write, $this->form_depth);
241   }
242 
243   /**
244    * Retrieves weight of the currently deepest fieldset
245    */
246   function getParentWeight($form = NULL, $keys = NULL ) {
2471    if (!isset($form)) {
2481      $form = $this->form;
249     }
2501    if (!isset($keys)) {
2511      $keys = $this->form_depth;
252     }
2531    if(count($keys) != 0) {
2541      $value = array_shift($keys);
2551      return $this->getParentWeight($form[$value], $keys);
256     }
2571    return $form['#weight'];
258   }
259 }
260 
261 function unit_tests($args, $reporter) {
262   return $reporter->form['Drupal Unit Tests'];
263 }
264 ?