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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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); } } |