否时是否可以通过多次调用创建模拟。和参数类型未知?
Is it possible to create mock with multiple calls when the no. and type of arguments are unknown?
我想知道当我们最初不知道该方法的参数数量时,我们能否创建一个 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));
我应该如何替换待办事项,以便如果 $n = 2 那么每个字符串都将作为参数发送给 tempFunc()。
public function tempFunc($text)
{
header($text);
}
public function headersSent()
{
return headers_sent();
}
我终于从认识的人那里得到了答案
$header_method = $mockResponse->expects($this->exactly(count($param)))
->method('tmpFunc');
call_user_func_array(array($header_method, 'withConsecutive'), $param);
我想知道当我们最初不知道该方法的参数数量时,我们能否创建一个 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));
我应该如何替换待办事项,以便如果 $n = 2 那么每个字符串都将作为参数发送给 tempFunc()。
public function tempFunc($text)
{
header($text);
}
public function headersSent()
{
return headers_sent();
}
我终于从认识的人那里得到了答案
$header_method = $mockResponse->expects($this->exactly(count($param)))
->method('tmpFunc');
call_user_func_array(array($header_method, 'withConsecutive'), $param);