Skip to content

Return intersection type from Generator::getMock() #32

Closed

Description

@Stadly

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 {}

function foo(): A
{
    $generator = new PHPUnit\Framework\MockObject\Generator();
    return $generator->getMock(A::class);
}
 ------ -------------------------------------------------------------------------
  Line   test.php
 ------ -------------------------------------------------------------------------
  5      Function foo() should return A but returns
         PHPUnit\Framework\MockObject\MockObject.
 ------ -------------------------------------------------------------------------

Activity

  1. k00ni commented on Aug 5, 2020

    @k00ni

    I have the same problem here. When using mocks in PHPUnit like

    $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();

    PHPStan complains like

    Parameter # 1 $entityManager of class App\Security\LoginFormAuthenticator constructor expects Doctrine\ORM\EntityManagerInterface, PHPUnit\Framework\MockObject\MockObject given.

    I am not sure how to tell PHPStan that getMock will in fact return an object of that type in this instance.

    In the documentation states:

    Interprets Foo|PHPUnit_Framework_MockObject_MockObject in phpDoc so that it results in an intersection type instead of a union type.

    Does it mean I have to add PHPUnit_Framework_MockObject_MockObject as viable type to my parameter lists so PHPStan wont complain?

  2. ondrejmirtes commented on Aug 5, 2020

    @ondrejmirtes
    Member

    @k00ni Just tried your problem and couldn't reproduce it. This works without a problem:

    <?php
    
    class Foo extends \PHPUnit\Framework\TestCase
    {
    
    	public function testBlabla(): void
    	{
    		$mock = $this->getMockBuilder(\stdClass::class)->disableOriginalConstructor()->getMock();
    		$this->doFoo($mock);
    	}
    
    	private function doFoo(\stdClass  $stdClass): void
    	{
    
    	}
    
    }

    No errors on max level.

    In order to help you, please state:

    • Your version of phpstan/phpstan, phpstan/phpstan-phpunit and phpunit/phpunit
    • Your phpstan.neon
  3. ondrejmirtes commented on Aug 5, 2020

    @ondrejmirtes
    Member

    @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.

  4. k00ni commented on Aug 5, 2020

    @k00ni

    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.

    Anyway, thank you for the feedback.

  5. github-actions commented on May 1, 2021

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions