在抽象类的单元测试期间,我遇到了一个问题,即闭包的$ this上下文是当前的测试类,而不是模拟的抽象类。我需要测试该方法中某些类属性的访问权限,但我无法访问正确的上下文。有没有办法实现这个目标?
示例:
abstract class AbstractClass {
/**
*
*/
protected $_attributes;
/**
*
*/
public function __construct($attributes) {
$this->_attributes = $attributes;
}
/**
*
*/
public abstract function processAttributes();
}
class AbstractClassTest extends PHPUnit_Framework_TestCase {
public function testAttributesProcessing() {
$closure = function() {
// $this context is now AbstractClassTest
// but the context of AbstractClass is desired
};
$stub = $this->getMockForAbstractClass("AbstractClass");
$stub->expects($this->any())
->method("processAttributes")
->will($this->returnCallback($closure);
$stub->processAttributes();
}
}
我试图将闭包绑定到$ stub,但上下文不会改变:
$closure->bindTo($stub);