I wanted to know that can we create a phpunit mock when we do not know initially the number of arguments for the method Normally we would do something like this when we know the number of method invocations
mockResponse->expects($this->exactly(2))
-> method('tmpFunc')
->withConsecutive(['header1'], ['header2']);
What I would like to do is get it to be something more dynamic
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));
}
}
So for example if $n = 2
and $params = array('HTTP/1.1 303 See Other', 'Location: index.php?lang=en')
then the function should do this
$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));
How should I replace the todo so that if $n = 2 then each string will be sent as an argument to tempFunc().
public function tempFunc($text)
{
header($text);
}
public function headersSent()
{
return headers_sent();
}