PHPUnit 的 returnValueMap 未产生预期结果
我正在尝试使用PHPUnit的returnValueMap()来存根读取的结果。它不会产生预期的结果,但等效的 returnCallback() 会。我已经提供了我的测试用例,如果你想自己检查它。
returnValueMap()
$enterprise = $this->getMock('Enterprise', array('field'));
$enterprise->expects($this->any())
->method('field')
->will($this->returnValueMap(array(
array('subscription_id', null),
array('name', 'Monday Farms')
)));
$enterprise->subscribe('basic');
结果:
Subscription ID: NULL
Name: NULL
returnCallback()
$enterprise = $this->getMock('Enterprise', array('field'));
$enterprise->expects($this->any())
->method('field')
->will($this->returnCallback(function ($arg) {
$map = array(
'subscription_id' => null,
'name' => 'Monday Farms'
);
return $map[$arg];
}));
$enterprise->subscribe('basic');
结果:
Subscription ID: NULL
Name: string(12) "Monday Farms"
企业::订阅
public function subscribe() {
echo 'Subscription ID: ';
var_dump($this->field('subscription_id'));
echo 'Name: ';
var_dump($this->field('name'));
}
为什么 returnValueMap() 不能按预期工作?我到底错过了什么?