Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
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 |
flackovic left a comment
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.
Uh oh!
There was an error while loading.Please reload this page.
a590d3d
to1d80249
CompareHi@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 writtenhttps://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing |
OskarStark commentedDec 7, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 mergesymfony/recipes#744 |
2f3514f
tof038841
CompareManuele, 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 containerFixes#13469I 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" servicesCommits-------156ac13 No longer recommend injecting the service container
alexislefebvre commentedJan 11, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 servicesI 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 containerand 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-testingThis is also consistent with the error message from Symfony:https://github.com/symfony/symfony/blob/3ffe5573e9dd045e157c6f17358c700fceae3feb/src/Symfony/Component/DependencyInjection/Container.php#L275See also#12647 andsymfony/symfony#30104Commits-------e6a6c33 Add warning about removed services
As requestedhere this commit will add a tip about accessing removed services through the special test container in the testing docs.