Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock a function that variable be passed by reference with PHPUnit?

My code looks something like:

class A
{
    function foo(&$a, $b)
    {
        if ($a == 0) {
            return false;
        } else {
            $a = $b + 1;
            return true;
        }
    }
}

class B
{
    function foo1($a, $b)
    {
        $a = new A;
        $a->foo($a, $b);
        if ($a == 0) {
            return false;
        }
        echo $a;
        return true;
    }
}

I need to get the value of $a.
How to mock the function foo()?

like image 453
user1679417 Avatar asked Nov 13 '22 22:11

user1679417


1 Answers

PHPUnit clones the arguments before passing them to the mocked method. There's no work-around for primitive arguments, but for objects you can add a non-public __clone method to block this behavior.

like image 66
David Harkness Avatar answered Nov 15 '22 11:11

David Harkness