Skip to content

cantSeeAuthentication()/dontSeeAuthentication() does not work due to missing service #138

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
W0rma opened this issue Jun 16, 2021 · 4 comments

Comments

@W0rma
Copy link
Contributor

W0rma commented Jun 16, 2021

I'm using symfony 5.3 and the new authenticator-based security (https://symfony.com/doc/current/security/authenticator_manager.html)

Calling cantSeeAuthentication()/dontSeeAuthentication() fails with the error message

Service security.helper is not available in container.
If the service isn't injected anywhere in your app, you need to set it to `public` in your `config/services_test.php`/`.yaml`,
see https://symfony.com/doc/current/testing.html#accessing-the-container

From what I can see the service security.helper is grabbed directly from the container here: https://github.com/Codeception/module-symfony/blob/main/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php#L180

Can anybody confirm this issue?

@TavoNiievez
Copy link
Member

TavoNiievez commented Jun 16, 2021

We have a test project (codeception/symfony-module-tests) that has tests with this module.

I updated this project to Symfony 5.3 using the new security system and according to our tests both methods are working correctly.

If the service isn't injected anywhere in your app, you need to set it to public in your config/services_test.php/.yaml

The error message gives you a little clue as to what the problem may be.

In the case of our test project, we do not have the security service defined as public in a test configuration file services_test. *, In our case, that service is injected into our SecurityAuthenticator class of the new Symfony security system, although actually that service is not used anywhere in that class.

@W0rma
Copy link
Contributor Author

W0rma commented Jun 17, 2021

Thank you for the quick reply.

So a workaround would be to inject the service security.helper somewhere in my app in order to avoid being removed during compilation of the DI container.

I wonder if there is a way to ensure that the service is always available when using the symfony module.

@TavoNiievez
Copy link
Member

I just made the changes in symfony-module-tests:

https://github.com/Codeception/symfony-module-tests/pull/19/files

Instead of doing a ghost injection of the security service in the app I defined the alias 'security.helper' as public in the test environment thanks to the services_test.php/.yaml file.

I wonder if there is a way to ensure that the service is always available when using the symfony module.

Regarding this question... I think that, even if there is a way to define that specific service as public in the test environment programmatically we probably should not do it, since each user should be able to decide exactly which services are public in their tests and which not.

Fail Service security.helper is not available in container.
            If the service isn't injected anywhere in your app, you need to set it to `public` in your `config/services_test.php`/`.yaml`,
            see https://symfony.com/doc/current/testing.html#accessing-the-container

So the solution is to improve the error message a bit more so that each user can define the alias for that service himself.

I'll close this issue when I have the PR by modifying that documentation.

@TavoNiievez
Copy link
Member

PR #142, #144 and #145 add the documentation that was required to close this issue.

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

2 participants