-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Allow setting private services with the test container #48938
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
Conversation
Really looking forward this one! Thanks a lot! |
Awesome! |
8f771fc
to
a4cc4a0
Compare
Is this cool? Yes I think so. It'd make tests less brittle if you don't have to instantiate smth but can rely on auto wiring too. Just one thought: could it detect if a service which was set is then not used during a test and output a warning? That'd allow to catch errors where you configure stuff that's useless / dead code. Possibly would back fire tho and be more annoying than useful :) |
@Seldaek that'd be neat but I don't know how we could make that work. There is no infrastructure in the container to know when a service is injected into another one, and even less to know when such a service is actually used. On a case by case basis, it's doable of course: $client = new TraceableHttpClient(new MockHttpClient(...));
static::getContainer()->set('example.client', $client);
// do stuff
// do expectations using TraceableHttpClient's API |
This would also be super useful to mock the clock (I do it all the time, mocking in the config isn't enough to test all cases): static::getContainer()->set(ClockInterface::class, new MockClock('2022-11-23 00:00:00', 'UTC')); |
fd0d04c
to
99883aa
Compare
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.
Awesome!
...fony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php
Outdated
Show resolved
Hide resolved
99883aa
to
f38b1db
Compare
f38b1db
to
30cb03f
Compare
Thank you @nicolas-grekas. |
|
@Bilge you must do the injection before services are instantiated. |
And the special TestContainer allowing you to retrieve private services already has a restriction that this does not support inlined services. This injection has the same restriction. |
Does this also allow to set services that never existed in container in first place? If yes this is actually a pretty dangaerous feature to have, you can make all your tests pass but your production code will still explode. Could be good to add some check if it already exists in container - if not - then throw exception. Just noticed this functionality now. @nicolas-grekas Okay checked it - and yea, it allows to set anything in container, even if it does not exist before, sounds a bit dangerous to me, I think this could be improved by some configuration option and probably on next major version throw exception in case of trying to set service that didn't exist before. |
This PR allows mocking a service for a test case. A typical example could be for HttpClient: