Problem/Motivation
This piece of code in KernelTestBase::tearDown()
// Free up memory: Custom test class properties.
// Note: Private properties cannot be cleaned up.
$rc = new \ReflectionClass(__CLASS__);
$blacklist = [];
foreach ($rc->getProperties() as $property) {
$blacklist[$property->name] = $property->getDeclaringClass()->name;
}
$rc = new \ReflectionClass($this);
foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
if (!$property->isStatic() && !isset($blacklist[$property->name])) {
$this->{$property->name} = NULL;
}
}
Prevents to write tests with typehinted properties in PHP 7.4+ - in the sense that yes, you can typehint but always have to allow null value too like e.g.
protected ?User $user;
which is not something we should encourage.
Also, since Kernel tests run in isolated processes in the PHPUnit era, while the cleanup intent is commendable, it is largely useless.
Proposed resolution
Remove that code and live happily.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3280882
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3280882-kerneltestbaseteardown-cleanup-prevents
changes, plain diff MR !2286
Comments
Comment #3
mondrakeComment #4
mondrakeComment #5
mallezieThe main reason for this code to me is to have memory reduction when running tests.
When indeed those tests are isolated, this does not have much use. Checked the originating issue where this was added #2304461: KernelTestBaseTNG™.
IMO if this was still an issue to have properties cleanup after the test to free up memory, this would also be needed in Browsertest where this would also occur. So IMO this is indeed safe to remove here.
Looking at the test run here, this does seem to run smooth with the cleanup code removed. Although i'm not experienced enough to see if removing this does indeed need more memory or not now.
Another option is to adjust this code to assign empty properties of the correct type instead of null. Which does not need to allow null values for the property on the test.
I use following code in a project (which uses drupal test traits).
Comment #6
mondrakeSee, also, #2880911: Remove unused KernelTestBase::getCompiledContainerBuilder()
Comment #7
mallezieThanks for referencing that issue. It explains a lot. In this case I can see no harm in removing this code.
Comment #8
alexpottThis was added in the initial implementation of KernelTestBase in #2304461: KernelTestBaseTNG™ tried to find out why. Haven't managed to yet. I agree that this looks largely pointless given that we run in isolation. +1 to removing.
Comment #9
alexpottEpic x-post with @mallezie's more in depth comment. Nice work @mondrake and @mallezie
Comment #10
alexpottCommitted and pushed e3983382da to 10.0.x and 4a392a17e7 to 9.5.x and 86819ab5e4 to 9.4.x. Thanks!
TBH we should be discouraging people from adding properties to tests - it's normally a source of interesting problems. Especially in anything extending WebDriverTestBase.