Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To configure Mockery to provide output depending on earlier method invocations, we need to use the shouldReceive method with the andReturn or andThrow methods.

Here is an example:

$mock = Mockery::mock(SomeClass::class);
$mock->shouldReceive('method1')->andReturn('result');

// Call the first method
$result1 = $mock->method1(); // returns 'result'

$mock->shouldReceive('method2')->with($result1)->andReturn('another result');

// Call the second method, passing the result from the first method
$result2 = $mock->method2($result1); // returns 'another result'

In this example, we create a mock of SomeClass and configure it to return 'result' when its method1 is called. We then call method1 to get the result, which we use as an argument when configuring method2. We configure method2 to return 'another result' when called with the result of method1. We call method2 with the result of method1 as an argument, and Mockery returns 'another result'.

By using these methods, we can simulate complex behavior in a controlled way and test our code thoroughly.