phpunit - mockbuilder - set mock object internal property
是否可以使用禁用的构造函数和手动设置的受保护属性创建模拟对象?
这是一个愚蠢的例子:
class A {
protected $p;
public function __construct(){
$this->p = 1;
}
public function blah(){
if ($this->p == 2)
throw Exception();
}
}
class ATest extend bla_TestCase {
/**
@expectedException Exception
*/
public function testBlahShouldThrowExceptionBy2PValue(){
$mockA = $this->getMockBuilder('A')
->disableOriginalConstructor()
->getMock();
$mockA->p=2; //this won't work because p is protected, how to inject the p value?
$mockA->blah();
}
}
所以我想注入受保护的p值,所以我不能。我应该定义 setter 还是 IoC,或者我可以使用 phpunit 来做到这一点?