PHPunit模拟对象抽象和静态方法 [英] PHPunit mockobject abstract and static method
问题描述
我想测试一个抽象类中的方法.在此类中,有一个带有static的抽象方法.
I would like to test a method from an abstract class. In this class is there a abstract method with is static.
我使用PHPUnit.使用普通的抽象方法,它可以工作:
I use PHPUnit. With normal abstract methods it works:
<?php
abstract class AbstractClass
{
public function concreteMethod()
{
return $this->abstractMethod();
}
public abstract function abstractMethod();
}
class AbstractClassTest extends PHPUnit_Framework_TestCase
{
public function testConcreteMethod()
{
$stub = $this->getMockForAbstractClass('AbstractClass');
$stub->expects($this->any())
->method('abstractMethod')
->will($this->returnValue(TRUE));
$this->assertTrue($stub->concreteMethod());
}
}
?>
phpunit file.php可以工作.
phpunit file.php works.
但是如果abstractMethod是静态的,它将显示:
But if the abstractMethod is static it displays:
PHP致命错误:类Mock_AbstractClass_6332ae11包含1个抽象方法,因此必须声明为抽象或实现/usr/local/apache2/php5.3/lib/php/PHPUnit/Framework/中的其余方法(AbstractClass :: abstractMethod) TestCase.php(1135):第33行上的eval()代码
PHP Fatal error: Class Mock_AbstractClass_6332ae11 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::abstractMethod) in /usr/local/apache2/php5.3/lib/php/PHPUnit/Framework/TestCase.php(1135) : eval()'d code on line 33
推荐答案
您不能拥有抽象的静态方法.它将在PHP中生成一条E_STRICT消息.
You can't have abstract static methods. It will generate an E_STRICT message in PHP.
为您的类实现设计替代策略.
Devise an alternative strategy for your class implementation.
这篇关于PHPunit模拟对象抽象和静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!