Based on the evolution of PHPUnit's testing philosophy and the specific changes discussed here and here, $this->expects($this->any()) represents an anti-pattern in modern PHPUnit usage and should be avoided in new tests. The construct creates semantic ambiguity that conflicts with PHPUnit's current emphasis on intentional, clear testing practices.
The any() matcher indicates that a method may be called "zero or more times". When combined with expects(), this creates a fundamental contradiction: you're creating a mock object (designed to verify communication) but configuring it with an expectation that essentially says "I don't care if this communication happens at all". This ambiguity is precisely what PHPUnit 12.5's new diagnostic notices target.
When you use expects($this->any()), you're treating a mock object as a stub while paying the complexity cost of a mock.
There is no valid use for expects($this->any()) anymore. The construct represents a legacy pattern that PHPUnit's evolution has rendered obsolete. The framework now enforces a clear dichotomy:
- Test Stubs control indirect inputs via
createStub() and method()->willReturn(), for example
- Mock Objects verify indirect outputs via
createMock() and specific expects() matchers
Considering the above, the any() matcher should be soft-deprecated in PHPUnit 12.5, hard-deprecated in PHPUnit 13.0, and removed in PHPUnit 14.0.
Migration Path
- Identify the intent: Is the test verifying communication or just controlling data?
- Replace with createStub() if no communication verification is needed
- Use specific matchers (
once(), ...) if communication matters
Based on the evolution of PHPUnit's testing philosophy and the specific changes discussed here and here,
$this->expects($this->any())represents an anti-pattern in modern PHPUnit usage and should be avoided in new tests. The construct creates semantic ambiguity that conflicts with PHPUnit's current emphasis on intentional, clear testing practices.The
any()matcher indicates that a method may be called "zero or more times". When combined withexpects(), this creates a fundamental contradiction: you're creating a mock object (designed to verify communication) but configuring it with an expectation that essentially says "I don't care if this communication happens at all". This ambiguity is precisely what PHPUnit 12.5's new diagnostic notices target.When you use
expects($this->any()), you're treating a mock object as a stub while paying the complexity cost of a mock.There is no valid use for
expects($this->any())anymore. The construct represents a legacy pattern that PHPUnit's evolution has rendered obsolete. The framework now enforces a clear dichotomy:createStub()andmethod()->willReturn(), for examplecreateMock()and specificexpects()matchersConsidering the above, the
any()matcher should be soft-deprecated in PHPUnit 12.5, hard-deprecated in PHPUnit 13.0, and removed in PHPUnit 14.0.Migration Path
once(), ...) if communication matters