diff --git a/Assert.php b/Assert.php new file mode 100644 index 0000000..7a06e74 --- /dev/null +++ b/Assert.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/FailedTest.php b/FailedTest.php new file mode 100644 index 0000000..606addb --- /dev/null +++ b/FailedTest.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/TestCase.php b/TestCase.php new file mode 100644 index 0000000..abb4c9d --- /dev/null +++ b/TestCase.php @@ -0,0 +1,103 @@ +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; + } + + } + +?> \ No newline at end of file diff --git a/TestListener.php b/TestListener.php new file mode 100644 index 0000000..4e71776 --- /dev/null +++ b/TestListener.php @@ -0,0 +1,73 @@ +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) { + + } + + } + +?> \ No newline at end of file diff --git a/TestReporter.php b/TestReporter.php new file mode 100644 index 0000000..2aa5883 --- /dev/null +++ b/TestReporter.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/TestResult.php b/TestResult.php new file mode 100644 index 0000000..2d5c409 --- /dev/null +++ b/TestResult.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/TestRunner.php b/TestRunner.php new file mode 100644 index 0000000..941bb52 --- /dev/null +++ b/TestRunner.php @@ -0,0 +1,39 @@ +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) . ')'); + } + } + } + } + + } + +?> \ No newline at end of file diff --git a/TestSuite.php b/TestSuite.php new file mode 100644 index 0000000..4f74c41 --- /dev/null +++ b/TestSuite.php @@ -0,0 +1,47 @@ +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(); + } + + } + +?> \ No newline at end of file diff --git a/Testable.php b/Testable.php new file mode 100644 index 0000000..0281a9e --- /dev/null +++ b/Testable.php @@ -0,0 +1,9 @@ + \ No newline at end of file