Active
Project:
Drupal core
Version:
main
Component:
phpunit
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
16 Sep 2026 at 09:33 UTC
Updated:
16 Sep 2026 at 15:01 UTC
Jump to comment: Most recent
When you do something in a test which causes a container rebuild, the test class still have a stale container in $this->container.
This is an easy trap for test developers to fall into.
But we could easily mitigate it by warning of an attempt to access a stale container.
Add a subclass or wrapper class of Container, to use in tests.
This adds:
protected bool $isStale;
public function setStale();
and wraps the call to get() with a check for whether it's stale, and throws an exception if it is.
Comments
Comment #2
berdircontainer is already a magic property that resolves to \Drupal::container() in functional tests, see \Drupal\Core\Test\TestSetupTrait::__get.
I thought we did that for kernel tests too, apparently not?
Comment #3
joachim commentedNo, it's set in Drupal\KernelTests\KernelTestBase::bootKernel():