Even though there's an accepted answer, I wanted to provide future users with another solution.
There should be no need to mock the router if it's only used as an intermediate step of the demeter chain. Try this:
$mock = M::mock('\Api\SessionMiddleware');
$mock->shouldReceive('getApplication->router->getCurrentRoute')->andReturn('myRoute');
The key is removing the call to shouldDeferMissing()
, which in this case seems to interfere with the demeter chain.
This way, a "pure" mock, which doesn't forward anything to the real implementation of SessionMiddleware
, is returned. It should be able to reply with 'myRoute'
when $mock->getApplication()->getRouter()->getCurrentRoute()
is invoked.