-
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
Callcenter returns null on non registered method call #472
Comments
I actually encountered this as well (and wanted to open an issue/pr about 😅 sorry). A possible solution (suggested in Slack): In the case of a |
Hello, Thanks for your reply and no need to be sorry. I opened the issue because I thought it was a problem but I indeed implemented a work around that replace the TypeError Exception with the Exception of But this needs to be implemented by everyone with this need (I think it might not be so many people) thus I raised the issue to know if it was an option to implement it directly in prophecy. If your suggestion is for an implementation in Prophecy I do not see right now how to do it in a clean way and the advantages over the options I provided. (I do not know the implementation of Prophecy very well so it may be why I cannot see it right now). |
If I understood correctly, what you are trying to fulfill is an interface / return-type, which can already be done using Prophecy ... use Prophecy\Argument;
$proph = $this->prophesize(Some::class);
$proph->THE_METHOD()->yourRules(); // ...
$proph->THE_METHOD()->withArguments([Argument::cetera()])->willReturn('some-correct-type-here'); This allows you to define a "default return" value (tested with version 1.10.3) so you can fulfill the return type there. // imagine a function with two arguments
...->withArguments([ Argument::any(), Argument::any() ])->...
// Score = 3 + 3 = 6
...->withArguments([ Argument::any(), Argument::cetera() ])->...
// Score = 3 + 2 = 5
...->withArguments([ Argument::cetera() ])->...
// Score = 2 Resolving which "prophecy-rules" matches will look for the biggest score and follow its definition / return value. And you need the https://github.com/phpspec/prophecy/blame/8ce87516be71aae9b956f81906aaf0338e0d8a2d/src/Prophecy/Argument/ArgumentsWildcard.php#L72 Even though the scoring of |
@ScreamingDev your solution is technically correct, but it's not feasible. The root of the issue is still the same: stubs and spies can have undefined behaviour, but that was defined in Prophecy long before PHP got return types. Now that we have them, returning Bottom line, Prophecy's behaviour is unclear when the subject of the prophecy has return types declared on its methods. We should intercept early when we would get a
IMHO we should bail out, because trying to guess a returnable value is very tricky:
...and you would still need to have a bail out as a fallback. Also, the DX issue would still be there, because the code under test could start to behave in unpredictable ways, possibly delaying the test failure, leading to different unexpected calls down the line and derailing the user. @ciaranmcnulty WDYT? |
even without return type in the mix, I still see an issue: @ciaranmcnulty to me, #441 was a mistake. It creates more issues that what it solves IMO. |
@stof I agree, the question is how to proceed without more disruption |
If we agree that reverting #441 is the way to go, it would certainly wreak havoc on users' test suites; many tests would start breaking just by changing that, and I'm not counting the fact that reverting #441 would force us to change a couple of other behaviours that were leveraging that. IMHO we should lump that into a 2.0 release, so that the upgrade wouldn't be automatic for end users, and mark this as a breaking change. |
no idea. the issue with spies is that they are configured in the MethodProphecy after using the double. So we have an issue here. |
@stof do you think that something along the lines of #509 would be a solution for spies? Basically, you would have to enable (or disable, depending on which default we want) the "bail out" on hitting a non registered method call, so that you could still define spies without having the double blow up on usage. |
Prophecy is 'opinionated' so I'd rather have a single behaviour, even if it loses some users. |
@ciaranmcnulty the current behavior is bad for anyone not using spies. Before #441, the |
I'm happy to go back to the world before #441, but what's the least disruptive way of doing that? |
The less disruptive way is a major version, so that projects have to opt-in for the behavior change. |
I drafted #526 to this effect. It seems a pretty straightforward change; I would just take the occasion to bake in other breaking changes if needed. |
See #528 for the discussion about the solution |
Hello,
First I do not often submit issues, so please accept my apologies if it is far from perfect.
This comment https://github.com/phpspec/prophecy/pull/441/files#r388191626 is perfectly true (as often with Stof) and thus creates an exception with an imprecise message.
Where I would like to see that there was no configured methodCall for these arguments with the list of provided arguments, I have a message about type mismatch.
This means to correct the error I have to look at the arguments with a debugger for instance.
I wonder if it is good to authorise a call that must return a value to fail later in case of
shouldHaveBeenCalled()
because it is not the job of prophecy to know/guess what to return.I think an option is to return the good type thanks to reflection. Another option would be to check if the type of return is not
void
and throw the exception immediately which would mean users could not expect ashouldHaveBeenCalled
to work for function with a return value without defining the correct return value themselves. There may be a lot of other better options but I only thought of these for now.Could you please tell me where I made mistakes in my understandings. And if I'm correct about the problem how should we proceed to make this better?
PS : @everzet I'm a regular user of Prophecy and I want to thank you for this piece of software.
The text was updated successfully, but these errors were encountered: