我想知道,当我们最初不知道该方法的参数数量时,我们可以创建一个phpunit模拟 通常,当我们知道方法调用的数量时,我们会做这样的事情
mockResponse->expects($this->exactly(2))
-> method('tmpFunc')
->withConsecutive(['header1'], ['header2']);
我想做的是让它变得更有活力
function mockMethod($n, $params) // $params is an array of strings
{
$mockResponse = $this->getMockBuilder('PMA\libraries\Response')
->disableOriginalConstructor()
->setMethods(array('tempFunc', 'headersSent'))
->getMock();
if($n > 1)
{
$mockResponse->expects($this->exactly($n))
->method('tempFunc')
->withConsecutive( //todo );
$mockResponse->expects($this->any())
->method('headersSent')
->with()
->will($this->returnValue(false));
}
}
例如,如果$n = 2
和$params = array('HTTP/1.1 303 See Other', 'Location: index.php?lang=en')
那么函数应该执行此操作
$mockResponse = $this->getMockBuilder('PMA\libraries\Response')
->disableOriginalConstructor()
->setMethods(array('tempFunc', 'headersSent'))
->getMock();
$mockResponse->expects($this->exactly($n))
->method('tempFunc')
->withConsecutive([$params[1]], [$params[2]]);
$mockResponse->expects($this->any())
->method('headersSent')
->with()
->will($this->returnValue(false));
如何替换todo以便如果$ n = 2则每个字符串将作为参数发送到tempFunc()。
public function tempFunc($text)
{
header($text);
}
public function headersSent()
{
return headers_sent();
}
答案 0 :(得分:1)
我终于得到了我认识的人的回答
$header_method = $mockResponse->expects($this->exactly(count($param)))
->method('tmpFunc');
call_user_func_array(array($header_method, 'withConsecutive'), $param);