Problem/Motivation
Process isolation in PHPUnit 10 must be specified on a single class level or as a global switch.
This causes issues because Drupal needs to run Unit tests NOT in isolation for performance reasons.
Proposed resolution
In this issue, explore adding #[RunTestsInSeparateProcesses] attribute to all Kernel and Functional tests, through an automated Rector script.
How to update the MR
- from a git branch of the MR
- reset the branch to current 11.x:
git reset 11.x --hard - install rector:
composer require --dev rector/rector - revert composer changes:
git checkout -- . - download the rector config file attached here as
rector.phpin the root directory, and git ignore it - run rector:
vendor/bin/rector - run PHPCBF to reorder use import statements:
vendor/bin/phpcbf --filter=GitModified --standard=Drupal --sniffs=SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses,PSR2.Namespaces.UseDeclaration,PSR2.Namespaces.NamespaceDeclaration,Drupal.Classes.UnusedUseStatement core - commit changes to branch
- force push commit
With over 3k files touched, we should try to avoid making manual changes - rather tune the rector script.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3445240
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:
Comments
Comment #3
mondrakeAttached file is the Rector config file (rector.php) that conveniently has a Rule class to do what is in the MR.
Comment #4
mondrakeSo, chicken and egg - we can't do this until we have PHPUnit 10 in place, otherwise PHPStan complains about the fact that the attribute class does not exist.
Comment #5
andypostI bet it will affect split of tests for running in parallel to optimize pipeline
Comment #6
andypostComment #7
mondrakeComment #8
mondrakeComment #9
mondrakeComment #10
mondrakeUploading latest version of the rector script
Comment #11
mondrakeComment #12
mondrakeOnly manual changes:
Comment #13
mondrakeAs noted over in #3417066-120: Upgrade PHPUnit to 10, drop Symfony PHPUnit-bridge dependency, we can't mix PHPUnit attributes and annotations, we will have to convert them all at once.
Comment #15
mondrakeI'm afraid we have to reopen this in light of PHPUnit 12 - that is making
finalthe constructor ofTestCase. We can no longer override it to set programmatically at the base class level.The issue was reported upstream but no alternatives were suggested: https://github.com/issues/created?issue=sebastianbergmann%7Cphpunit%7C5838
So I think we need to
Comment #16
mondrakeWe need to convert all tests from annotation to attributes, first.
Comment #18
mondrakeComment #19
mondrakeComment #20
jibranComment #23
mondrakeFiled #3546029: Ensure that #[RunTestsInSeparateProcesses] attribute is added to all Kernel tests that imho should happen first.
Comment #25
mondrakeComment #26
mondrakeComment #27
mondrakeAll children are in, I think we can mark this fixed too - leaving to RTBC for any input
Comment #28
catchI think we're clear here, shame we had to do it, but glad that it is done!
Comment #31
m.stentaIs there a PHPStan rule that can be used by downstream projects to ensure that tests always have the
#[RunTestsInSeparateProcesses]?I tried adapting the Rector code from #10, but it didn't work.
Still wrapping my head around the consequences of not including
#[RunTestsInSeparateProcesses]... will tests break in PHPUnit 12? Or will they still run, but not in a separate process, and that might go unnoticed?If the tests will outright break, then maybe a PHPStan rule isn't needed. I just want to make sure we don't miss something.
Comment #32
mondrakeYes, the
Drupal\PHPStan\Rules\TestClassClassMetadatarule exactly implements that check.Not including the attribute in PHPUnit 12 will mean the test will be run in the same runtime space of the PHPUnit framework; as long as only one test is executed, it will likely not have problems, but as soon as multiple tests are executed in the same testrun, Drupal global settings of each test method executed will leak into the next test and there will be fun.
Comment #33
m.stentaThank you for the quick answer @mondrake!
Sorry if this is off-topic, but is there guidance anywhere for using core PHPStan rules in contrib projects? I'll try to dig into understanding how the core GitLab runner works... but any pointers would be appreciated!
Comment #34
mondrake#33 not that I am aware of. You may want to ask on Slack in #gitlab? Or file a policy issue even here in core. Good question!