PHPunit mockobject 抽象和静态方法 [英] PHPunit mockobject abstract and static method
问题描述
我想测试一个抽象类的方法.在这个类中有一个抽象方法,它是静态的.
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 mockobject 抽象和静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!