Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the argument of a mocked method?

How can I make it so in PHPUnit a mocked method returns its passed argument?

E.g:

$redirector->expects( $this->once() )->method('gotoUrl')->will( $this->returnValue($argument) );
like image 399
Darren Avatar asked Dec 04 '22 11:12

Darren


2 Answers

As per the PHPUnit manual:

$stub->expects($this->any())
     ->method('doSomething')
     ->will($this->returnArgument(0));
like image 183
Arjan Avatar answered Dec 09 '22 15:12

Arjan


$redirector->expects($this->once())
           ->method('gotoUrl')
           ->will($this->returnCallback(function() {
                $args = func_get_args();
                return $args[0]; // or w/e
            ));
like image 38
maxiscool Avatar answered Dec 09 '22 15:12

maxiscool