1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
<?php
// $Id$
/**
* Minimal drupal displayer. Accumulates output to $_output.
* Based on HtmlReporter by Marcus Baker
*/
class DrupalReporter extends SimpleReporter {
var $_output_error = '';
var $_character_set;
var $_fails_stack = array(0);
var $_exceptions_stack = array(0);
var $_passes_stack = array(0);
var $_progress_stack = array(0);
var $test_info_stack = array();
var $_output_stack_id = -1;
var $form;
var $form_depth = array();
var $current_field_set = array();
var $content_count = 0;
var $weight = -10;
var $test_stack = array();
function DrupalReporter($character_set = 'ISO-8859-1') {
$this->SimpleReporter();
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
$this->_character_set = $character_set;
}
/**
* Paints the top of the web page setting the
* title to the name of the starting test.
* @param string $test_name Name class of test.
* @access public
**/
function paintHeader($test_name) {
}
/**
* Paints the end of the test with a summary of
* the passes and failures.
* @param string $test_name Name class of test.
* @access public
*/
function paintFooter($test_name) {
$ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
$class = $ok ? 'simpletest-pass' : 'simpletest-fail';
$this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '<strong> exceptions.');
}
/**
* Paints the test passes
* @param string $message Failure message displayed in
* the context of the other tests.
* @access public
**/
function paintPass($message, $group) {
parent::paintPass($message);
if ($group == 'Other') {
$group = t($group);
}
$this->test_stack[] = array(
'data' => array($message, "<strong>[$group]</strong>", t('Pass'), theme('image', 'misc/watchdog-ok.png')),
'class' => 'simpletest-pass',
);
}
/**
* Paints the test failure with a breadcrumbs
* trail of the nesting test suites below the
* top level test.
* @param string $message Failure message displayed in
* the context of the other tests.
* @access public
*/
function paintFail($message, $group) {
parent::paintFail($message);
if ($group == 'Other') {
$group = t($group);
}
$this->test_stack[] = array(
'data' => array($message, "<strong>[$group]</strong>", t('Fail'), theme('image', 'misc/watchdog-error.png')),
'class' => 'simpletest-fail',
);
}
/**
* Paints a PHP error or exception.
* @param string $message Message is ignored.
* @access public
**/
function paintError($message) {
parent::paintError($message);
$this->test_stack[] = array(
'data' => array($message, '<strong>[PHP]</strong>', t('Exception'), theme('image', 'misc/watchdog-warning.png')),
'class' => 'simpletest-exception',
);
}
/**
* Paints the start of a group test. Will also paint
* the page header and footer if this is the
* first test. Will stash the size if the first
* start.
* @param string $test_name Name of test that is starting.
* @param integer $size Number of test cases starting.
* @access public
*/
function paintGroupStart($test_name, $size, $extra = '') {
$this->_progress_stack[] = $this->_progress;
$this->_progress = 0;
$this->_exceptions_stack[] = $this->_exceptions;
$this->_exceptions = 0;
$this->_fails_stack[] = $this->_fails;
$this->_fails = 0;
$this->_passes_stack[] = $this->_passes;
$this->_passes = 0;
$this->form_depth[] = $test_name;
$this->writeToLastField($this->form, array(
'#type' => 'fieldset',
'#title' => $test_name,
'#weight' => $this->weight++,
), $this->form_depth);
if (! isset($this->_size)) {
$this->_size = $size;
}
if (($c = count($this->test_info_stack)) > 0) {
$info = $this->test_info_stack[$c - 1];
$this->writeContent('<strong>' . $info['name'] . '</strong>: ' . $info['description'], $this->getParentWeight() );
}
$this->_test_stack[] = $test_name;
}
function paintCaseStart($test_name) {
$this->_progress++;
$this->paintGroupStart($test_name, 1);
}
/**
* Paints the end of a group test. Will paint the page
* footer if the stack of tests has unwound.
* @param string $test_name Name of test that is ending.
* @param integer $progress Number of test cases ending.
* @access public
*/
function paintGroupEnd($test_name) {
array_pop($this->_test_stack);
$ok = ($this->getFailCount() + $this->getExceptionCount() == 0);
$class = $ok ? "simpletest-pass" : "simpletest-fail";
$parent_weight = $this->getParentWeight() - 0.5;
/* Exception for the top groups, no subgrouping for singles */
if (($this->_output_stack_id == 2) && ($this->_output_stack[$this->_output_stack_id]['size'] == 1)) {
$this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
$parent_weight = $this->getParentWeight() - 0.5;
$this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
array_pop($this->form_depth);
}
else {
$collapsed = $ok ? TRUE : FALSE;
if ($this->getTestCaseProgress()) {
$this->writeContent(format_plural($this->getTestCaseProgress(), '1 test case complete: ', '@count test cases complete: '), -10);
$use_grouping = FALSE;
}
else {
$use_grouping = TRUE;
}
$write = array('#collapsible' => $use_grouping, '#collapsed' => $collapsed);
$this->writeToLastField($this->form, $write, $this->form_depth);
$this->writeContent('<strong>' . $this->getPassCount() . '</strong> passes, <strong>' . $this->getFailCount() . '</strong> fails and <strong>' . $this->getExceptionCount() . '</strong> exceptions.', $parent_weight, $class);
if (count($this->test_stack) != 0) {
$this->writeContent(theme('table', array(), $this->test_stack));
$this->test_stack = array();
}
array_pop($this->form_depth);
}
$this->_progress += array_pop($this->_progress_stack);
$this->_exceptions += array_pop($this->_exceptions_stack);
$this->_fails += array_pop($this->_fails_stack);
$this->_passes += array_pop($this->_passes_stack);
}
function paintCaseEnd($test_name) {
$this->paintGroupEnd($test_name);
}
/**
* Could be extended to show more headers or whatever?
**/
function getOutput() {
return drupal_get_form('unit_tests', $this);
}
/**
* Recursive function that writes attr to the deepest array
*/
function writeToLastField(&$form, $attr, $keys) {
while(count($keys) != 0) {
$value = array_shift($keys);
if (isset($form[$value])) {
if (count($keys) == 0) {
$form[$value] += $attr;
}
else {
$this->writeToLastField($form[$value], $attr, $keys);
}
$keys = array();
}
else {
$form[$value] = $attr;
}
}
}
/**
* writes $msg into the deepest fieldset
* @param $msg content to write
*/
function writeContent($msg, $weight = NULL, $class = 'simpletest') {
if (!$weight) {
$weight = $this->weight++;
}
$write['content' . $this->content_count++] = array(
'#value' => '<div class=' . $class .'>' . $msg . '</div>',
'#weight' => $weight,
);
$this->writeToLastField($this->form, $write, $this->form_depth);
}
/**
* Retrieves weight of the currently deepest fieldset
*/
function getParentWeight($form = NULL, $keys = NULL ) {
if (!isset($form)) {
$form = $this->form;
}
if (!isset($keys)) {
$keys = $this->form_depth;
}
if(count($keys) != 0) {
$value = array_shift($keys);
return $this->getParentWeight($form[$value], $keys);
}
return $form['#weight'];
}
}
function unit_tests($args, $reporter) {
return $reporter->form['Drupal Unit Tests'];
}
?>
|