-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: New unexpected method call behaviour #528
Comments
if it is really a dummy that you need, you won't have any issue though, as no calls would be made on it. |
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. |
yeah, phpunit-prophecy would get a major version bump to v3 to opt-in for prophecy 2. That's the plan. |
I'm not sure that's a understandable behaviour from developer POV. I mean, why a method that returns a certain type will throw immediatly, whereas a method that returns |
@DonCallisto it can be used as a spy, but only if you also configured it to tell Prophecy what should be returned by the call |
Yeah, right.
|
Since almost all of my doubles already throw an exception without the |
@DonCallisto I don't think that would be necessary: $this->sutMethod($stub);
$stub->method(Argument::cetera())->shouldNotHaveBeenCalled(); This will be fine, if the method is called the test will fail because the stub will throw an Exception and the matcher is just information for a reader There are other cases where you might want to do both: $stub->save(Argument::any())->willReturn(null);
$this->savePrimes(2,3,4,5);
$stub->method(4)->shouldNotHaveBeenCalled(); |
The last case you mentioned is way too generic from my standpoint, because forces the user to be way too generic for the call, whereas I might want to assert that a stub will return something under certain input and want to be sure that a call with other input has not hitten the stub. Of course the |
@TimoBakx that will be fine. All the @DonCallisto you don't assert that a stub returns something. You define it.
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). |
@stof yes, sorry, my bad, I mean "define a canned method response" not assert, of course. |
Hi guys, |
FYI #526 already implements this and we're using that fork since April with no issues. |
Guys, any chance of something solving this? Why is this stalled? It is absurdly difficult to debug tests for methods with return values and expected calls when the expectation is not met - you get a return value exception rather than an unexpected call exception. |
There will likely be errors in this, corrections/comments are welcome
inspired by this comment #527 (comment)
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:
null
Spy problems
This caused issues when using a spy and also stubbing a method, cases like this would typically throw:
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 returnsvoid
: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.
The text was updated successfully, but these errors were encountered: