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

Fixes failing to recognize mock methods when using getMockForTrait() #29

Open
wants to merge 2 commits into
base: 1.1.x
Choose a base branch
from

Conversation

BluePsyduck
Copy link

This pull request fixes that PHPStan failed to recognize the mock methods when calling getMockForTrait() on the mock builder.

I also repaired the two failing tests of the current master.

Example:

// Context: Test method in a TestCase class.

$trait = $this->getMockBuilder(MyFancyTrait::class)
              ->setMethods(['myFancyMethod'])
              ->getMockForTrait();
$trait->expects($this->once())
      ->method('myFancyMethod')
      ->with($myFancyParameter);

This would always trigger Call to an undefined method PHPUnit\Framework\MockObject\MockBuilder<MyFancyTrait>::expects()..

@BluePsyduck
Copy link
Author

I noticed that it is currently not possible to have all tests passing on Travis because of a BC break in phpstan from 0.10.2 to 0.10.3.

Should I change the dependency in composer.json to "phpstan/phpstan": "^0.10.3"?

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 25, 2018 via email

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 25, 2018 via email

@BluePsyduck
Copy link
Author

Hi, this isn’t that easy to fix, trait is not a standalone type, your solution probably doesn’t work or it would break in some situation. Correct solution is to generate an intersection type of ObjectWithoutClassType and HasMethodType + HasPropertyType of all the traits methods and properties.

Okay, this surpasses my knowledge about how phpstan works internally so I won't be able to fix it that way myself. (I did a quick test, and for my case phpstan recognized both the mock methods and the trait methods.)

What’s the BC break?

The constructor of ImpossibleCheckTypeHelper changed (now expecting Broker as first parameter), which is used in the failing tests. It's only in the test though, not in the actual source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants