I am trying to send a pre-calculated array of test cases into a mock to be returned on consecutive calls - similar to the map function. However it takes a list of arguments, not an array.
My array is already generated by a fixture-generator and may be of variable length.
What I want to do is something like this, but of course this causes it to return the entire array on the first call.
// In a test case:
$processorMock
->method('process')
->willReturnOnConsecutiveCalls(
$fixtureLoader->getProcessorScenarios() // how to explode this?
);
// class FixtureLoader pseudocode:
function getProcessorScenarios(){
return [
[ $param1, $param2, $param3 ], // case 1
[ $param1, $param2, $param3 ], // case 2
...
[ $param1, $param2, $param3 ], // case N
];
}
I want to destructure the returned array from the fixture loader, using list() or some var-args exploding language construct like "..." in other languages, but couldn't find anything native that worked.
I do have what feels like a hacky method, which I will post as an initial answer Q&A style. I want to know if it's the best method.
A friend suggested an alternative available in PHP 5.6 - the "splat" operator or elipsis (...) although it doesn't seem to have an official name in PHP, but is a form of destructuring like the "spread" operator in Javascript ES6.
While I haven't tested this (as my app is currently locked to PHP 5.4 due to environmental constraints), it should therefore work simply thus:
$processorMock
->method('process')
->willReturnOnConsecutiveCalls(
...$fixtureLoader->getProcessorScenarios()
);
This is definitely the sweeter alternative I was looking for.
The approach I found works is using the call_user_func_array function to expand the array into parameters.
call_user_func_array(
[$processorMock->method('process'), 'willReturnOnConsecutiveCalls'],
$fixtureLoader->getProcessorScenarios());
This feels nasty, and breaks up the fluent definition of the mock. I'm guessing it will have downsides such as brittleness for the future.
I'd like to find a sweeter alternative before it becomes the copy-pasta template solution!
Update:
I've since found I need to wrap some of the parameters in asserts, as they are more complex types, e.g. classes.
// class FixtureLoader pseudocode:
function getProcessorScenarios(){
$param1 = PHPUnit_Framework_Assert::IsInstanceOf('MyApp\Model\Thingumy');
$param1 = PHPUnit_Framework_Assert::isInstanceOf('MyApp\Model\Whatsit');
return [
[ $param1, $param2c1, $param3 ], // case 1
[ $param1, $param2c2, $param3 ], // case 2
...
[ $param1, $param2c3, $param3 ], // case N
];
}
It's not necessary to use willReturnOnConsecutiveCalls to get the outcome you desire.
The @dataProvider will take an array of test data and feed it into your test one batch at a time, for as many times as necessary to get through the data.
I wasn't 100% clear on whether you have a variable number of tests, or a variable number of parameters.
Example for 3 variables:
/**
* @dataProvider getProcessorScenarios
*/
function testcase($param1, $param2, $param3) {
// use the variables
}
function getProcessorScenarios() {
return [
[ $param1, $param2, $param3 ], // case 1
[ $param1, $param2, $param3 ], // case 2
...
[ $param1, $param2, $param3 ], // case N
];
}
A way you could do this with a changing number of variables would be to pass the variables as an array into the test function instead of individually, But I don't know how you could use them if you don't know how many are coming, so I don't think this is a valid solution.
Example with changing number of variables:
/**
* @dataProvider getProcessorScenarios
*/
function testcase($params) {
// use the variables which are in an array
}
function getProcessorScenarios() {
return [
[ 'params' => [$param1, $param2, $param3 ]], // case 1
[ 'params' => [$param1, $param2, $param3 ]], // case 2
...
[ 'params' => [$param1, $param2, $param3 ]], // case N
];
}