As a guy whose responsibility among other things is unit testing in php using PhpUnit, Closures are also you great friend for testing a code which was not written in the best regards of test.
About two months ago I took over a team who had been developing a project with no unit tests what so ever and decided to dedicate a few programmers to learn how to unit test and write the tests. The project had service locator all over it, and I had to find a solution on how to solve the problem and with the help of a SO user Elias Van Ootegem I was able to.
As mentioned in the post, you can also simulate parameters passed and returned through a reference.
Should you have a function to test:
public function ReturnIntAndChangeOutputParameter(&$outputParameter = false)
{
$outputParameter = true;
return 10;
}
You can use callback to simulate the parameter return:
$function = function(&$outputParameter)
{
$outputParameter = true;
return 10;
}
$classMock
->expects(/* */)
->method('ReturnIntAndChangeOutputParameter')
->will($this->returnCalback($function));
In fact, when writing unit tests, this is the only way how to do that, PhpUnit otherwise converts all variables to be passed by value even if the definition is pass by reference.