Line # | Frequency | Source 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') {
|
25 | 1 | $this->SimpleReporter();
|
26 | 1 | drupal_add_css(drupal_get_path('module', 'simpletest') .'/simpletest.css');
|
27 | 1 | $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) {
|
59 | 1 | parent::paintPass($message);
|
60 | 1 | $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 = '') {
|
109 | 1 | $this->_progress_stack[] = $this->_progress;
|
110 | 1 | $this->_progress = 0;
|
111 | 1 | $this->_exceptions_stack[] = $this->_exceptions;
|
112 | 1 | $this->_exceptions = 0;
|
113 | 1 | $this->_fails_stack[] = $this->_fails;
|
114 | 1 | $this->_fails = 0;
|
115 | 1 | $this->_passes_stack[] = $this->_passes;
|
116 | 1 | $this->_passes = 0;
|
117 | 1 | $this->form_depth[] = $test_name;
|
118 | 1 | $this->writeToLastField($this->form, array(
|
119 | | '#type' => 'fieldset',
|
120 | | '#title' => $test_name,
|
121 | | '#weight' => $this->weight++,
|
122 | | ), $this->form_depth);
|
123 | |
|
124 | 1 | if (! isset($this->_size)) {
|
125 | 1 | $this->_size = $size;
|
126 | | }
|
127 | |
|
128 | 1 | if (($c = count($this->test_info_stack)) > 0) {
|
129 | 1 | $info = $this->test_info_stack[$c - 1];
|
130 | 1 | $this->writeContent('<strong>' . $info['name'] . '</strong>: ' . $info['description'], $this->getParentWeight() );
|
131 | | }
|
132 | |
|
133 | 1 | $this->_test_stack[] = $test_name;
|
134 | | }
|
135 | |
|
136 | | function paintCaseStart($test_name) {
|
137 | 1 | $this->_progress++;
|
138 | 1 | $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) {
|
150 | 1 | array_pop($this->_test_stack);
|
151 | 1 | $ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
|
152 | 1 | $class = $ok ? "simpletest-pass" : "simpletest-fail";
|
153 | 1 | $parent_weight = $this->getParentWeight() - 0.5;
|
154 | | /* Exception for the top groups, no subgrouping for singles */
|
155 | 1 | 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 {
|
162 | 1 | $collapsed = $ok ? TRUE : FALSE;
|
163 | 1 | if ($this->getTestCaseProgress()) {
|
164 | 1 | $this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
|
165 | 1 | $use_grouping = FALSE;
|
166 | | }
|
167 | | else {
|
168 | 1 | $use_grouping = TRUE;
|
169 | | }
|
170 | | $write = array('#collapsible' => $use_grouping, '#collapsed' => $collapsed);
|
171 | 1 | $this->writeToLastField($this->form, $write, $this->form_depth);
|
172 | 1 | $this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
|
173 | 1 | if (count($this->test_stack) != 0) {
|
174 | 1 | $this->writeContent(theme('table', array(), $this->test_stack));
|
175 | 1 | $this->test_stack = array();
|
176 | | }
|
177 | 1 | array_pop($this->form_depth);
|
178 | | }
|
179 | |
|
180 | 1 | $this->_progress += array_pop($this->_progress_stack);
|
181 | 1 | $this->_exceptions += array_pop($this->_exceptions_stack);
|
182 | 1 | $this->_fails += array_pop($this->_fails_stack);
|
183 | 1 | $this->_passes += array_pop($this->_passes_stack);
|
184 | | }
|
185 | |
|
186 | | function paintCaseEnd($test_name) {
|
187 | 1 | $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) {
|
197 | 1 | 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) {
|
211 | 1 | while(count($keys) != 0) {
|
212 | 1 | $value = array_shift($keys);
|
213 | 1 | if (isset($form[$value])) {
|
214 | 1 | if (count($keys) == 0) {
|
215 | 1 | $form[$value] += $attr;
|
216 | | }
|
217 | | else {
|
218 | 1 | $this->writeToLastField($form[$value], $attr, $keys);
|
219 | | }
|
220 | | $keys = array();
|
221 | | }
|
222 | | else {
|
223 | 1 | $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') {
|
233 | 1 | if (!$weight) {
|
234 | 1 | $weight = $this->weight++;
|
235 | | }
|
236 | 1 | $write['content'.$this->content_count++] = array(
|
237 | | '#value' => '<div class=' . $class .'>' . $msg . '</div>',
|
238 | | '#weight' => $weight,
|
239 | | );
|
240 | 1 | $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 ) {
|
247 | 1 | if (!isset($form)) {
|
248 | 1 | $form = $this->form;
|
249 | | }
|
250 | 1 | if (!isset($keys)) {
|
251 | 1 | $keys = $this->form_depth;
|
252 | | }
|
253 | 1 | if(count($keys) != 0) {
|
254 | 1 | $value = array_shift($keys);
|
255 | 1 | return $this->getParentWeight($form[$value], $keys);
|
256 | | }
|
257 | 1 | return $form['#weight'];
|
258 | | }
|
259 | | }
|
260 | |
|
261 | | function unit_tests($args, $reporter) {
|
262 | | return $reporter->form['Drupal Unit Tests'];
|
263 | | }
|
264 | | ? |