Skip to content

Deprecate support for using with*() without expects() #6507

@sebastianbergmann

Description

@sebastianbergmann

Essentially, using with() without expects() is currently treated as if expects($this->any()) were used, meaning the above is equivalent to this:

$dependency = $this->createMock(MyInterface::class);
$dependency->expects($this->any())->method('myMethod')->with('payload')->willReturn('my result');

Let me quote what I wrote in #6461:

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".

I think that using with() without expects() should be deprecated in PHPUnit 13 and support for this anti-pattern should be removed in PHPUnit 14. This aligns with the deprecation of the any() matcher and its scheduled removal.

Originally posted by @sebastianbergmann in #6504

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions