phpstan-mockery icon indicating copy to clipboard operation
phpstan-mockery copied to clipboard

Call to an undefined method Mockery\Expectation::shouldReceive() after chaining onto `andReturn`

Open mr-feek opened this issue 6 years ago • 5 comments
trafficstars

similar to https://github.com/phpstan/phpstan-mockery/issues/3

I am still experiencing the following code as failing on 0.11.2

$mock = Mockery::mock(Something::class);
$mock
    ->shouldReceive('foo')
    ->andReturn(null)
    ->shouldReceive('bar');

Currently, the second shouldReceive call will trigger this error: Call to an undefined method Mockery\Expectation::shouldReceive().

mr-feek avatar Aug 13 '19 20:08 mr-feek

I confirm the bug is still present as of phpstan 1.0.*

A temporary solution is to use an ignore comment to silence the error:

/** @phpstan-ignore-next-line */
$this->partialMock(User::class)
  ->shouldReceive('plan')
  ->andReturn(Plan::where('id', Plan::FREE)->firstOrFail());

khalyomede avatar Nov 08 '21 20:11 khalyomede

It seems this is failing because shouldReceive() has more than one possible return type, so this can be fixed by decoupling the chain and using an annotation:

$mock = Mockery::mock(Something::class);
/** @var \Mockery\Expectation $mock */
$mock->shouldReceive('foo');
$mock->andReturn(null);

StuartCowley avatar Dec 08 '21 19:12 StuartCowley

This issue still exist

lchhieu avatar Jun 27 '23 05:06 lchhieu

I'm having this problem :(

davisenra avatar Nov 30 '23 23:11 davisenra