Document that return values cannot be configured multiple times
Moved here from https://github.com/sebastianbergmann/phpunit-mock-objects/issues/260.
Voting for this feature too
I vote for this feature, thumb up!
Hi, just ran into this. At least update the docs that two consecutive calls to $mock->method('test') will have no effect. Only the first one will be used.
is there any progress on this issue?
I researched this question and see a problem is here:
https://github.com/sebastianbergmann/phpunit/blob/f9eb8a061d332e80f922554a1d1e512693069683/src/Framework/MockObject/InvocationHandler.php#L119
We go through all iterations of this foreach, however already use first match.
https://github.com/sebastianbergmann/phpunit/blob/f9eb8a061d332e80f922554a1d1e512693069683/src/Framework/MockObject/InvocationHandler.php#L124
I see 2 problems here:
- We always go through foreach, regardless of matches
- First match will be returned always
$this->request->method('getAttributes')->willReturn([1]);
$this->request->method('getAttributes')->willReturn([2]);
$this->request->method('getAttributes')->willReturn([3]);
$this->request->getAttributes() // return "1" it is first match
This is a big problem when we use builders for complicated mock objects. I propose to discuss the pull request for fix this.
I too like the method where you set up the stub's default 'good path' behaviour in setUp, then override it with new behaviour in a specific test as described here https://github.com/sebastianbergmann/phpunit-mock-objects/issues/260#issuecomment-264892662
Maybe it'd be possible to have a method such as:
$this->request->clearMethod('getAttributes')->method('getAttributes')->willReturn([2]);
I'd also be interested in a "clearMethod" method, sounds like a pretty backwards compatible way of doing this. I'm currently testing a class with multiple preconditions and just need to test code paths where a single one of them is different, so this would reduce setup code significantly.
Not sure where the willReturn limitation is documented because I ran into the same roadblock and don't see it in the latest willReturn section.