如果一个或多个抽象方法不是公共的,看起来 $this->getMock(...) 将失败抽象类。
如果您不想使用派生类,可以在测试基类中添加类似这样的内容:
protected function getMockOfAbstractClass($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) {
if ($methods !== null) {
$methods = array_unique(array_merge($methods,
$this->getMethods($originalClassName, $callAutoload)));
}
return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload);
}
private function getMethods($class, $autoload=true) {
$methods = array();
if (class_exists($class, $autoload) || interface_exists($class, $autoload)) {
$reflector = new ReflectionClass($class);
foreach ($reflector->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_ABSTRACT ) as $method) {
$methods[] = $method->getName();
}
}
return $methods;
}
这改编自:Mocking concrete method in abstract class using phpunit