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

Command icon 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

mondrake created an issue. See original summary.

mondrake’s picture

Title: Kernel tests sharing the environment stop working in PHPUnit 13 » Sharing the environment across kernel tests stops working in PHPUnit 13
mondrake’s picture

Issue tags: +PHPUnit 13
mondrake’s picture

Category: Task » Bug report
plach’s picture

Assigned: Unassigned » plach

I'll work on this ASAP.

plach’s picture

Assigned: plach » Unassigned
Issue summary: View changes
Status: Active » Needs review

Posted 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 RunTestsInSeparateProcesses attribute everywhere, relying on --parallel auto --recycle-workers-after 1 to eventually achieve per-class isolation.

mondrake’s picture

Yep 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.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

catch made their first commit to this issue’s fork.

catch’s picture

Status: Reviewed & tested by the community » Needs work

A few comments on the MR, mainly comment suggestions but found some very hard to read.

plach’s picture

Status: Needs work » Reviewed & tested by the community

Reworked comments as suggested

plach’s picture

Issue summary: View changes
catch’s picture

Version: main » 12.0.x-dev
Status: Reviewed & tested by the community » Fixed

OK 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed 6f0d8a53 on main
    fix: #3642569 Sharing the environment across kernel tests stops working...

  • catch committed a7ebb06b on 12.0.x
    fix: #3642569 Sharing the environment across kernel tests stops working...
mondrake’s picture

Thanks 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.

I'm slightly wary about PHPUnit extensions

Mind elaborating a bit on the concern?

catch’s picture

@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.

mondrake’s picture

Yep... 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.

plach’s picture

@catch

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.

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.