Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

megaman's avatar

mocks with multiple shouldRecieve() statements

I am trying to create a mock which has two shouldRecieve statements

Something like this

$mock = $this->mock(Service::class, function (MockInterface $mock) {

$mock->shouldReceive('init')->once();

$mock->shouldReceive('process')->once();

});

ive tried several variations but no matter what it try it only mocks one of the functions

shouldrecieve can take multiple arguments as an array but if i do this there is no apparent way to set a different set of asserts/function ( once(), with(), andReturns() ect ) to each mocked function

0 likes
1 reply
megaman's avatar

I have fixed this

It turned out that the class i was trying to mock had a builder class which ran several functions which returned itself before a function which returned the class i was trying to mock

so for each of the mocked functions in the builder, except the one that returns the class it builds, i had to call

andReturn($builderMock)

Please or to participate in this conversation.