| <?php |
| require_once(dirname(__FILE__) . '/debug.php'); |
| trace_file_info(__FILE__); |
| |
| class TestRunner { |
| var $target; |
| |
| function TestRunner($target) { |
| $this->target = $target; |
| } |
| |
| function run_tests() { |
| $methods = get_class_methods($this->target); |
| if (!$methods) return; |
| foreach($methods as $method) { |
| if (substr($method, 0, 4) == 'test') { |
| $this->run_test($method); |
| } |
| } |
| $this->showTrace(); |
| } |
| |
| function showTrace() { |
| echo get_trace_html(); |
| } |
| |
| function displayStartTest($name) { |
| echo "<h1>Running test: $name</h1><blockquote>"; |
| } |
| |
| function displayError($message) { |
| echo "<p><font color=\"red\">$message</font></p>"; |
| } |
| |
| function displayEndTest($name) { |
| echo "</blockquote>"; |
| } |
| |
| function run_test(&$test) { |
| $name = "$this->target#$test"; |
| $this->displayStartTest($name); |
| try { |
| $instance = new $this->target($this); |
| $instance->setUp(); |
| $instance->$test(); |
| } catch (TestFailedException $e) { |
| // Ignore |
| } catch (Exception $e) { |
| $message = $e->getMessage(); |
| $this->displayError("An unhandled exception occurred while running $test: $message"); |
| } |
| $instance->tearDown(); |
| $this->displayEndTest($name); |
| } |
| } |
| |
| class TextTestRunner extends TestRunner { |
| function showTrace() { |
| echo get_trace_text(); |
| } |
| |
| function displayStartTest($name) { |
| echo "=== Running test: $name\n"; |
| } |
| |
| function displayError($message) { |
| echo "*** $message\n"; |
| } |
| |
| function displayEndTest($name) { |
| echo "\n\n"; |
| } |
| } |
| |
| class TestFailedException extends Exception { |
| } |
| |
| class TestCase { |
| var $runner; |
| |
| function __construct($runner) { |
| $this->runner = $runner; |
| } |
| |
| function setUp() { |
| } |
| |
| function tearDown() { |
| } |
| |
| function fail($message) { |
| $this->runner->displayError("Test failed: $message"); |
| throw new TestFailedException(); |
| } |
| |
| function assertEquals($expected, $actual) { |
| if ($expected != $actual) { |
| $this->runner->displayError("Expected \"$expected\" but found \"$actual\""); |
| throw new TestFailedException(); |
| } |
| } |
| |
| function assertNull($actual) { |
| if ($actual !== null) { |
| $this->runner->displayError("Expected null but found \"$actual\""); |
| throw new TestFailedException(); |
| } |
| } |
| |
| function assertNotNull($actual) { |
| if ($actual === null) { |
| $this->runner->displayError("Expected a value, but got null."); |
| throw new TestFailedException(); |
| } |
| } |
| |
| function assertTrue($value) { |
| if (!$value) { |
| $this->runner->displayError("Expected true value (or equivalent)."); |
| throw new TestFailedException(); |
| } |
| } |
| |
| function assertFalse($value) { |
| if ($value) { |
| $this->runner->displayError("Expected false value (or equivalent)."); |
| throw new TestFailedException(); |
| } |
| } |
| |
| /** |
| * This function attempts to assert the sameness of the two parameters. |
| */ |
| function assertSame($value1, $value2) { |
| if ($value1 !== $value2) { |
| $this->runner->displayError("Values are not identical"); |
| throw new TestFailedException(); |
| } |
| } |
| } |
| ?> |
| |