more test runner refactoring

This commit is contained in:
tmont 2009-06-20 03:54:48 +00:00
parent c4bcc9082b
commit b4837595ee
7 changed files with 86 additions and 35 deletions

View File

@ -1,15 +1,29 @@
<?php
abstract class BaseTestRunner {
abstract class TestRunner implements Countable {
protected $tests;
protected $listeners;
protected $options;
private $startTime;
private $endTime;
public function __construct(array $tests = array(), array $listeners = array(), array $options = array()) {
$this->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();

View File

@ -1,24 +0,0 @@
<?php
class ConsoleTestRunner extends BaseTestRunner {
public function run() {
self::printMeta();
$this->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");
}
}
?>

View File

@ -0,0 +1,14 @@
<?php
class ConsoleTestRunner extends TestRunner {
protected function getAllowableOptions() {
return array(
'recursive' => 'boolean',
'bootstrap' => 'string'
);
}
}
?>

View File

@ -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:

View File

@ -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) {
}
}

View File

@ -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',

View File

@ -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 . ')';
}
}
?>