-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Impossible to get private services from TestContainer #30104
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
Comments
What the error says is accurate. If you want to test an unused service, you need to create a public test alias for it: services:
test.CLASS:
alias: CLASS
public: true Closing as this is expected behavior, thus no bug to keep track of. |
Hi @nicolas-grekas, thank you for your quick response. Thanks, |
@lelledaniele The thing is, service is not used anywhere in the project, so "RemoveUnusedDefinitionsPass" removes it from container. I am not sure why does it behave like that in test container, since it is making TDD approach difficult. @nicolas-grekas are there any plans to change this behaviour in test container, like private / public was changed here ? |
Nope, no plans to do so, |
@nicolas-grekas I see. Thanks for clarifying. |
Thank you, both. |
I think that this should be documented here https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing This because is pretty common that someone uses a service for the first time only in an integration test which tests its behavior. |
@mmenozzi would you mind sending a PR to the doc repo? |
Hi @nicolas-grekas I'd be happy but I don't know where to submit the PR. Is that blog post on the https://github.com/symfony/symfony-docs repo? If so could you point me to the correct file? |
@mmenozzi that's correct! According to our supported versions, this change must be done in 4.3 branch (and we'll later merge it in 4.4 and master too). Specifically, I guess you need to update the section about testing + container in this file: https://github.com/symfony/symfony-docs/blob/4.3/testing.rst Thanks! |
Ok @javiereguiluz I'll do it asap. |
Hi guys, I did the PR symfony/symfony-docs#12647 but I also think that the blog post https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing should be updated as well. That blog post is well indexed on Google. If I search for "symfony access private service in test" that post is the first result. |
@mmenozzi good idea! I've just updated the blog post. |
…doc (mmenozzi, weaverryan) This PR was submitted for the master branch but it was merged into the 4.4 branch instead (closes #12647). Discussion ---------- Add tip about accessing removed services in the testing doc As requested [here](symfony/symfony#30104 (comment)) this commit will add a tip about accessing removed services through the special test container in the testing docs. Commits ------- f038841 clarifying how to create a public alias for a test 67dd3c1 Add tip about accessing removed services in the testing doc
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- Add warning about removed services I was confused by the fact that the documentation explains: > Finally, for the most rare edge-cases, Symfony includes a special container which provides access to all services, public and private. This special container is a service that can be get via the normal container and the fact that the special container doesn't give access to the removed service. So I added a tip in the documentation, taking inspiration from a Symfony article: > Keep in mind that, because of how Symfony's service container work, unused services are removed from the container. This means that if you have a private service not used by any other service, Symfony will remove it and you won't be able to get it as explained in this article. The solution is to define the service as public explicitly so Symfony doesn't remove it. Source: https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing This is also consistent with the error message from Symfony: https://github.com/symfony/symfony/blob/3ffe5573e9dd045e157c6f17358c700fceae3feb/src/Symfony/Component/DependencyInjection/Container.php#L275 See also #12647 and symfony/symfony#30104 Commits ------- e6a6c33 Add warning about removed services
Hi there,
Symfony version(s) affected: 4.2.3
Description
An old PR #26499 should allow us to use private services in
test
env as public.On my local, I am struggling to make it work. Post here
How to reproduce
./bin/phpunit --stop-on-failure
Additional context
File:
vendor/symfony/framework-bundle/Test/TestContainer
Method:
get
Many thanks for your time,
Lelle
The text was updated successfully, but these errors were encountered: