PriestVallon
PriestVallon

Reputation: 1518

Return Argument from PHPUnit Test as part of an array

I want to return an array from this mocked method and I want it to contain one of the arguments originally passed to the method.

The code below runs but instead of data having the original argument value it's an object of type

PHPUnit_Framework_MockObject_Stub_ReturnArgument

$stub->method('insert')->willReturn(array('success'=>true,'data'=>$this->returnArgument(0)));

The only way I've been able to get the original argument is to return only the argument but then I don't have the array I need

$stub->method('insert')->will('data'=>$this->returnArgument(0));

Is there something small I'm doing wrong or do I need to try an implement a callback or something like that?

Upvotes: 3

Views: 1121

Answers (1)

Matteo
Matteo

Reputation: 39390

You can use returnCallback as example:

        ->will($this->returnCallback(function ($arg1) {
                return array('success'=>true,'data'=>$arg1);
        }));

Hope this help

Upvotes: 4

Related Questions