Skip to content

False positive in Tests: Service XY is private #27

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

Closed
Pierstoval opened this issue Dec 6, 2018 · 12 comments
Closed

False positive in Tests: Service XY is private #27

Pierstoval opened this issue Dec 6, 2018 · 12 comments

Comments

@Pierstoval
Copy link
Contributor

Pierstoval commented Dec 6, 2018

When using the KernelTestCase and asking PhpStan to analyze a test, I have this error:

image

However, the static::$container that is in the KernelTestCase is supposed to be an instance of the TestContainer but is typed as ContainerInterface because it might happen that the test.container service can be absent from the container, therefore making static::$container be the full container (as of KernelTestCase here )

Here is the code that triggers the error: Pierstoval/AgateApps/.../RedeemerTest.php#L61-L79

If the test.container service exists in the container AND for this specific case, I think there shouldn't be any error.

@lookyman lookyman added the 0.10.3 label Dec 7, 2018
@karser
Copy link

karser commented Jan 2, 2019

I'm also getting these warnings, although since Symfony 4.1 tests allow fetching private services by default.
https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing

A workaround here is to declare a service public for the test environment:

#services_test.yaml

services:
    _defaults:
        public: true

    Voucher\Redeem\Redeemer: ~

@Pierstoval
Copy link
Contributor Author

As a workaround, I'm using the dev-master branch and added this in my config:

parameters:
    ignoreErrors:
        - message: '#^Service "[^"]+" is private.$#'
          path: %rootDir%/../../../tests/

I can't wait for v0.11 to release, since this feature is only available in 0.11+ (therefore dev-master).

@lookyman
Copy link
Collaborator

I plan to try to tackle these false positives in the near future. It's... difficult. Symfony container is not as straightforward as I imagined when I started working with it and writing this extension. 😊

@Pierstoval
Copy link
Contributor Author

By chance there are not that many errors with the container 👍

@lookyman lookyman removed the 0.10.3 label Oct 7, 2019
@Wirone
Copy link
Contributor

Wirone commented Sep 14, 2020

I was struggling with this too, but @Pierstoval's solution worked for me. Maybe it's not as clean as it should be, but resolves my CI issue :) Subscribing to this, so hopefully it can be replaced with proper solution on some point.

@mitelg
Copy link
Contributor

mitelg commented Oct 2, 2020

plus 1 😊
would be nice, if this could be fixed somehow

@Pierstoval's solution also worked for me in the meantime 👍

@jeroennoten
Copy link
Contributor

I created #210 to resolve this issue.

@ondrejmirtes
Copy link
Member

Fixed by: #210

Merged and released :)

@Pierstoval
Copy link
Contributor Author

By the way, the exact same will happen to KernelTestCase::getContainer() method, because using self::$container is now deprecated as of Symfony 5.3 😅

@ondrejmirtes
Copy link
Member

Feel free to send another PR :)

@Pierstoval
Copy link
Contributor Author

@ondrejmirtes Is that it? => #212

I have no knowledge on how phpstan works, I just took example on #210, I have no idea on whether it's right or not 😅

@github-actions
Copy link

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 Dec 31, 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

7 participants