1<?php
2    /**
3     *	base include file for SimpleTest
4     *	@package	SimpleTest
5     *	@subpackage	UnitTester
6     *	@version	$Id: scorer.php,v 1.1 2005/11/09 23:41:18 gsmet Exp $
7     */
8
9    /**
10     *    Can recieve test events and display them. Display
11     *    is achieved by making display methods available
12     *    and visiting the incoming event.
13	 *	  @package SimpleTest
14	 *	  @subpackage UnitTester
15     *    @abstract
16     */
17    class SimpleScorer {
18        var $_passes;
19        var $_fails;
20        var $_exceptions;
21        var $_is_dry_run;
22
23        /**
24         *    Starts the test run with no results.
25         *    @access public
26         */
27        function SimpleScorer() {
28            $this->_passes = 0;
29            $this->_fails = 0;
30            $this->_exceptions = 0;
31            $this->_is_dry_run = false;
32        }
33
34        /**
35         *    Signals that the next evaluation will be a dry
36         *    run. That is, the structure events will be
37         *    recorded, but no tests will be run.
38         */
39        function makeDry($is_dry = true) {
40            $this->_is_dry_run = $is_dry;
41        }
42
43        /**
44         *    The reporter has a veto on what should be run.
45         *    @param string $test_case_name  name of test case.
46         *    @param string $method          Name of test method.
47         *    @access public
48         */
49        function shouldInvoke($test_case_name, $method) {
50            return ! $this->_is_dry_run;
51        }
52
53        /**
54         *    Accessor for current status. Will be false
55         *    if there have been any failures or exceptions.
56         *    Used for command line tools.
57         *    @return boolean        True if no failures.
58         *    @access public
59         */
60        function getStatus() {
61            if ($this->_exceptions + $this->_fails > 0) {
62                return false;
63            }
64            return true;
65        }
66
67        /**
68         *    Paints the start of a test method.
69         *    @param string $test_name     Name of test or other label.
70         *    @access public
71         */
72        function paintMethodStart($test_name) {
73        }
74
75        /**
76         *    Paints the end of a test method.
77         *    @param string $test_name     Name of test or other label.
78         *    @access public
79         */
80        function paintMethodEnd($test_name) {
81        }
82
83        /**
84         *    Paints the start of a test case.
85         *    @param string $test_name     Name of test or other label.
86         *    @access public
87         */
88        function paintCaseStart($test_name) {
89        }
90
91        /**
92         *    Paints the end of a test case.
93         *    @param string $test_name     Name of test or other label.
94         *    @access public
95         */
96        function paintCaseEnd($test_name) {
97        }
98
99        /**
100         *    Paints the start of a group test.
101         *    @param string $test_name     Name of test or other label.
102         *    @param integer $size         Number of test cases starting.
103         *    @access public
104         */
105        function paintGroupStart($test_name, $size) {
106        }
107
108        /**
109         *    Paints the end of a group test.
110         *    @param string $test_name     Name of test or other label.
111         *    @access public
112         */
113        function paintGroupEnd($test_name) {
114        }
115
116        /**
117         *    Increments the pass count.
118         *    @param string $message        Message is ignored.
119         *    @access public
120         */
121        function paintPass($message) {
122            $this->_passes++;
123        }
124
125        /**
126         *    Increments the fail count.
127         *    @param string $message        Message is ignored.
128         *    @access public
129         */
130        function paintFail($message) {
131            $this->_fails++;
132        }
133
134        /**
135         *    Deals with PHP 4 throwing an error.
136         *    @param string $message    Text of error formatted by
137         *                              the test case.
138         *    @access public
139         */
140        function paintError($message) {
141            $this->paintException($message);
142        }
143
144        /**
145         *    Deals with PHP 5 throwing an exception
146         *    This isn't really implemented yet.
147         *    @param Exception $exception     Object thrown.
148         *    @access public
149         */
150        function paintException($exception) {
151            $this->_exceptions++;
152        }
153
154        /**
155         *    Accessor for the number of passes so far.
156         *    @return integer       Number of passes.
157         *    @access public
158         */
159        function getPassCount() {
160            return $this->_passes;
161        }
162
163        /**
164         *    Accessor for the number of fails so far.
165         *    @return integer       Number of fails.
166         *    @access public
167         */
168        function getFailCount() {
169            return $this->_fails;
170        }
171
172        /**
173         *    Accessor for the number of untrapped errors
174         *    so far.
175         *    @return integer       Number of exceptions.
176         *    @access public
177         */
178        function getExceptionCount() {
179            return $this->_exceptions;
180        }
181
182        /**
183         *    Paints a simple supplementary message.
184         *    @param string $message        Text to display.
185         *    @access public
186         */
187        function paintMessage($message) {
188        }
189
190        /**
191         *    Paints a formatted ASCII message such as a
192         *    variable dump.
193         *    @param string $message        Text to display.
194         *    @access public
195         */
196        function paintFormattedMessage($message) {
197        }
198
199        /**
200         *    By default just ignores user generated events.
201         *    @param string $type        Event type as text.
202         *    @param mixed $payload      Message or object.
203         *    @access public
204         */
205        function paintSignal($type, &$payload) {
206        }
207    }
208
209    /**
210     *    Recipient of generated test messages that can display
211     *    page footers and headers. Also keeps track of the
212     *    test nesting. This is the main base class on which
213     *    to build the finished test (page based) displays.
214	 *	  @package SimpleTest
215	 *	  @subpackage UnitTester
216     */
217    class SimpleReporter extends SimpleScorer {
218        var $_test_stack;
219        var $_size;
220        var $_progress;
221
222        /**
223         *    Starts the display with no results in.
224         *    @access public
225         */
226        function SimpleReporter() {
227            $this->SimpleScorer();
228            $this->_test_stack = array();
229            $this->_size = null;
230            $this->_progress = 0;
231        }
232
233        /**
234         *    Paints the start of a group test. Will also paint
235         *    the page header and footer if this is the
236         *    first test. Will stash the size if the first
237         *    start.
238         *    @param string $test_name   Name of test that is starting.
239         *    @param integer $size       Number of test cases starting.
240         *    @access public
241         */
242        function paintGroupStart($test_name, $size) {
243            if (! isset($this->_size)) {
244                $this->_size = $size;
245            }
246            if (count($this->_test_stack) == 0) {
247                $this->paintHeader($test_name);
248            }
249            $this->_test_stack[] = $test_name;
250        }
251
252        /**
253         *    Paints the end of a group test. Will paint the page
254         *    footer if the stack of tests has unwound.
255         *    @param string $test_name   Name of test that is ending.
256         *    @param integer $progress   Number of test cases ending.
257         *    @access public
258         */
259        function paintGroupEnd($test_name) {
260            array_pop($this->_test_stack);
261            if (count($this->_test_stack) == 0) {
262                $this->paintFooter($test_name);
263            }
264        }
265
266        /**
267         *    Paints the start of a test case. Will also paint
268         *    the page header and footer if this is the
269         *    first test. Will stash the size if the first
270         *    start.
271         *    @param string $test_name   Name of test that is starting.
272         *    @access public
273         */
274        function paintCaseStart($test_name) {
275            if (! isset($this->_size)) {
276                $this->_size = 1;
277            }
278            if (count($this->_test_stack) == 0) {
279                $this->paintHeader($test_name);
280            }
281            $this->_test_stack[] = $test_name;
282        }
283
284        /**
285         *    Paints the end of a test case. Will paint the page
286         *    footer if the stack of tests has unwound.
287         *    @param string $test_name   Name of test that is ending.
288         *    @access public
289         */
290        function paintCaseEnd($test_name) {
291            $this->_progress++;
292            array_pop($this->_test_stack);
293            if (count($this->_test_stack) == 0) {
294                $this->paintFooter($test_name);
295            }
296        }
297
298        /**
299         *    Paints the start of a test method.
300         *    @param string $test_name   Name of test that is starting.
301         *    @access public
302         */
303        function paintMethodStart($test_name) {
304            $this->_test_stack[] = $test_name;
305        }
306
307        /**
308         *    Paints the end of a test method. Will paint the page
309         *    footer if the stack of tests has unwound.
310         *    @param string $test_name   Name of test that is ending.
311         *    @access public
312         */
313        function paintMethodEnd($test_name) {
314            array_pop($this->_test_stack);
315        }
316
317        /**
318         *    Paints the test document header.
319         *    @param string $test_name     First test top level
320         *                                 to start.
321         *    @access public
322         *    @abstract
323         */
324        function paintHeader($test_name) {
325        }
326
327        /**
328         *    Paints the test document footer.
329         *    @param string $test_name        The top level test.
330         *    @access public
331         *    @abstract
332         */
333        function paintFooter($test_name) {
334        }
335
336        /**
337         *    Accessor for internal test stack. For
338         *    subclasses that need to see the whole test
339         *    history for display purposes.
340         *    @return array     List of methods in nesting order.
341         *    @access public
342         */
343        function getTestList() {
344            return $this->_test_stack;
345        }
346
347        /**
348         *    Accessor for total test size in number
349         *    of test cases. Null until the first
350         *    test is started.
351         *    @return integer   Total number of cases at start.
352         *    @access public
353         */
354        function getTestCaseCount() {
355            return $this->_size;
356        }
357
358        /**
359         *    Accessor for the number of test cases
360         *    completed so far.
361         *    @return integer   Number of ended cases.
362         *    @access public
363         */
364        function getTestCaseProgress() {
365            return $this->_progress;
366        }
367
368        /**
369         *    Static check for running in the comand line.
370         *    @return boolean        True if CLI.
371         *    @access public
372         *    @static
373         */
374        function inCli() {
375            return php_sapi_name() == 'cli';
376        }
377    }
378?>