Why does the mock()->makePartial() function doesn't work in a fluent chain?

Viewed 53

Why does the mock()->makePartial() function need to be called on a separate line? What am I missing?

This works:

$mock = Mockery::mock()->makePartial();
$mock->shouldReceive(123);

This doesn't work:

$mock = Mockery::mock()->makePartial()
  ->shouldReceive(123);

phpunit returns an error for test_chained:

TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, class Mockery\Expectation does not have a method "setTime"

1 Answers

The two code snippets are not semantically equivalent. In the first one you assign a Mock to $mock (the return value of ::mock()) and in the second an Expectation (the return value of shouldReceive()). Therefore, when calling $mock->setTime() in the second case, you are not calling setTime() on the mock object, but you are trying to call it on the Expectation. (Please mind that this contains a lot of guesswork, since you seem not to have presented all of the relevant code parts.)

Related