AI assisted in analysing this issue
Problem/Motivation
PHPUnit 13.5 no longer calls #[BeforeClass] and #[AfterClass] methods in the test runner process when all the tests of a class run in separate processes, so every test method of a class with #[ShareEnvironment] allocates a database of its own.
Proposed resolution
Introduce a PHPUnit extension allocating the database when the test class starts, passing its prefix to the test method processes in an environment variable, and dropping tables when the test class finishes.
Remaining tasks
- Validate the proposed solution
Post a MR- Review it
Introduced terminology
Shared environment allocator: a PHPUnit extension responsible for allocating a shared database prefix and managing the test environment database.
API changes
A new extension is introduced in the PHPUnit configuration:
<bootstrap class="Drupal\TestTools\Extension\SharedEnvironment\SharedEnvironmentAllocator"/>
Release notes snippet
Test environment sharing needs to be explicitly enabled in the PHPUnit configuration.
Issue fork drupal-3642569
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:
- 3642569-shared-environment-extension
changes, plain diff MR !18059
Comments
Comment #2
mondrakeComment #3
mondrakeComment #4
mondrakeComment #5
plachI'll work on this ASAP.
Comment #7
plachPosted a MR and updated the IS with the solution currently implemented.
The solution is compatible with a scenario where we adopt parallel test execution (shipped with PHPUnit 14) and drop the
RunTestsInSeparateProcessesattribute everywhere, relying on--parallel auto --recycle-workers-after 1to eventually achieve per-class isolation.Comment #8
mondrakeYep going the event way seems much more robust than the hooks.
It looks good to me, just one question inline - did not try myself: if we do not put the extension in phpunit.xml, what happens?
If we get this in D12.0, then we are unblocked on PHPUnit 13 too.
Comment #9
mondrakeComment #11
catchA few comments on the MR, mainly comment suggestions but found some very hard to read.
Comment #12
plachReworked comments as suggested
Comment #13
plachComment #14
catchOK so I'm slightly wary about PHPUnit extensions but generally PHPUnit gives us less and less choice on how to do things. The bright side is that this gets us closer to the point of being able to evaluate phpunit parallel against run-tests.sh, whether we can actually replace it and keep equivalent features is a different question but it'd be nice to find out.
Committed/pushed to main and 12.0.x, thanks!
I think we probably don't need this in 11.x because there's no way we can support PHPUnit 13.5 there, so marking fixed.
Comment #18
mondrakeThanks all for the fast turnaround here!
Apart from two failures due to Composer stability not accepting PHPUnit 13.5-dev, #3574681: Introduce support for PHPUnit 13.5 is all solved now. Unit, kernel, functional and functional JS tests all pass. It should (theoretically) go all green as soon as PHPUnit 13.5 is released on Dec 4.
Mind elaborating a bit on the concern?
Comment #19
catch@mondrake just that we have to keep them in sync in phpunit.xml which means people need to refresh their local copy every so often. Whereas if we were able to use only PHPUnit's PHP API there is nothing for individual developers to keep track of.
Comment #20
mondrakeYep... wish there could be a way to register extensions in the bootstrap phase through bootstrap.php...
But I think this is not in the radar, PHPUnit is now very declarative about extending its features. To an extent it's surprising it's possible to perform test discovery through PHP API.
Comment #21
plach@catch
Thanks!
I agree we don't need to worry about PHPUnit 13.5 in 11.x, however since 11.5 will be the lowest version where we support shared environment tests, I think it would be nice to recommend the same setup on every branch from the get go. I don't feel strongly about this, but personally I think backporting to 11.x and 11.5.x would be preferable.