Hi there,
We've started unit testing our application with PHPUnit 1 month ago (meaning we're doing "test after", unfortunately).
The last trouble I'm running into is that there doesn't seem to be a way to mock values returned via
parameters passed by-reference.
For example, I can't mock the $user returned by getUser(&$user, $userId), where the first parameter is used
to return the user (the function's return type being a status code).
You can't do this with returnCallback() since values assigned to the parameters passed by ref. are lost...
Has anybody ran into this trouble and already found a workaround ?
(for now mine is using hand crafted stubs, but this becomes cumbersome very quickly)
Thanks in advance!
Mocking functions with parameters passed by reference
Moderator: General Moderators
Re: Mocking functions with parameters passed by reference
Can you post the test that is problematic? I'm not a user of phpunit but I reckon I can come up with a solution 
Re: Mocking functions with parameters passed by reference
Hehe ok, I'll try to put up a simple example that will isolate the problem clearly... and post it here.
Re: Mocking functions with parameters passed by reference
Does this isolate the problem?
Both of these tests should pass. I am submitting a feature enhancement request. http://www.phpunit.de/ticket/976
Code: Select all
<?php
class myMock
{
function returnReference( $a )
{
return $a;
}
}
class TestPHPUnit extends PHPUnit_Framework_TestCase
{
function testReal()
{
$a = new stdClass;
$b = new stdClass;
$this->assertFalse( $a === $b, 'we understand how references work' );
$this->assertTrue( $a === $a, 'we understand how references work' );
$mockObject = $this->getMock( 'myMock' );
$mockObject->expects( $this->any() )->method( 'returnReference' )->will( $this->returnArgument(0) );
$c = $mockObject->returnReference( $a );
$this->assertTrue( $a == $c, 'equals should work' );
$this->assertTrue( $a === $c, 'references should work' );
}
function testMock()
{
$a = new stdClass;
$b = new stdClass;
$this->assertFalse( $a === $b, 'we understand how references work' );
$this->assertTrue( $a === $a, 'we understand how references work' );
$mockObject = new myMock; // use a real object
$c = $mockObject->returnReference( $a );
$this->assertTrue( $a == $c, 'equals should work' );
$this->assertTrue( $a === $c, 'references should work' );
}
}Re: Mocking functions with parameters passed by reference
Mine was a duplicate and someone already wrote this patch. http://www.phpunit.de/ticket/940