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

False-positive: PhpUnit mocks support #1053

Closed
kalessil opened this issue Nov 2, 2018 · 8 comments
Closed

False-positive: PhpUnit mocks support #1053

kalessil opened this issue Nov 2, 2018 · 8 comments

Comments

@kalessil
Copy link

kalessil commented Nov 2, 2018

Inside PhpUnit test cases, following code (simplified) is triggering ERROR: InvalidArgument - ... - Argument 1 of Service::__construct expects Clazz, PHPUnit\Framework\MockObject\MockObject provided

        $service = new Service(
            ...
            $this->createMock(Clazz::class),
            ...
        );

The psalm config generated for level 1. My expectation is that psalm is aware of PhpUnit mocks.

@weirdan
Copy link
Collaborator

weirdan commented Nov 2, 2018

This is better suited for a plugin, I believe.

@muglug , would it be possible to declare a generic like this:

/**
 * @template T
 * @template-typeof T $class
 * @param class-string $class
 * @return MockObject&T
 */
public function getMock($class) {}

? Last time I checked, intersection types did not accept type parameters.

@muglug muglug added the bug label Nov 2, 2018
@muglug
Copy link
Collaborator

muglug commented Nov 2, 2018

Oh, if not that's a bug - marking as such.

@muglug
Copy link
Collaborator

muglug commented Nov 2, 2018

That bug was fixed in f7a37d0

@kalessil for the moment we don't have a plugin system that supports stubs (and I don't want to support external packages natively), so you'll have to add the stub file yourself:

Get it here:
https://gist.github.com/muglug/45c8f47ded9bbc2fd53a065079bd6f6d

And update your config with

    <stubs>
        <file name="path/to/phpunit.php" />
    </stubs>

Also make sure path/to/phpunit.php is not analysed in your Psalm config.

I've just tagged a release that includes the fix above: https://github.com/vimeo/psalm/releases/tag/2.0.16

@kalessil
Copy link
Author

kalessil commented Nov 2, 2018

Awesome, thank you @muglug! I'll test it on Monday (the codebase is on my work machine, but I'm eager to test).

@kalessil
Copy link
Author

kalessil commented Nov 5, 2018

Checking. Meanwhile phive complains:

[WARNING]  psalm 2.0.16: No GPG Signature 
[WARNING]  psalm 2.0.15: No GPG Signature 
[WARNING]  psalm 2.0.14: No GPG Signature 
[WARNING]  psalm 2.0.13: No GPG Signature 
[WARNING]  psalm 2.0.12: No GPG Signature 
[WARNING]  psalm 2.0.11: No GPG Signature 
[WARNING]  psalm 2.0.10: No GPG Signature

@kalessil
Copy link
Author

kalessil commented Nov 5, 2018

@muglug: awesome work - works like a charm. Many thanks!

Now I need only possibility to get the release via phive and it goes into our pipeline!

@kalessil kalessil closed this as completed Nov 5, 2018
@muglug
Copy link
Collaborator

muglug commented Nov 27, 2018

Psalm V3 now supports PHPUnit mocks a bit better via a plugin: https://packagist.org/packages/psalm/plugin-phpunit

With Psalm 3, run

composer require --dev psalm/plugin-phpunit
vendor/bin/psalm-plugin enable psalm/plugin-phpunit

If you use mockery, that's also handled:

composer require --dev psalm/plugin-mockery
vendor/bin/psalm-plugin enable psalm/plugin-mockery

@kalessil
Copy link
Author

Awesome, thank you for an update @muglug. May I also gently push for re-enabling phive distribution (the signature file is missing, since v1.1.9 so I have to experiment with newer version while CI running on a outdated version).

SpazzMarticus added a commit to SpazzMarticus/TusServer that referenced this issue Mar 1, 2020
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

No branches or pull requests

3 participants