非常简单的测试框架,有兴趣的朋友可以增加功能。
<?php
/**
* Base test class
* All test case must extends from this class
*/
class test {
/* All construct method must inherited test() method.
* $name: The name of the test.
*/
function test($name) {
assert_options( ASSERT_CALLBACK, 'assert_callback');
$name = "PHP test \"".$name."\"";
echo "<table width=\"100%\">
<tr>
<td bgcolor=\"#C9C4C4\">
<div align=\"center\">
<font size=\"6\">".$name."</font>
</div></td>
</tr>
</table><br>";
}
/**
* initialize all class variable that needed.
*/
function setup() {
}
/**
* destrouct variables;
*/
function tear_down() {
}
/**
* run the test.
*/
function do_test() {
$functions = get_class_methods(&$this);
$this->setup();
while (list($k, $v) = each($functions)) {
switch ($v){
case "setup":
break;
case "test":
break;
case "tear_down":
break;
case "do_test":
break;
default:
if ($v != get_class($this)) {
echo "<font color=\"#FF00E6\"> *** Test method \"$v\" ***</font><br><br>";
call_user_func(array(&$this, $v));
echo "<font color=\"#4FAF2B\"> *** Success ! *** </font><br><br>";
}
}
}
$this->tear_down();
}
}
/**
* Function show array
* Show the content of $array;
*/
function show_array($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
/**
* assert call back function.
*/
function assert_callback( $script, $line, $message ) {
echo '<font color="#FF0033">You have a design error in your script <b>', $script,'</b> : line <b>', $line,'</b> :<br />';
echo '<br />Source code: "<b>', ereg_replace( '^.*//\*', '', $message ), '"</b><br /><br />';
echo 'Open the source file and check it, because it\'s not a normal behaviour !</font>';
exit;
}
?>