From c4bcc9082bc4c6c2417a6a7eb506aee70b5cbb4a Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 20 Jun 2009 03:35:46 +0000 Subject: [PATCH] refactored test runner a bit --- src/TUnit/framework/BaseTestRunner.php | 88 +++++++++++++++++++ src/TUnit/framework/TestRunner.php | 48 ++-------- src/TUnit/framework/exceptions/Exceptions.php | 18 ++++ src/TUnit/manifest.php | 7 +- src/TUnit/util/entry_console.php | 15 ++-- 5 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 src/TUnit/framework/BaseTestRunner.php create mode 100644 src/TUnit/framework/exceptions/Exceptions.php diff --git a/src/TUnit/framework/BaseTestRunner.php b/src/TUnit/framework/BaseTestRunner.php new file mode 100644 index 0000000..80bd60d --- /dev/null +++ b/src/TUnit/framework/BaseTestRunner.php @@ -0,0 +1,88 @@ +tests = $tests; + $this->listeners = $listeners; + $this->options = (!empty($options)) ? $this->parseOptions($options) : array(); + } + + public final function warn($message) { + foreach ($this->listeners as $listener) { + $listener->onFrameworkWarning($message); + } + } + + public final function error($message) { + foreach ($this->listeners as $listener) { + $listener->onFrameworkError($message); + } + } + + public final function addTest(Testable $test) { + $this->tests[] = $test; + return $this; + } + + public final function addListener(TestListener $listener) { + $this->listeners[] = $listener; + return $this; + } + + public final function setOptions(array $options) { + $this->options = $this->parseOptions($options); + return $this; + } + + protected final function parseOptions(array $unparsed) { + $allowedOptions = $this->getAllowableOptions(); + $options = array_fill_keys(array_key($allowedOptions), null); + foreach ($unparsed as $option => $value) { + if (!isset($allowedOptions[$option])) { + throw new InvalidOptionException($option); + } + if (gettype($value) !== $allowedOptions[$option]) { + throw new InvalidOptionException($option, 'Expected a value of type ' . $allowedOptions[$option] . ', got ' . gettype($value)); + } + + $options[$option] = $value; + } + + return $options; + } + + public function runTests() { + $results = array(); + foreach ($this->tests as $test) { + if ($test instanceof Testable) { + try { + $results[] = $test->run($this->listeners); + } catch (TUnitException $e) { + $this->error($e->getMessage()); + } + } else { + $this->warn('Unable to run test because it is not an instance of Testable (' . gettype($test) . ')'); + } + } + + return $results; + } + + public function publishResults(array $testResults) { + foreach ($this->listeners as $listener) { + $listener->publishTestResults($testResults); + } + } + + public abstract function run(); + + protected abstract function getAllowableOptions(); + + } + +?> \ No newline at end of file diff --git a/src/TUnit/framework/TestRunner.php b/src/TUnit/framework/TestRunner.php index 1547016..5f62e72 100644 --- a/src/TUnit/framework/TestRunner.php +++ b/src/TUnit/framework/TestRunner.php @@ -1,51 +1,19 @@ tests = $tests; - $this->listeners = $listeners; - } - - public final function addTest(Testable $test) { - $this->tests[] = $test; - return $this; - } - - public final function addListener(TestListener $listener) { - $this->listeners[] = $listener; - return $this; - } - - public function runTests() { - $results = array(); - foreach ($this->tests as $test) { - if ($test instanceof Testable) { - $results[] = $test->run($this->listeners); - } else { - foreach ($this->listeners as $listener) { - $listener->onFrameworkWarning('Unable to run test because it is not an instance of Testable (' . gettype($test) . ')'); - } - } - } - - return $results; - } - - public function publishResults(array $testResults) { - foreach ($this->listeners as $listener) { - $listener->publishTestResults($testResults); - } - } + 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"); diff --git a/src/TUnit/framework/exceptions/Exceptions.php b/src/TUnit/framework/exceptions/Exceptions.php new file mode 100644 index 0000000..842b204 --- /dev/null +++ b/src/TUnit/framework/exceptions/Exceptions.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/src/TUnit/manifest.php b/src/TUnit/manifest.php index b8f648c..b381817 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 01:41:57 + * Autogenerated by manifester.php on 2009-06-19 20:34:30 * * @package TUnit * @version 0.4.0 @@ -13,11 +13,13 @@ 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', 'Constraint' => 'TUnit/framework/constraints/Constraint.php', 'DefaultConstraint' => 'TUnit/framework/constraints/DefaultConstraint.php', 'EmptyConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', @@ -30,6 +32,7 @@ 'IdenticalConstraint' => 'TUnit/framework/constraints/IdenticalConstraint.php', 'IgnoredTest' => 'TUnit/framework/result/FailedTest.php', 'IgnoredTestResult' => 'TUnit/framework/result/IgnoredTestResult.php', + 'InvalidOptionException' => 'TUnit/framework/exceptions/Exceptions.php', 'InvocationExpectation' => 'TUnit/framework/mock/InvocationExpectation.php', 'InvocationTracker' => 'TUnit/framework/mock/InvocationTracker.php', 'IssetConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', @@ -47,13 +50,13 @@ 'RecursiveTestIterator' => 'TUnit/util/RecursiveTestIterator.php', 'SimpleConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', 'SingleTestResult' => 'TUnit/framework/result/SingleTestResult.php', + 'TUnitException' => 'TUnit/framework/exceptions/Exceptions.php', 'TestAccumulator' => 'TUnit/util/TestAccumulator.php', 'TestCase' => 'TUnit/framework/test/TestCase.php', 'TestFailure' => 'TUnit/framework/result/FailedTest.php', 'TestListener' => 'TUnit/framework/listeners/TestListener.php', 'TestMethod' => 'TUnit/framework/test/TestMethod.php', 'TestResult' => 'TUnit/framework/result/TestResult.php', - 'TestRunner' => 'TUnit/framework/TestRunner.php', 'TestSuite' => 'TUnit/framework/test/TestSuite.php', 'Testable' => 'TUnit/framework/test/Testable.php', 'TrueConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php', diff --git a/src/TUnit/util/entry_console.php b/src/TUnit/util/entry_console.php index a9da6fd..ce514e5 100644 --- a/src/TUnit/util/entry_console.php +++ b/src/TUnit/util/entry_console.php @@ -22,30 +22,35 @@ $switches = new CliSwitchCollection(); $switches->addSwitch(new CliSwitch(null, null, true, '', 'Files and/or directories to parse for test cases')) ->addSwitch(new CliSwitch('help', 'h', false, null, 'Display this help message (--usage is an alias)')) - ->addSwitch(new CliSwitch('recursive', null, false, null, 'Recurse into subdirectories')); + ->addSwitch(new CliSwitch('recursive', null, false, null, 'Recurse into subdirectories')) + ->addSwitch(new CliSwitch('bootstrap', 'b', false, 'file', 'File to include before each test')); array_shift($argv); $args = Cli::parseArgs($argv, $switches); + //print_r($args); exit; - if (isset($args['help'])) { + $localSwitches = $args['switches']; + $localArgs = $args['args']; + + if (isset($localSwitches['help'])) { usage(); exit(0); } - if (empty($args['args'])) { + if (empty($localArgs)) { usage(); exit(1); } //accumulate tests - $tests = TestAccumulator::getTests($args['args'], isset($args['recursive'])); + $tests = TestAccumulator::getTests($localArgs, isset($localSwitches['recursive'])); if (empty($tests)) { fwrite(STDERR, 'Found no TestCase subclasses in the given files'); exit(1); } - $runner = new TestRunner(array(new TestSuite('Main Test Suite', $tests)), array(new ConsoleListener(/*ConsoleListener::VERBOSITY_HIGH*/))); + $runner = new ConsoleTestRunner(array(new TestSuite('Main Test Suite', $tests)), array(new ConsoleListener(/*ConsoleListener::VERBOSITY_HIGH*/)), $localSwitches); $runner->run(); exit(0);