From b4837595ee3f00a843efc070debf332f1885883b Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 20 Jun 2009 03:54:48 +0000 Subject: [PATCH] more test runner refactoring --- src/TUnit/framework/BaseTestRunner.php | 34 +++++++++++++++++-- src/TUnit/framework/TestRunner.php | 24 ------------- src/TUnit/framework/TestRunners.php | 14 ++++++++ .../framework/listeners/ConsoleListener.php | 19 ++++++++--- .../framework/listeners/TestListener.php | 12 +++++-- src/TUnit/manifest.php | 6 ++-- tools/productize.php | 12 +++++++ 7 files changed, 86 insertions(+), 35 deletions(-) delete mode 100644 src/TUnit/framework/TestRunner.php create mode 100644 src/TUnit/framework/TestRunners.php diff --git a/src/TUnit/framework/BaseTestRunner.php b/src/TUnit/framework/BaseTestRunner.php index 80bd60d..0fa4600 100644 --- a/src/TUnit/framework/BaseTestRunner.php +++ b/src/TUnit/framework/BaseTestRunner.php @@ -1,15 +1,29 @@ tests = $tests; $this->listeners = $listeners; $this->options = (!empty($options)) ? $this->parseOptions($options) : array(); + + $this->startTime = -1; + $this->endTime = -1; + } + + public final function getStartTime() { + return $this->startTime; + } + + public final function getEndTime() { + return $this->endTime; } public final function warn($message) { @@ -79,7 +93,23 @@ } } - public abstract function run(); + public final function run() { + foreach ($this->listeners as $listener) { + $listener->beforeTestRunner($this); + } + + $this->startTime = microtime(true); + $this->publishResults($this->runTests()); + $this->endTime = microtime(true); + + foreach ($this->listeners as $listener) { + $listener->afterTestRunner($this); + } + } + + public function count() { + return count($this->tests); + } protected abstract function getAllowableOptions(); diff --git a/src/TUnit/framework/TestRunner.php b/src/TUnit/framework/TestRunner.php deleted file mode 100644 index 5f62e72..0000000 --- a/src/TUnit/framework/TestRunner.php +++ /dev/null @@ -1,24 +0,0 @@ -publishResults($this->runTests()); - } - - protected function getAllowableOptions() { - return array( - 'recursive' => 'boolean', - 'bootstrap' => 'string' - ); - } - - public static function printMeta() { - fwrite(STDOUT, Product::NAME . ' ' . Product::VERSION . ' (build date: ' . Product::DATE . ')' . "\n"); - fwrite(STDOUT, ' by ' . Product::AUTHOR . "\n\n"); - } - - } - -?> \ No newline at end of file diff --git a/src/TUnit/framework/TestRunners.php b/src/TUnit/framework/TestRunners.php new file mode 100644 index 0000000..2a972ec --- /dev/null +++ b/src/TUnit/framework/TestRunners.php @@ -0,0 +1,14 @@ + 'boolean', + 'bootstrap' => 'string' + ); + } + + } + +?> \ No newline at end of file diff --git a/src/TUnit/framework/listeners/ConsoleListener.php b/src/TUnit/framework/listeners/ConsoleListener.php index 2d4d368..60175a6 100644 --- a/src/TUnit/framework/listeners/ConsoleListener.php +++ b/src/TUnit/framework/listeners/ConsoleListener.php @@ -2,8 +2,8 @@ class ConsoleListener extends TestListener { - private $verbosity; - private $currentLineLength; + protected $verbosity; + protected $currentLineLength; const LINE_LENGTH = 64; @@ -16,11 +16,11 @@ $this->currentLineLength = 0; } - private function out($text) { + protected function out($text) { fwrite(STDOUT, $text); } - private function err($text) { + protected function err($text) { fwrite(STDERR, $text); } @@ -34,6 +34,17 @@ $this->currentLineLength = strlen($text); } + public function beforeTestRunner(TestRunner $runner) { + $this->out(Product::getVersionString() . "\n"); + $this->out(' by ' . Product::AUTHOR . "\n\n"); + } + + public function afterTestRunner(TestRunner $runner) { + $elapsedTime = $runner->getEndTime() - $runner->getStartTime(); + $numTests = (count($runner) === 1) ? '1 test' : count($runner) . ' tests'; + $this->out('Ran ' . $numTests . ' in ' . round($elapsedTime, 3) . ' seconds' . "\n"); + } + public function beforeTestSuite(TestSuite $suite) { switch ($this->verbosity) { case self::VERBOSITY_HIGH: diff --git a/src/TUnit/framework/listeners/TestListener.php b/src/TUnit/framework/listeners/TestListener.php index 37df899..5b1f044 100644 --- a/src/TUnit/framework/listeners/TestListener.php +++ b/src/TUnit/framework/listeners/TestListener.php @@ -6,10 +6,18 @@ } - public function beforeTestSuite(TestSuite $suite) { + public function beforeTestRunner(TestRunner $runner) { + + } + + public function afterTestRunner(TestRunner $runner) { } + public function beforeTestSuite(TestSuite $suite) { + + } + public function afterTestSuite(TestSuite $suite) { } @@ -67,7 +75,7 @@ } public function publishTestResult(TestResult $result) { - + } } diff --git a/src/TUnit/manifest.php b/src/TUnit/manifest.php index b381817..b7a3db2 100644 --- a/src/TUnit/manifest.php +++ b/src/TUnit/manifest.php @@ -3,7 +3,7 @@ /** * Autoload manifest * - * Autogenerated by manifester.php on 2009-06-19 20:34:30 + * Autogenerated by manifester.php on 2009-06-19 20:50:38 * * @package TUnit * @version 0.4.0 @@ -13,13 +13,12 @@ return array( 'Assert' => 'TUnit/framework/Assert.php', 'Autoloader' => 'TUnit/util/Autoloader.php', - 'BaseTestRunner' => 'TUnit/framework/BaseTestRunner.php', 'Cli' => 'TUnit/util/cli.php', 'CliSwitch' => 'TUnit/util/cli.php', 'CliSwitchCollection' => 'TUnit/util/cli.php', 'CombinedTestResult' => 'TUnit/framework/result/CombinedTestResult.php', 'ConsoleListener' => 'TUnit/framework/listeners/ConsoleListener.php', - 'ConsoleTestRunner' => 'TUnit/framework/TestRunner.php', + 'ConsoleTestRunner' => 'TUnit/framework/TestRunners.php', 'Constraint' => 'TUnit/framework/constraints/Constraint.php', 'DefaultConstraint' => 'TUnit/framework/constraints/DefaultConstraint.php', 'EmptyConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', @@ -57,6 +56,7 @@ 'TestListener' => 'TUnit/framework/listeners/TestListener.php', 'TestMethod' => 'TUnit/framework/test/TestMethod.php', 'TestResult' => 'TUnit/framework/result/TestResult.php', + 'TestRunner' => 'TUnit/framework/BaseTestRunner.php', 'TestSuite' => 'TUnit/framework/test/TestSuite.php', 'Testable' => 'TUnit/framework/test/Testable.php', 'TrueConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', diff --git a/tools/productize.php b/tools/productize.php index fbfd014..c91b293 100644 --- a/tools/productize.php +++ b/tools/productize.php @@ -95,8 +95,20 @@ const DATE = '$datetime'; /**#@-*/ + /** + * @ignore + */ private function __construct() {} + /** + * Gets a human-readable version string + * + * @return string + */ + public static function getVersionString() { + return self::NAME . ' ' . self::VERSION . ' (' . self::DATE . ')'; + } + } ?>