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

[Feature Request] Report error when trying to configure a non existing method on MockObject. #71

Closed
VincentLanglet opened this issue Apr 20, 2020 · 6 comments

Comments

@VincentLanglet
Copy link
Contributor

I would be nice if phpstan could return an error for

$foo = $this->createMock(Foo::class);
$foo->method('nonExistingMethod');

$foo is know as Foo&MockObject so I think this would be possible.

@VincentLanglet
Copy link
Contributor Author

This would need a InvocationMockerType.
C.F. #48 (comment)

@ondrejmirtes I'll be happy to write the rule, I think I can (thanks to TDD). I have already wrote another rule. But I have more difficulties to see what I should I write in the InvocationMockerType and how to use it. Could you help me ?

@ondrejmirtes
Copy link
Member

$foo->method('nonExistingMethod') is called on MockObject&Foo in your case. So writing the rule is easy, you can start by registering a rule for MethodCall, see that $scope->getType($methodCall->var) is an IntersectionType of MockObject and the other type, get the other type and see if it ->hasMethod() that's in the string.

To also understand $foo->expects(...)->method('nonExistingMethod'), we'd have to probably make InvocationMocker generic (using a stub file) and write a dynamic return type extension so that ->expects on MockObject&T returns InvocationMocker<T>. We'd then be able to write a similar rule to the one described in the first paragraph.

@VincentLanglet
Copy link
Contributor Author

@ondrejmirtes stubs seems to not be used when I run tests.

I get

object(PHPStan\Type\ObjectType)#8114 (4) {
  ["className":"PHPStan\Type\ObjectType":private]=>
  string(39) "PHPUnit\Framework\MockObject\MockObject"
  ["subtractedType":"PHPStan\Type\ObjectType":private]=>
  NULL
  ["genericObjectType":"PHPStan\Type\ObjectType":private]=>
  NULL
  ["superTypes":"PHPStan\Type\ObjectType":private]=>
  array(0) {
  }
}

instead of an IntersectionType.

If I modify the phpDoc of the phpunit-vendor, I get an IntersectionType.

Do you know how to fix this ?

@ondrejmirtes
Copy link
Member

Can you open a draft PR so I can look at it more closely?

@VincentLanglet
Copy link
Contributor Author

Sure @ondrejmirtes : https://github.com/phpstan/phpstan-phpunit/pull/72/files

I expect, var_dump($type) to return an IntersectionType

@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

2 participants