Skip to content

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

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

Closed
Stadly opened this issue Dec 10, 2018 · 5 comments
Closed

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

Stadly opened this issue Dec 10, 2018 · 5 comments

Comments

@Stadly
Copy link

Stadly commented Dec 10, 2018

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.
 ------ -------------------------------------------------------------------------
@k00ni
Copy link

k00ni commented Aug 5, 2020

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?

@ondrejmirtes
Copy link
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

@ondrejmirtes
Copy link
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.

@k00ni
Copy link

k00ni commented 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.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.

@github-actions
Copy link

github-actions bot commented 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants