Skip to content

[PHPUnit90] Fix ReplaceAtMethodWithDesiredMatcherRector to treat at() as call index - #862

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-at-matcher
Dec 3, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-at-matcher

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

$this->at(N) matches the Nth call on the mock (0-based), not "N calls". The rule mapped at(0) to never(), at(1) to once() and at(N) to exactly(N), so a mock expected to be called became never() and passing tests started to fail.

Now the rule works on the test method and looks at all at() expectations of one mock together. It changes them only when:

  • the mock is a local variable,
  • every expectation on it is at(), with indexes 0..n-1 and no gap,
  • all expect the same ->method('...'), with only with() and willReturn() besides, used by all or none,
  • nothing else is stubbed on that mock in the method (a stubbed call shifts the indexes).

Then they collapse into one expectation:

-$mock->expects($this->at(0))->method('foo')->with(1)->willReturn('a');
-$mock->expects($this->at(1))->method('foo')->with(2)->willReturn('b');
+$mock->expects($this->exactly(2))->method('foo')->withConsecutive([1], [2])->willReturnOnConsecutiveCalls('a', 'b');

A lone at(0) becomes once(). Anything else is skipped. The old fixtures encoded the wrong mapping, so they are replaced with new ones covering the collapse and each skip case.

@TomasVotruba
TomasVotruba merged commit a4efff5 into main Dec 3, 2026
15 checks passed
@TomasVotruba
TomasVotruba deleted the fix-at-matcher branch December 3, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant