prophecy: RFC: New unexpected method call behaviour

There will likely be errors in this, corrections/comments are welcome

inspired by this comment https://github.com/phpspec/prophecy/issues/527#issuecomment-802103309

Motivation

#441 changed the behaviour in a way that seemed sensible at the time, but has caused DX issues. To fix I seems to require a major version bump, so let’s take the chance to properly think through the behaviour and work out what’s best for the next major.

Historical context

Prophecy is an opinionated tool, and we have tried to be guided not by what makes testing easy, but by what pushes developers in the right direction.

Back in the dawn of time, in the PHP 5.3 days we did not have return types. It was decided that Prophecy would essentially have two modes:

  1. for objects that haven’t got any expectations, all method calls return null
  2. for objects with expectations, unexpected method calls throw an immediate exception

Spy problems

This caused issues when using a spy and also stubbing a method, cases like this would typically throw:

$stub->foo()->willReturn('bar');

$this->bar($stub);

$stub->baz()->shouldHaveBeenCalled();

This is why #441 was implemented, but this now means that the call stack for an unexpected message is messed up, and null returns then being passed on to other methods cause an error down the line before the unexpected call error would have been displayed.

Return type problems

Modern PHP is at the stage where return types can and should be added to nearly all methods. This makes the fallback behaviour far less useful as in most cases it’s a type error.

New Behaviour

In a new major version, Prophecy doubles will become strict by default.

That is: even if no expectations are set they will throw an Exception at call time for any unexpected method calls with one exception: void-returning methods.

This will allow the Spy behaviour for command-like methods, as long as the baz example returns void:

$stub->foo()->willReturn('bar');

$this->bar($stub);

$stub->baz()->shouldHaveBeenCalled();

This will disallow some previous Dummy-like use of doubles

BC Impacts

Tests that don’t specify return values for doubles will break unless those doubles only have void method calls.

PhpSpec and the PHPUnit-bridge depend on prophecy 1, so can choose when to expose this breaking change to their users.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (12 by maintainers)

Most upvoted comments

@TimoBakx that will be fine. All the ->will* methods are defining expectations, not only willReturn.

@DonCallisto you don’t assert that a stub returns something. You define it.

want to be sure that a call with other input has not hitten the stub

that’s already done automatically by the UnexpectedCallException thrown by Prophecy (except that today, we have the special case where nothing defined as expected calls means everything is accepted, which is scheduled for removal in this RFC).

This will disallow some previous Dummy-like use of doubles

if it is really a dummy that you need, you won’t have any issue though, as no calls would be made on it. And this could be prepared by triggered a deprecation warning for such cases in 1.x

Since almost all of my doubles already throw an exception without the ->willReturn(), this all good for me. As long as ->willThrow() will also not create any issues without a ->willReturn() present.

The deprecations would be a nice touch!

I agree on the rest of the RFC. I would advise to bump a major in phpunit-prophecy too, to avoid silent major bumps for end users. As for PHPUnit, they already deprecated the direct integration, so they could possibly ignore this and push to the trait further.