-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add tip about accessing removed services in the testing doc #12647
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
Add tip about accessing removed services in the testing doc #12647
Conversation
I like this ... but in addition of explaining the problem, I think we should mention the possible solution. We did that in the new paragraph added at the end of the blog post that mentions this feature: https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @javiereguiluz already suggested, i think it would be helpful to hint possible solutions for this.
One is to make service public and other one is to inject service somewhere (controller?) just for the time being so it does not get removed.
a590d3d
to
1d80249
Compare
Hi @javiereguiluz and @flackovic, sorry for the delay. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you need such a service in a test env? Can you tell me a real world use case?
For the same reason this blog post has been written https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing |
Thank you for the blog post reference, I wasn’t aware of it 👍🏻😊 Anyway I couldn’t find a real world use case where I have a project and I wrote some tests for a Service which isn’t used in the same project 🤔 @nicolas-grekas do you have an example? |
A better way is to create a public alias, for tests only. |
Thanks for the fast feedback Nicolas 👌🏻 |
I do it very often. As TDD says I write a failing test before the implementation. In this test I get the service under test from the special test container but at that time the service isn’t used elsewhere in the application. Then, when the implementation is done and all tests are green I inject my new service where I need it. Anyway I think that it’s important that the docs says that a service is removed if not used in the application, because I wasted time on this before. The solution to this depends on the context I think. I never tried a test alias for example. |
I just added some more details - it will allow us to merge symfony/recipes#744 |
2f3514f
to
f038841
Compare
Manuele, thanks for your contribution. I'm sorry it took us so long to merge it. Congrats on your first Symfony Docs contribution! |
Don’t worry Javier, thank you for merging! |
…terj) This PR was merged into the master branch. Discussion ---------- No longer recommend injecting the service container Fixes #13469 I think it makes sense to no longer document injecting the service container. Service subscriber is the better alternative in any case. I actually also thought about removing setting a service public at all. But let's keep it for now (it is sometimes usefull in tests see e.g. symfony/symfony#36147 and #12647 . It's also a bit weird to have a "Public Versus Private Services" section that only discusses "private" services Commits ------- 156ac13 No longer recommend injecting the service container
This PR was merged but the content is not in the file anymore: https://github.com/symfony/symfony-docs/blob/4.4/testing.rst Was it removed later? Update: it was removed in #14071 |
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
As requested here this commit will add a tip about accessing removed services through the special test container in the testing docs.