phpstan-phpunit
phpstan-phpunit copied to clipboard
Fixes failing to recognize mock methods when using getMockForTrait()
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()..
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"?
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 [email protected] 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:
https://github.com/phpstan/phpstan-phpunit/pull/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 https://github.com/phpstan/phpstan-phpunit/pull/29, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuLY-0kYpcJw9GTl3foe0g70Jf31Tks5uelRUgaJpZM4W49LI .
--
Ondřej Mirtes
What’s the BC break?
On Tue, 25 Sep 2018 at 18:20, BluePsyduck [email protected] 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 https://github.com/phpstan/phpstan-phpunit/pull/29#issuecomment-424406007, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuCWv8bclkzcDOaKshjtvnkT0X6Poks5ueleugaJpZM4W49LI .
--
Ondřej Mirtes
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.