GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixes failing to recognize mock methods when using getMockForTrait() by BluePsyduck · Pull Request #29 · phpstan/phpstan-phpunit

Conversation

@BluePsyduck

@BluePsyduck BluePsyduck commented Sep 25, 2018

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
BluePsyduck added 2 commits September 25, 2018 17:38
@BluePsyduck
Fixes failing tests in current master.
3e23ca9
@BluePsyduck
Fixes failing to recognize mock methods when using getMockForTrait().
65e6eaa
@BluePsyduck

BluePsyduck commented Sep 25, 2018

Copy link
Copy Markdown
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"?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Sep 25, 2018 via email

Copy link
Copy Markdown
Member
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 Tue, 25 Sep 2018 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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Sep 25, 2018 via email

Copy link
Copy Markdown
Member
What’s the BC break?
On Tue, 25 Sep 2018 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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BluePsyduck

BluePsyduck commented Sep 25, 2018

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pavarnos pavarnos mentioned this pull request Jul 8, 2019
add getMockForTrait to dynamic return type #50
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@BluePsyduck @ondrejmirtes
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.