UnitTestCase::getContainerWithCacheTagsInvalidator() provides an easy, standardized way to set up a service container in unit tests that is pre-built for invalidating cache tags. The returned container is a PHPUnit MockObject upon which you can set expectations for whether it is utilized.
Previously, a default expectation of any() was set on the the container's get() method, meaning that the test did not care whether the method was used or not. But any() is deprecated in PHPUnit 12.5 and higher. The Drupal community is working to remove it from Core. In getContainerWithCacheTagsInvalidator() this expectation has been replaced by atLeastOnce(), which means that the container MUST be used in a way that get() is called for the cache_tags.invalidator service.
This has implications for large, complex test classes. In some cases the container may be created in the test's setUp() method, then be unused in many of the test's functions. Because the container must be used now, this pattern will no longer work. You must only set up the container when it is going to be used by a test.
Before:
class ExampleTest extends UnitTestCase {
protected function setUp()
\Drupal::setContainer($this->getContainerWithCacheTagsInvalidator());
}
public function testThatUsesContainer() {
\Drupal::getContainer()->get('cache_tags.invalidator');
}
public function testThatDoesntUseContainer() {
// Don't use the container.
}
}
After:
class ExampleTest extends UnitTestCase {
public function testThatUsesContainer() {
\Drupal::setContainer($this->getContainerWithCacheTagsInvalidator());
\Drupal::getContainer()->get('cache_tags.invalidator');
}
public function testThatDoesntUseContainer() {
// Don't use the container.
}
}