Reputation: 56
I'm rather new to the PHP Mockery framework, and attempting to test multiple functions on a mocked service request function. The different functions send the same variable names with different values. I'd like to test to make sure each case is hitting the mocked class. Can I receive multiple arguments with my mocked function and return each of them to the tester function?
The function to test:
public function doSomething() {
$result = $this->serviceRequest('stuff', '/url', []);
return $result;
}
The pseudocode for testing function:
public function testDoSomething() {
$service_mock = $this->mockService();
$this->assertEquals($service_mock->doSomething(), $returnedArgs);
}
I'd ideally like to take those three arguments being sent to the mock, return them with the mock's andReturn()
or something similar to be asserted in the original test method (to verify they are being sent to the mock). Is there a good way to do this or a common practice I'm missing?
Right now, my mock is looking similar to this pseudocode (and those 3 arguments are being validated in closure $args
):
$client->shouldReceive('serviceRequest')->withArgs($args)->once()->andReturnUsing($args)->getMock();
Thanks!
Upvotes: 1
Views: 2903
Reputation: 1444
You can use a callback to do this: The below example is adapted form the Mockery docs, although it uses it as an example for reference passing, it can be used for any custom expectations.
$m->shouldReceive('insert')->with(
\Mockery::on(function(&$data) use($logger) {
if (!is_array($data)) return false;
$data['_id'] = 123;
$logger->logParameters('insert', $data);
return true;
}),
\Mockery::any()
);
http://docs.mockery.io/en/latest/reference/pass_by_reference_behaviours.htm
Upvotes: 2