Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Open
BluePsyduck wants to merge 2 commits into phpstan:1.1.x
base: 1.1.x
Choose a base branch
Loading
from BluePsyduck:fix-traits

Conversation

Copy link

@BluePsyduck BluePsyduck commented Sep 25, 2018

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()..

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"?

Copy link
Member

ondrejmirtes commented Sep 25, 2018 via email

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.
On 2018年9月25日 at 18:05, BluePsyduck ***@***.***> wrote: 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().. ------------------------------ You can view, comment on, or merge this pull request online at: #29 Commit Summary - Fixes failing tests in current master. - Fixes failing to recognize mock methods when using getMockForTrait(). File Changes - *M* src/Type/PHPUnit/MockBuilderDynamicReturnTypeExtension.php <https://github.com/phpstan/phpstan-phpunit/pull/29/files#diff-0> (1) - *M* tests/Rules/PHPUnit/AssertSameMethodDifferentTypesRuleTest.php <https://github.com/phpstan/phpstan-phpunit/pull/29/files#diff-1> (2) - *M* tests/Rules/PHPUnit/AssertSameStaticMethodDifferentTypesRuleTest.php <https://github.com/phpstan/phpstan-phpunit/pull/29/files#diff-2> (2) Patch Links: - https://github.com/phpstan/phpstan-phpunit/pull/29.patch - https://github.com/phpstan/phpstan-phpunit/pull/29.diff — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#29>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAGZuLY-0kYpcJw9GTl3foe0g70Jf31Tks5uelRUgaJpZM4W49LI> .
-- Ondřej Mirtes

Copy link
Member

ondrejmirtes commented Sep 25, 2018 via email

What’s the BC break?
On 2018年9月25日 at 18:20, BluePsyduck ***@***.***> wrote: 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"? — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#29 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAGZuCWv8bclkzcDOaKshjtvnkT0X6Poks5ueleugaJpZM4W49LI> .
-- Ondřej Mirtes

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
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /