You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Return intersection type from Generator::getMock() #32
The easiest way to create mock objects outside of a test case is through PHPUnit\Framework\MockObject\Generator::getMock(). This method does not return intersection types, like PHPUnit\Framework\MockObject\MockBuilder::getMock() and PHPUnit\Framework\TestCase::createMock() does.
Example:
interface A {}
functionfoo(): A
{
$generator = newPHPUnit\Framework\MockObject\Generator();
return$generator->getMock(A::class);
}
------ -------------------------------------------------------------------------
Line test.php
------ -------------------------------------------------------------------------
5 Function foo() should return A but returns
PHPUnit\Framework\MockObject\MockObject.
------ -------------------------------------------------------------------------
@Stadly You can solve your problem by adding custom stub files https://phpstan.org/user-guide/stub-files to your project, or by contributing generic annotations to PHPUnit itself. I don't plan to address it in phpstan/phpstan-phpunit, because the class in PHPUnit is marked as @internal and shouldn't be really used outside of the project.
I wanted to provide a demo showing my problem, but after I set it up, I found out, that I actually ran PHPStan 0.11.x together with phpstan-phpunit 0.11.x. After upgrading to the latest 0.12.x such errors aren't mentioned anymore. I assume between these major versions this kind of "errors" got covered.
Activity
k00ni commentedon Aug 5, 2020
I have the same problem here. When using mocks in PHPUnit like
PHPStan complains like
I am not sure how to tell PHPStan that
getMockwill in fact return an object of that type in this instance.In the documentation states:
Does it mean I have to add
PHPUnit_Framework_MockObject_MockObjectas viable type to my parameter lists so PHPStan wont complain?ondrejmirtes commentedon Aug 5, 2020
@k00ni Just tried your problem and couldn't reproduce it. This works without a problem:
No errors on max level.
In order to help you, please state:
ondrejmirtes commentedon Aug 5, 2020
@Stadly You can solve your problem by adding custom stub files https://phpstan.org/user-guide/stub-files to your project, or by contributing generic annotations to PHPUnit itself. I don't plan to address it in phpstan/phpstan-phpunit, because the class in PHPUnit is marked as
@internaland shouldn't be really used outside of the project.k00ni commentedon Aug 5, 2020
I wanted to provide a demo showing my problem, but after I set it up, I found out, that I actually ran PHPStan
0.11.xtogether with phpstan-phpunit0.11.x. After upgrading to the latest 0.12.x such errors aren't mentioned anymore. I assume between these major versions this kind of "errors" got covered.Anyway, thank you for the feedback.
github-actions commentedon May 1, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.