[FrameworkBundle] Add a "configure_container" option to WebTestCase::createClient() - #65997
Open
nicolas-grekas wants to merge 1 commit into
Open
[FrameworkBundle] Add a "configure_container" option to WebTestCase::createClient()#65997nicolas-grekas wants to merge 1 commit into
nicolas-grekas wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KernelBrowser::doRequest()reboots the kernel on every request after the first one. That includes crawler-driven requests:click(),submit()and redirect following all go through the same code path. So a service replaced withself::getContainer()->set(...)survives exactly one request and then silently vanishes: the test gets the real service back, with no error and no warning.This adds a
configure_containeroption toWebTestCase::createClient(), holding a closure that is applied to the container after every kernel boot, for the lifetime of the client:The closure receives the container returned by
KernelBrowser::getContainer(), which resolvestest.service_containerwhen available, so private services can be replaced. It runs on a freshly booted, untouched container, which is required sinceTestContainer::set()refuses to replace a private service that has already been initialized.Why
$optionsand not a new parameter oncreateClient(): overridingcreateClient()in a project base test case is a very common pattern, and PHP fatals when a child method declares fewer parameters than its parent. Adding a third argument would break every such override.KernelTestCase::createKernel()already documents$optionsas an extensible bag and reads only the keys it knows about, so this fits.createClient()extracts the key andunset()s it before booting, so a user-definedcreateKernel()never sees the closure.Notes:
disableReboot()there is no new boot, so the closure is not re-applied (the container simply keeps what was set);insulate()now throws a clearLogicExceptioninstead of failing obscurely;InvalidArgumentException;KernelBrowser::setContainerConfigurator()is public but@internalfor now, to keep the public surface minimal. It can be opened up later if re-registering mid-test turns out to be useful.The documentation is symfony/symfony-docs#22979. Note that
testing.rstcurrently recommends$container->set(...)under "Mocking Dependencies" with no mention of the reboot, so that PR also adds a warning there.Thanks to @lyrixx for reporting the issue, and to @nikophil and @benito103e who both volunteered to implement it and never got an answer.