-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Improvements on withConsecutive with return #4255
Comments
@sebastianbergmann while I was doing some research on how to implement this I found the flowing lines of code.
I'm almost sure that this part is not directly on top of your mind. Since the last changes were just style changes. Nothing has been changed here since 2006. I would not remember why I wrote that this way. But what this part of the
Both of the asserts above will fail. because the loop in
Everyone searching for this will find out that But now I'm not sure what I should do ATM, so I would like to have some advice from someone more familiar with this part of PHPUnit. From my point of view, I think we should collect matchers for the same method into a single aggregate that can verify the parameters of each call. if none of them is matching an error will be reported with the expected calls. Does that sound ok? |
I did not write that code to begin with, so even back in 2006 I was not familiar with it :-/ No, it does not make sense the way it is right now. Changing it to make sense very likely breaks backward compatibility. So I think we should deprecate |
This patch will make a mock throw an exception when multiple matchers can be applied to a invoke. When allowing this, results of tests are not predictable. refs sebastianbergmann#4255
This patch will make a mock throw an exception when multiple matchers can be applied to a invoke. When allowing this, results of tests are not predictable. refs sebastianbergmann#4255
This patch will make a mock throw an exception when multiple matchers can be applied to a invoke. When allowing this, results of tests are not predictable. refs sebastianbergmann#4255
This patch will make a mock throw an exception when multiple matchers can be applied to a invoke. When allowing this, results of tests are not predictable. refs #4255
@jaapio Let me know if you need help implementing this. I'd be happy to lend a hand. |
I have way too much on my plate with other side-projects. So I won't be able to work on this. If anyone else is able to work on this feel free to provide a PR. If you want to discuss something with me about my suggestion, feel free to contact me at any time. See you around. |
@sebastianbergmann I think this issue can be closed based on #4026 (comment) and #4564 |
As a developer using the build-in mocks of PHPUnit can be complicated. One of the things I do have to look up aways is the way to have
withConsecutive
mocks. On the other hand, when using thewillReturnOnConsecutiveCalls
in combinationwillReturnConsecutive
is a real issue.Stubs do have a somewhat better method called
willReturnMap
but that one is using a less complicated check to match the arguments.In an example where we have something like this:
The problem here is that there is a relation between
willReturnOnConsecutiveCalls
andwithConsecutive
but it is not really clear to everyone that this relation is here. Especially when the number of checks done in thewithConsecutive
arrays is growing, it gets unreadable. because the lines of argument matchers are not fitting into your screen anymore.Using
willReturnMap
is not an option in this case, because the subject under test has some internal complexity that needs to be tested. And the arguments ofdoRequest
are objects so checking needs a callback.In the past, I created a manual test double that was implementing a certain interface and just stores the calls, like some kind of spy object. But in the end, it would be a nice feature to have this in the mocks so those extra classes are not required anymore.
What I would like to propose is a combination of
willReturnMap
andwithConsecutive
andwillReturnOnConsecutiveCalls
or maybe an extension ofwillReturnMap
that will allow a developer to addConstraints
into the return map. I'm a bit in doubt if this will work properly since the new method will mix the concepts ofparameterMatchers
andstubs
for the return types. Which I dislike inwillReturnMap
Another approach would be more like a builder type of implementation. So we could use the existing calls to create a mock.
This doesn't work currently because the matcher will throw an error. A more suitable solution would be something like the example below. The order of calls to
with
andwillReturn
would set the expected order of calls just likewithConsecutive
andwillReturnOnConsecutiveCalls
I think this will make it easier for new developers to start using the mocks.
If you agree with me that the suggestion above should be implemented I will try to get a PR ready.
The text was updated successfully, but these errors were encountered: