Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
nreynis opened this issue Mar 18, 2025 · 8 comments
Closed

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

nreynis opened this issue Mar 18, 2025 · 8 comments
Assignees
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/12 Something affects PHPUnit 12

Comments

@nreynis
Copy link

nreynis commented Mar 18, 2025

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

@nreynis nreynis added the type/bug Something is broken label Mar 18, 2025
@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects version/12 Something affects PHPUnit 12 labels Mar 18, 2025
@sebastianbergmann
Copy link
Owner

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@sebastianbergmann sebastianbergmann added the status/waiting-for-feedback Waiting for feedback from original reporter label Mar 18, 2025
@nreynis
Copy link
Author

nreynis commented Mar 18, 2025

Sure here is a minimal example:
https://github.com/nreynis/phpunit-6154

@sebastianbergmann sebastianbergmann changed the title Method with on mocks yields error with static analyzer Method with() on test doubles yields error with static analyzer Mar 19, 2025
@sebastianbergmann sebastianbergmann removed the status/waiting-for-feedback Waiting for feedback from original reporter label Mar 19, 2025
@sebastianbergmann sebastianbergmann self-assigned this Mar 19, 2025
@nreynis
Copy link
Author

nreynis commented Mar 19, 2025

Thank you for the very quick response 💪

@sebastianbergmann
Copy link
Owner

You're welcome. Were you able to verify that the change solves your problem?

@nreynis
Copy link
Author

nreynis commented Mar 19, 2025

Yes, I've just checked commit c9fa5f5504ca03cb9565e9130cfc87bc74b44910 and this version does work.

@smpn2
Copy link

smpn2 commented Mar 19, 2025

@nreynis @sebastianbergmann Have you taken into consideration the following cases? I am having a different problem.

from https://github.com/nreynis/phpunit-6154

    public function testMockGet(): void
    {
        $mockedService = $this->createMock(MyService::class);

        $mockedService
+           ->expects($this->exactly(1))
            ->method('get')
            ->with(24)
            ->willReturn('24');

        $this->assertSame('24', $mockedService->get(24));
    }
------ ---------------------------------------------------------------------------------------------------- 
  Line   test\MyServiceTest.php
 ------ ----------------------------------------------------------------------------------------------------
  :16    Call to method method() on an unknown class PHPUnit\Framework\MockObject\Builder\InvocationMocker.
         🪪  class.notFound
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ ----------------------------------------------------------------------------------------------------

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Mar 19, 2025

I am having a different problem.

Please do not use an existing issue to discuss a new / different issue. If your issue still exists in PHPUnit 12.0.9 then please open a new issue. Thank you.

That being said, your issue might be related to PHPStan's cache (as PHPUnit 12.0.9 no longer has PHPUnit\Framework\MockObject\Builder\InvocationMocker). Delete PHPStan's cache and run again.

@smpn2
Copy link

smpn2 commented Mar 19, 2025

@sebastianbergmann I'm Sorry. I created new issue. (#6156)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/12 Something affects PHPUnit 12
Projects
None yet
Development

No branches or pull requests

3 participants