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
[FrameworkBundle] Allow setting private services with the test container #48938
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. |
This PR allows mocking a service for a test case. A typical example could be for HttpClient: