-
Notifications
You must be signed in to change notification settings - Fork 48
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
Invalid "phpunit.mockMethod" error #227
Comments
Downgrading to phpstan-phpunit: 2.0.4 solves the issue. |
Please show code that causes this error. A full example that we can reproduce the problem with. |
I tried to extract this from our code base: use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
class SomeTest extends BaseTestCase {
protected MockObject|TypoScriptFrontendController $tsfe;
protected function setUp(): void
{
$this->tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
->onlyMethods(['addCacheTags', 'getLanguage'])
->disableOriginalConstructor()
->getMock();
$language = ....;
// this works
$this->tsfe->method('getLanguage')->willReturn($language);
}
#[Test]
public function sometest(): void
{
// this causes the error
$this->tsfe->expects(self::once())->method('addCacheTags');
} This is the mocked class: |
This: protected MockObject|TypoScriptFrontendController $tsfe; Should be an intersection type: protected MockObject&TypoScriptFrontendController $tsfe; Can you try that? |
I confirm that using the proper intersection type fixes it. |
Yes, fixes it. So we have two issues here now:
I have no problem with adjusting the code-base to intersection types, but those who need to support PHP < 8.1 will. |
Your code has always been wrong because it's been reporting this error even on 2.0.4 and prior:
That said, I managed to improve the extension not to report "method X does not exist on Y": 0aef32f
Intersection types work in PHPDocs too. In fact, it was where they originated: https://phpstan.org/blog/union-types-vs-intersection-types |
Argss... I forgot to disable the old baseline for the test. Sorry.
🤝👍 Thanks for the very fast support here. Highly appreciated. |
We are getting:
PHP: 8.4
phpunit: 10.5.45
phpstan: 2.1.11
phpstan-phpunit: 2.0.5
It looks like #222 is related to this.
The text was updated successfully, but these errors were encountered: