cvs: pear(ZEND_ENGINE_2) /PHPUnit/TestRunner CLI.php
| From: | Sebastian Bergmann | Date: | Fri, 06 Jun 2003 13:40:45 +0000 |
| Subject: | cvs: pear(ZEND_ENGINE_2) /PHPUnit/TestRunner CLI.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-10726@lists.php.net to get a copy of this message | ||
sebastian Fri Jun 6 09:40:45 2003 EDT
Modified files: (Branch: ZEND_ENGINE_2)
/pear/PHPUnit/TestRunner CLI.php
Log:
More work on the CLI TestRunner.
Index: pear/PHPUnit/TestRunner/CLI.php diff -u pear/PHPUnit/TestRunner/CLI.php:1.1.2.20 pear/PHPUnit/TestRunner/CLI.php:1.1.2.21 --- pear/PHPUnit/TestRunner/CLI.php:1.1.2.20 Fri Jun 6 06:02:45 2003 +++ pear/PHPUnit/TestRunner/CLI.php Fri Jun 6 09:40:45 2003 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: CLI.php,v 1.1.2.20 2003/06/06 10:02:45 sebastian Exp $ +// $Id: CLI.php,v 1.1.2.21 2003/06/06 13:40:45 sebastian Exp $ // require_once 'Benchmark/Timer.php'; @@ -31,11 +31,40 @@ * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestRunner_CLI extends PHPUnit_TestRunner { - // {{{ public static function main() + // {{{ Members + + /** + * @var integer + * @access private + */ + private $fColumn = 0; + + // }}} + // {{{ public function doRun(PHPUnit_Test $suite, $wait = false) /** - * @param array $args + * Runs a test suite. + * + * @param PHPUnit_Test $suite + * @param optional boolean $wait + * @access public */ + public function doRun(PHPUnit_Test $suite, $wait = false) { + $result = new PHPUnit_TestResult; + $result->addListener(new PHPUnit_TestRunner_CLI); + + $suite->run($result); + + if ($wait) { + // ... + } + + return $result; + } + + // }}} + // {{{ public static function main() + public static function main() { printf( "PHPUnit %s by Sebastian Bergmann.\n\n", @@ -61,14 +90,16 @@ $testRunner = new PHPUnit_TestRunner_CLI; try { + $timer = new Benchmark_Timer; + $timer->start(); + $result = $testRunner->doRun( $testRunner->getTest($test), $wait ); - $testRunner->printTests($result->passedTests()); - $testRunner->printTests($result->failures()); - $testRunner->printTests($result->errors()); + $timer->stop(); + $testRunner->printResult($result, $timer->timeElapsed()); } catch (Exception $e) { @@ -80,31 +111,151 @@ } // }}} - // {{{ public function doRun(PHPUnit_Test $suite, $wait = false) + // {{{ public function printResult(PHPUnit_TestResult $result, $runTime) /** - * Runs a test suite. - * - * @param PHPUnit_Test $suite - * @param optional boolean $wait + * @param PHPUnit_TestResult $result + * @param float $runTime + * @access public */ - public function doRun(PHPUnit_Test $suite, $wait = false) { - $result = new PHPUnit_TestResult; - $result->addListener(new PHPUnit_TestRunner_CLI); + public function printResult(PHPUnit_TestResult $result, $runTime) { + $this->printHeader($runTime); + $this->printErrors($result); + $this->printFailures($result); + $this->printFooter($result); + } - $timer = new Benchmark_Timer; - $timer->start(); + // }}} + // {{{ protected function printDefects($defects, $count, $type) - $suite->run($result); + /** + * @param array $defects + * @param integer $count + * @param string $type + * @access protected + */ + protected function printDefects($defects, $count, $type) { + if ($count == 0) { + return; + } - $timer->stop(); - $timeElapsed = $timer->timeElapsed(); + printf( + "There %s %d %s%s:\n", - if ($wait) { - // ... + ($count == 1) ? 'was' : 'were', + $count, + $type, + ($count == 1) ? '' : 's' + ); + + $i = 1; + + foreach ($defects as $defect) { + $this->printDefect($defect, $i++); } + } - return $result; + // }}} + // {{{ protected function printDefect(PHPUnit_TestFailure $defect, $count) + + /** + * @param PHPUnit_TestFailure $defect + * @param integer $count + * @access protected + */ + protected function printDefect(PHPUnit_TestFailure $defect, $count) { + $this->printDefectHeader($defect, $count); + $this->printDefectTrace($defect); + } + + // }}} + // {{{ protected function printDefectHeader(PHPUnit_TestFailure $defect, $count) + + /** + * @param PHPUnit_TestFailure $defect + * @param integer $count + * @access protected + */ + protected function printDefectHeader(PHPUnit_TestFailure $defect, $count) { + printf( + "%d) %s\n", + + $count, + $defect->failedTest()->getName() + ); + } + + // }}} + // {{{ protected function printDefectTrace(PHPUnit_TestFailure $defect) + + /** + * @param PHPUnit_TestFailure $defect + * @access protected + */ + protected function printDefectTrace(PHPUnit_TestFailure $defect) { + } + + // }}} + // {{{ protected function printErrors(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printErrors(PHPUnit_TestResult $result) { + $this->printDefects($result->errors(), $result->errorCount(), 'error'); + } + + // }}} + // {{{ protected function printFailures(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printFailures(PHPUnit_TestResult $result) { + $this->printDefects($result->failures(), $result->failureCount(), 'failure'); + } + + // }}} + // {{{ protected function printHeader($runTime) + + /** + * @param float $runTime + * @access protected + */ + protected function printHeader($runTime) { + printf( + "\n\nTime: %s\n", + + $runTime + ); + } + + // }}} + // {{{ protected function printFooter(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printFooter(PHPUnit_TestResult $result) { + if ($result->wasSuccessful()) { + printf( + "\nOK (%d test%s)\n", + + $result->runCount(), + ($result->runCount() == 1) ? '' : 's' + ); + } else { + printf( + "\nFAILURES!!!\nTests run: %d, Failures: %d, Errors: %d.\n", + + $result->runCount(), + $result->failureCount(), + $result->errorCount() + ); + } } // }}} @@ -114,6 +265,7 @@ * A test ended. * * @param string $testName + * @access public */ public function testEnded($testName) {} @@ -123,11 +275,24 @@ /** * A test failed. * - * @param integer $status + * @param integer $status * @param PHPUnit_Test $test * @param PHPUnit_AssertionFailedError $e + * @access public */ - public function testFailed($status, PHPUnit_Test $test, PHPUnit_AssertionFailedError $e) {} + public function testFailed($status, PHPUnit_Test $test, PHPUnit_AssertionFailedError $e) { + switch ($status) { + case PHPUnit_TestRunListener::STATUS_ERROR: { + print 'E'; + } + break; + + case PHPUnit_TestRunListener::STATUS_FAILURE: { + print 'F'; + } + break; + } + } // }}} // {{{ public function testStarted($testName) @@ -136,40 +301,14 @@ * A test started. * * @param string $testName - */ - public function testStarted($testName) {} - - // }}} - // {{{ public function printTests($tests) - - /** - * Prints an array of Tests. - * - * @param array $tests * @access public */ - public function printTests($tests) { - foreach ($tests as $test) { - if ($test instanceof PHPUnit_TestFailure) { - $exception = $test->thrownException(); - $name = $test->failedTest()->getName(); - - if ($exception instanceof PHPUnit_AssertionFailedError) { - $prefix = 'FAIL'; - } else { - $prefix = 'ERROR'; - } - } else { - $name = $test->getName(); - $prefix = 'PASS'; - } - - printf( - "%s: %s\n", - - $prefix, - $name - ); + public function testStarted($testName) { + print '.'; + + if ($this->fColumn++ >= 40) { + $this->fColumn = 0; + print "\n"; } } @@ -183,7 +322,7 @@ * @access protected */ protected function runFailed($message) { - echo $message; + print $message; } // }}}
Index: pear/PHPUnit/TestRunner/CLI.php diff -u pear/PHPUnit/TestRunner/CLI.php:1.1.2.20 pear/PHPUnit/TestRunner/CLI.php:1.1.2.21 --- pear/PHPUnit/TestRunner/CLI.php:1.1.2.20 Fri Jun 6 06:02:45 2003 +++ pear/PHPUnit/TestRunner/CLI.php Fri Jun 6 09:40:45 2003 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: CLI.php,v 1.1.2.20 2003/06/06 10:02:45 sebastian Exp $ +// $Id: CLI.php,v 1.1.2.21 2003/06/06 13:40:45 sebastian Exp $ // require_once 'Benchmark/Timer.php'; @@ -31,11 +31,40 @@ * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestRunner_CLI extends PHPUnit_TestRunner { - // {{{ public static function main() + // {{{ Members + + /** + * @var integer + * @access private + */ + private $fColumn = 0; + + // }}} + // {{{ public function doRun(PHPUnit_Test $suite, $wait = false) /** - * @param array $args + * Runs a test suite. + * + * @param PHPUnit_Test $suite + * @param optional boolean $wait + * @access public */ + public function doRun(PHPUnit_Test $suite, $wait = false) { + $result = new PHPUnit_TestResult; + $result->addListener(new PHPUnit_TestRunner_CLI); + + $suite->run($result); + + if ($wait) { + // ... + } + + return $result; + } + + // }}} + // {{{ public static function main() + public static function main() { printf( "PHPUnit %s by Sebastian Bergmann.\n\n", @@ -61,14 +90,16 @@ $testRunner = new PHPUnit_TestRunner_CLI; try { + $timer = new Benchmark_Timer; + $timer->start(); + $result = $testRunner->doRun( $testRunner->getTest($test), $wait ); - $testRunner->printTests($result->passedTests()); - $testRunner->printTests($result->failures()); - $testRunner->printTests($result->errors()); + $timer->stop(); + $testRunner->printResult($result, $timer->timeElapsed()); } catch (Exception $e) { @@ -80,31 +111,151 @@ } // }}} - // {{{ public function doRun(PHPUnit_Test $suite, $wait = false) + // {{{ public function printResult(PHPUnit_TestResult $result, $runTime) /** - * Runs a test suite. - * - * @param PHPUnit_Test $suite - * @param optional boolean $wait + * @param PHPUnit_TestResult $result + * @param float $runTime + * @access public */ - public function doRun(PHPUnit_Test $suite, $wait = false) { - $result = new PHPUnit_TestResult; - $result->addListener(new PHPUnit_TestRunner_CLI); + public function printResult(PHPUnit_TestResult $result, $runTime) { + $this->printHeader($runTime); + $this->printErrors($result); + $this->printFailures($result); + $this->printFooter($result); + } - $timer = new Benchmark_Timer; - $timer->start(); + // }}} + // {{{ protected function printDefects($defects, $count, $type) - $suite->run($result); + /** + * @param array $defects + * @param integer $count + * @param string $type + * @access protected + */ + protected function printDefects($defects, $count, $type) { + if ($count == 0) { + return; + } - $timer->stop(); - $timeElapsed = $timer->timeElapsed(); + printf( + "There %s %d %s%s:\n", - if ($wait) { - // ... + ($count == 1) ? 'was' : 'were', + $count, + $type, + ($count == 1) ? '' : 's' + ); + + $i = 1; + + foreach ($defects as $defect) { + $this->printDefect($defect, $i++); } + } - return $result; + // }}} + // {{{ protected function printDefect(PHPUnit_TestFailure $defect, $count) + + /** + * @param PHPUnit_TestFailure $defect + * @param integer $count + * @access protected + */ + protected function printDefect(PHPUnit_TestFailure $defect, $count) { + $this->printDefectHeader($defect, $count); + $this->printDefectTrace($defect); + } + + // }}} + // {{{ protected function printDefectHeader(PHPUnit_TestFailure $defect, $count) + + /** + * @param PHPUnit_TestFailure $defect + * @param integer $count + * @access protected + */ + protected function printDefectHeader(PHPUnit_TestFailure $defect, $count) { + printf( + "%d) %s\n", + + $count, + $defect->failedTest()->getName() + ); + } + + // }}} + // {{{ protected function printDefectTrace(PHPUnit_TestFailure $defect) + + /** + * @param PHPUnit_TestFailure $defect + * @access protected + */ + protected function printDefectTrace(PHPUnit_TestFailure $defect) { + } + + // }}} + // {{{ protected function printErrors(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printErrors(PHPUnit_TestResult $result) { + $this->printDefects($result->errors(), $result->errorCount(), 'error'); + } + + // }}} + // {{{ protected function printFailures(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printFailures(PHPUnit_TestResult $result) { + $this->printDefects($result->failures(), $result->failureCount(), 'failure'); + } + + // }}} + // {{{ protected function printHeader($runTime) + + /** + * @param float $runTime + * @access protected + */ + protected function printHeader($runTime) { + printf( + "\n\nTime: %s\n", + + $runTime + ); + } + + // }}} + // {{{ protected function printFooter(PHPUnit_TestResult $result) + + /** + * @param PHPUnit_TestResult $result + * @access protected + */ + protected function printFooter(PHPUnit_TestResult $result) { + if ($result->wasSuccessful()) { + printf( + "\nOK (%d test%s)\n", + + $result->runCount(), + ($result->runCount() == 1) ? '' : 's' + ); + } else { + printf( + "\nFAILURES!!!\nTests run: %d, Failures: %d, Errors: %d.\n", + + $result->runCount(), + $result->failureCount(), + $result->errorCount() + ); + } } // }}} @@ -114,6 +265,7 @@ * A test ended. * * @param string $testName + * @access public */ public function testEnded($testName) {} @@ -123,11 +275,24 @@ /** * A test failed. * - * @param integer $status + * @param integer $status * @param PHPUnit_Test $test * @param PHPUnit_AssertionFailedError $e + * @access public */ - public function testFailed($status, PHPUnit_Test $test, PHPUnit_AssertionFailedError $e) {} + public function testFailed($status, PHPUnit_Test $test, PHPUnit_AssertionFailedError $e) { + switch ($status) { + case PHPUnit_TestRunListener::STATUS_ERROR: { + print 'E'; + } + break; + + case PHPUnit_TestRunListener::STATUS_FAILURE: { + print 'F'; + } + break; + } + } // }}} // {{{ public function testStarted($testName) @@ -136,40 +301,14 @@ * A test started. * * @param string $testName - */ - public function testStarted($testName) {} - - // }}} - // {{{ public function printTests($tests) - - /** - * Prints an array of Tests. - * - * @param array $tests * @access public */ - public function printTests($tests) { - foreach ($tests as $test) { - if ($test instanceof PHPUnit_TestFailure) { - $exception = $test->thrownException(); - $name = $test->failedTest()->getName(); - - if ($exception instanceof PHPUnit_AssertionFailedError) { - $prefix = 'FAIL'; - } else { - $prefix = 'ERROR'; - } - } else { - $name = $test->getName(); - $prefix = 'PASS'; - } - - printf( - "%s: %s\n", - - $prefix, - $name - ); + public function testStarted($testName) { + print '.'; + + if ($this->fColumn++ >= 40) { + $this->fColumn = 0; + print "\n"; } } @@ -183,7 +322,7 @@ * @access protected */ protected function runFailed($message) { - echo $message; + print $message; } // }}}