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