8000 Add tip about accessing removed services in the testing doc by mmenozzi · Pull Request #12647 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

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

Merged

Conversation

mmenozzi
Copy link
Contributor

As requested here this commit will add a tip about accessing removed services through the special test container in the testing docs.

@javiereguiluz
Copy link
Member

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

Copy link
@flackovic flackovic left a 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.

@mmenozzi mmenozzi force-pushed the testing-accessing-removed-services branch from a590d3d to 1d80249 Compare December 6, 2019 08:06
@mmenozzi
Copy link
Contributor Author
mmenozzi commented Dec 6, 2019

Hi @javiereguiluz and @flackovic, sorry for the delay.
I updated the PR giving insights about possible solutions!

Copy link
Contributor
@OskarStark OskarStark left a 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?

@mmenozzi
Copy link
Contributor Author
mmenozzi commented Dec 6, 2019

For the same reason this blog post has been written https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing

@OskarStark
Copy link
Contributor
OskarStark commented Dec 7, 2019

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?

@nicolas-grekas
Copy link
Member

A better way is to create a public alias, for tests only.
Note that I wouldn't use the word solution in the text: there is no problem to solve as Oskar hints (I don't have an example sorry)

@OskarStark
Copy link
Contributor

Thanks for the fast feedback Nicolas 👌🏻

@mmenozzi
Copy link
Contributor Author
mmenozzi commented Dec 7, 2019

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.

@weaverryan
Copy link
Member

I just added some more details - it will allow us to merge symfony/recipes#744

@javiereguiluz javiereguiluz added this to the 4.4 milestone Apr 3, 2020
@javiereguiluz javiereguiluz changed the base branch from master to 4.4 April 3, 2020 13:21
@javiereguiluz javiereguiluz force-pushed the testing-accessing-removed-services branch from 2f3514f to f038841 Compare April 3, 2020 13:21
@javiereguiluz javiereguiluz merged commit df4b2cb into symfony:4.4 Apr 3, 2020
@javiereguiluz
Copy link
Member

Manuele, thanks for your contribution. I'm sorry it took us so long to merge it. Congrats on your first Symfony Docs contribution!

@mmenozzi
Copy link
Contributor Author
mmenozzi commented Apr 3, 2020

Don’t worry Javier, thank you for merging!

javiereguiluz added a commit that referenced 8000 this pull request Apr 6, 2020
…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
@alexislefebvre
Copy link
Contributor
alexislefebvre commented Jan 11, 2021

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

javiereguiluz added a commit that referenced this pull request Jan 15, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0