phpunit-documentation-english icon indicating copy to clipboard operation
phpunit-documentation-english copied to clipboard

Document that return values cannot be configured multiple times

Open sebastianbergmann opened this issue 7 years ago • 7 comments

Moved here from https://github.com/sebastianbergmann/phpunit-mock-objects/issues/260.

sebastianbergmann avatar May 12 '18 08:05 sebastianbergmann

Voting for this feature too

alexweb-zz avatar Jul 29 '19 09:07 alexweb-zz

I vote for this feature, thumb up!

jarenal avatar Sep 24 '19 11:09 jarenal

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.

bmarwell avatar Oct 11 '19 06:10 bmarwell

is there any progress on this issue?

nepster-web avatar Apr 11 '20 18:04 nepster-web

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:

  1. We always go through foreach, regardless of matches
  2. 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.

nepster-web avatar Apr 11 '20 19:04 nepster-web

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]);

fowlerwill avatar Sep 17 '21 22:09 fowlerwill

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.

fd-sturniolo avatar Mar 09 '22 22:03 fd-sturniolo

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.

evanlesmez avatar Jul 14 '23 20:07 evanlesmez