initial version of tunit (totally untested)

This commit is contained in:
tmont 2009-06-13 06:00:17 +00:00
parent eb06ed24b5
commit d92fb480c0
9 changed files with 312 additions and 0 deletions

13
Assert.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class Assert {
private function __construct() {}
public static function that($expected, $value, $message = '') {
}
}
?>

8
FailedTest.php Normal file
View File

@ -0,0 +1,8 @@
<?php
class TestFailure extends Exception {}
class ErredTest extends FailedTest {}
class IgnoredTest extends FailedTest {}
class FailedTest extends FailedTest {}
?>

103
TestCase.php Normal file
View File

@ -0,0 +1,103 @@
<?php
class TestCase implements Testable {
protected $name;
public function __construct($name) {
$this->name = $name;
}
protected function setUp() {
}
protected function tearDown() {
}
public function run(array $listeners) {
foreach ($listeners as $listener) {
$listener->onBeforeTestCase($this);
}
foreach ($this->getTestableMethods() as $method) {
$this->runTestMethod($method);
}
foreach ($listeners as $listener) {
$listener->onAfterTestCase($this);
}
}
public function runTestMethod(ReflectionMethod $method, array $listeners) {
foreach ($listeners as $listener) {
$listener->beforeTestMethod($method);
}
$testPassed = false;
$result = null;
$failure = null;
$this->setUp();
try {
$method->invoke($this);
$testPassed = true;
} catch (TestFailure $failure) {
if ($failure instanceof FailedTest) {
foreach ($listeners as $listener) {
$listener->onTestMethodFailed($method);
}
} else if ($failure instanceof IgnoredTest) {
foreach ($listeners as $listener) {
$listener->onTestMethodIgnored($method);
}
} else if ($failure instanceof ErredTest) {
foreach ($listeners as $listener) {
$listener->onTestMethodErred($method);
}
}
} catch (Exception $e) {
//test for expected exception
foreach ($listeners as $listener) {
$listener->onTestMethodErred($method);
}
}
$result = $this->createTestResult($method, $failure);
$this->tearDown();
foreach ($listeners as $listener) {
$listener->afterTestMethod($method);
}
}
protected function createTestResult(ReflectionMethod $method, TestFailure $failure = null) {
if ($failure === null) {
return new PassedTestResult($method);
} else if ($failure instanceof FailedTest) {
return new FailedTestResult($method, $failure);
} else if ($failure instanceof ErredTest) {
return new ErredTestResult($method, $failure);
} else if ($failure instanceof IgnoredTest) {
return new IgnoredTestResult($method, $failure);
}
throw new InvalidArgumentException('Unknown test failure type: ' . get_class($failure));
}
protected final function getTestableMethods() {
$refClass = new ReflectionClass($this);
$methods = array();
foreach ($refClass->getMethods() as $method) {
if (preg_match('/^[\*\s]*@test\s*$/m', $method->getDocBlock())) {
$methods[] = $method;
}
}
return $methods;
}
}
?>

73
TestListener.php Normal file
View File

@ -0,0 +1,73 @@
<?php
class TestListener {
protected $reporter;
public function __construct(TestReporter $reporter) {
$this->reporter = $reporter;
}
public function beforeTestSuite(TestSuite $suite) {
}
public function afterTestSuite(TestSuite $suite) {
}
public function beforeTestCase(TestCase $test) {
}
public function afterTestCase(TestCase $test) {
}
public function onTestCaseFailed(TestCase $test) {
}
public function onTestCasePassed(TestCase $test) {
}
public function onTestCaseErred(TestCase $test) {
}
public function beforeTestMethod(ReflectionMethod $method) {
}
public function afterTestMethod(ReflectionMethod $method) {
}
public function onTestMethodFailed(ReflectionMethod $method) {
}
public function onTestMethodPassed(ReflectionMethod $method) {
}
public function onTestMethodErred(ReflectionMethod $method) {
}
public function onTestMethodIgnored(ReflectionMethod $method) {
}
public function onFrameworkError($message) {
}
public function onFrameworkWarning($message) {
}
}
?>

9
TestReporter.php Normal file
View File

@ -0,0 +1,9 @@
<?php
interface TestReporter {
public function write(TestResult $result);
}
?>

11
TestResult.php Normal file
View File

@ -0,0 +1,11 @@
<?php
abstract class TestResult {
public function __construct(TestCase $test) {
}
}
?>

39
TestRunner.php Normal file
View File

@ -0,0 +1,39 @@
<?php
class TestRunner {
protected $tests;
protected $name;
protected $listeners;
public function __construct($name, array $tests, array $listeners = array()) {
$this->name = $name;
$this->tests = $tests;
$this->listeners = $listeners;
}
public function addTest(Testable $test) {
$this->tests[] = $test;
return $this;
}
public function addListener(TestListener $listener) {
$this->listeners[] = $listener;
return $this;
}
public function run() {
foreach ($this->tests as $test) {
if ($test instanceof Testable) {
$test->run($this->listeners);
} else {
foreach ($this->listeners as $listener) {
$listener->onFrameworkWarning('Unable to run test because it is not an instanceof Testable (' . gettype($test) . ')');
}
}
}
}
}
?>

47
TestSuite.php Normal file
View File

@ -0,0 +1,47 @@
<?php
class TestSuite implements Testable {
protected $name;
protected $tests;
public function __construct($name, array $tests) {
$this->name = $name;
$this->tests = $tests;
}
protected function setUp() {
}
protected function tearDown() {
}
public function run(array $listeners) {
$this->setUp();
foreach ($this->tests as $test) {
if ($test instanceof self) {
foreach ($this->listeners as $listener) {
$listener->beforeTestSuite($test);
}
$result = $test->run($listeners);
foreach ($this->listeners as $listener) {
$listener->afterTestSuite($test);
}
} else if ($test instanceof TestCase) {
$test->run($listeners);
} else {
foreach ($this->listeners as $listener) {
$listener->onFrameworkWarning('Unable to run test because it is not an instanceof Testable (' . gettype($test) . ')');
}
}
}
$this->tearDown();
}
}
?>

9
Testable.php Normal file
View File

@ -0,0 +1,9 @@
<?php
interface Testable {
public function run(array $listeners);
}
?>