While testing PHP we need sometimes to create a mock for a method with reference parameters. I use returnCallback() from PHPUnit framework to to this. returnCallback() will call a function that imitates functionality of the original method. The original method can be either public or protected.
For example:
class A
{
function getReference(&$ref)
{
$ref = 99;
return 0;
}
}
class ATest extends PHPUnit_Framework_TestCase
{
function testgetReference()
{
$f = function(&$ref)
{
$ref = 100;
return true;
};
$mock = $this->getMock('A');
$mock->expects($this->any())
->method('getReference')
->will($this->returnCallback($f));
$return = $mock->getReference($a);
$this->assertEquals(100, $a);
$this->assertTrue($return);
}
}