Closed chubidu closed 6 years ago
Thank you for your report.
Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.
Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.
Hi Sebastian,
so I've created a repository containing the example to reproduce the error I'm getting.
https://github.com/chubidu/phpunit-withConsecutive-failure
You just need to clone it, run composer install
and run the tests. I've also added a README.md
file which contains the error output and the steps to reproduce.
Please let me know if anything is missing.
Thanks in advance.
Sorry, but that is not minimal.
Then please tell me your definition of minimal.
This would be just 3 commands on cli. All relevant code is just located in ./src/Examples/
and ./tests/.../Example/
.
So, I removed everything from the repository which might be unnecessary.
@chubidu I'm not sure this is a problem from phpunit. Check this out:
$tmp = $this->repository->all(
new ParameterBag([
'limit' => $this->chunkSize,
'offset' => $this->chunkSize * $iteration,
])
);
this would work (every time you're creating a new instance/reference), but when you're doing this:
$tmp = $this->repository->all(
$this->getParameterBag([
'limit' => $this->chunkSize,
'offset' => $this->chunkSize * $iteration,
])
);
you're using the same instance/reference of ParameterBag
inside the do/while loop, meaning that if inside this all
method you would store it for some reason (and not use it immediately), in the second, third etc. iteration of your do/while loop you'll be changing the parameter bag values (and thus the same reference as well).
That's what's happening with the phpunit mock code, it stores the object you're sending to all
method, but that reference is being modified inside the do/while loop (the last iteration set parameter values to 10/10, that's why the expectation shows "Actual: 10".
You could clone your parameter bag (to have a different references sent to your repository), unless this is really something phpunit should do (clone the references when running the mock objects).
What do you think @sebastianbergmann ?
@vsouz4 Thanks for your answer. I will check that out and let you know.
@vsouz4 I adjusted the method getParameterBag
to simply return a new instance of ParameterBag
every time it gets called. This fixed my issue with withConsecutive
failing.
Thank you.
Verifying the arguments passed to a method call with
withConsecutive
fails. The mocked method is being called within a loop. The first iteration passes and the error occurs on the second iteration of the loop. The error I'm getting is telling me that it failed on the first iteration since it's sayingParameter 0 for invocation #0 ...
:Now, if I'm going to change the expected values for the second iteration (in
withConsecutive
) to something invalid, the Testrunner is telling me the truth and giving me the expected output, thatParameter 0 for invocation #1 ...
failed. As soon as the expected values are exactly like the actual ones (which you can verify dumping the params), the test fails with the error message mentioned above (Parameter 0 for invocation #0 ...
).Maybe I'm doing something wrong? Thanks for your time/help in advance.
Example to reproduce
This class is getting mocked in the test. It's accepting an instance of
ParameterBag
. This object will be verified in the test which fails.This is the class which is getting tested.
This is the test
Last but not least the output of
composer info | sort