Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
smpn2 opened this issue Mar 19, 2025 · 2 comments
Closed

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

smpn2 opened this issue Mar 19, 2025 · 2 comments
Labels
type/bug Something is broken

Comments

@smpn2
Copy link

smpn2 commented Mar 19, 2025

Q A
PHPUnit version 12.0.9
PHP version 8.4.4
Installation Method Composer

Summary

ref #6154

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

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

phpstan report

> composer analyze
------ ---------------------------------------------------------------------------------------------------- 
  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
 ------ ----------------------------------------------------------------------------------------------------

Current behavior

Fail static analysis

How to reproduce

https://github.com/smpn2/phpunit-6156

Expected behavior

@sebastianbergmann
Copy link
Owner

PHPUnit 12.0.9 no longer has a class named PHPUnit\Framework\MockObject\Builder\InvocationMocker. This class was an implementation detail which unfortunately was not treated as such in earlier versions. However, test code does not need to be adapted to change. This is why I was comfortable making this change in a non-major version (it should have been made in PHPUnit 12.0.0 but somehow I forgot, sorry) and do not consider it a breaking change.

PHPStan 2.1.8 by itself does not report an error for the code you provided. It is only when the rules from the phpstan-phpunit extension are active that an error is reported. A quick search in the source of phpstan-phpunit shows that it contains the string PHPUnit\Framework\MockObject\Builder\InvocationMocker and therefore needs to be updated for PHPUnit 12.0.9.

@ondrejmirtes @staabm Is this comment sufficient or should I open a ticket for phpstan-phpunit? I am sorry to cause this trouble :-(

@VincentLanglet
Copy link

@ondrejmirtes @staabm Is this comment sufficient or should I open a ticket for phpstan-phpunit? I am sorry to cause this trouble :-(

I created phpstan/phpstan-phpunit#222 to track this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants