Skip to content

Method with() on test doubles yields error with static analyzer #6154

@nreynis

Description

@nreynis
Q A
PHPUnit version 12
PHP version 8.3
Installation Method Composer

Summary

This piece of code works, but does not pass static analysis anymore.

$this->createMock(MyService::class)
    ->method('get')
    ->with($id)
    ->willReturn($value);

The test still run correctly but does not pass a PHPStan analysis anymore:

Call to an undefined method
         PHPUnit\Framework\MockObject\Builder\InvocationStubber::with().

In PHPUnit 11 method returned an InvocationMocker

I can workaround by using willReturnMap, but I haven't seen any deprecation, or anything in the release notes. The documentation still has examples with the same construction (ie: https://docs.phpunit.de/en/12.0/test-doubles.html#id34).

Should I change my code or is this an unwanted side-effect of refactoring?

Current behavior

Fail static analysis

How to reproduce

See minimal example: https://github.com/nreynis/phpunit-6154

Expected behavior

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions