使用反射 实现的 简单的 单元测试框架
– 原文 (飞扬轻狂,fallseir) 20090427
==== generalized test methods use introspection ====
class customClass{
function selfTest(){[..] return [message|false]}
}
class tester{
function test($thing){
if(is_object($thing)){
if(method_exists($thing,'selfTest')){
$this->handleTest(call_user_method('selfTest',$thing));
}
}else if(is_array($thing)){
foreach($thing as $component){ $this->test($component); }
}else{
;//ignore if not an array or object
}
}
function handleTest($result){if($result)print "Warning $result";}
}
参考:
John.Wiley.and.Sons.PHP5.and.MySQL.Bible