Merge conflict Gerrit cannot merge this change. It needs a rebase onto main.

Review main Work in progress Orphaned

88839[WIP][TASK] Reset `GeneralUtility::$container` in unit tests

Stefan Bürk CR 0 V 0 Normal +63 −0 updated 04/24/25, 15:36

Commit message

Naturally unit tests operates not within a full TYPO3 instance
environment, which includes that dependency injection works and
is expected not to work.

A couple of tests sets a container instance with prepared services
using `GeneralUtility::setContainer()` to allow `makeInstance()`
calls to return service dependency, either real or mocked, when a
DI aware service is requested in tested code.

The `typo3/testing-framework` currently does not reset the container
instance property in `GeneralUtility`, which leads to pullation of
following tests and unforseable side-effects. Randomized test runs
reveals test pullation like this from time to time, and it happened
also here.

Using following commands (at least at the current state) reveales
a broken test and reveleaded pullated container from a previous
test leading to a difference in the test outcome:

```
Build/Scripts/runTests.sh -s composerInstallMax -p 8.4
&& Build/Scripts/runTests.sh -s unitRandom -p 8.4 -- \
    --random-order-seed 1743210354 --stop-on-error --stop-on-failure
```

This change resets the `GU::$container` instance within `tearDown()`
of tests using `GU::setContainer()` toset instances to mitigate the
pullation into following tests.

This should be handled by the `typo3/testing-framework` in some way,
either as a pure integration check letting a test fail or silently
resetting the container instance in `GeneralUtilitly`, which needs
to be discussed and decided.

Detecting all related tests has be done by adding following code to
the local `UnitTestCase::tearDown()` method:

```
$containerPropertyReflection = new \ReflectionProperty(GeneralUtility::class, 'container');
if ($containerPropertyReflection->getValue(null) !== null) {
  // Reset container instances set with `GeneralUtility::setContainer()`.
  $containerPropertyReflection->setValue(null , null);
  self::fail(
    'tearDown() integrity check deteected that GeneralUtility::setContainer() has been used and not'
    . ' cleaned up properly. Please check "' . static::class . "'."
  );
}
```

Test this change

Run a command in your clone of the TYPO3 Core. Then try what the change does and vote on Verified in Gerrit. How to review a patch

Check out patch set 1
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/39/88839/1 && git checkout FETCH_HEAD
Cherry-pick onto main
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/39/88839/1 && git cherry-pick FETCH_HEAD

Review

  • Code review CR 0 Needs a vote
  • Verified V 0 Needs a vote
    • core-ci +1 CI

Files 9

Review log 56

  1. Stefan Bürk uploaded patch set 1 03/30/25, 14:04

  2. Stefan Bürk wrote 03/30/25, 14:06

    Hashtag added: lolli-feedback
  3. core-ci voted on patch set 1 V +1 03/30/25, 14:12 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/76668

  4. Benjamin Franzke commented on patch set 1 03/30/25, 17:05

    Can we transform tests that need to set a container to functional tests? Another option would be to fix them to use DI and provide the services via injection… I think `GeneralUtility::setContainer()` should ideally be forbidden in Unit tests and should have never been created in unit tests… It is an internal method and is not meant to be used in tests (tests should not rely on internal API). Not because I want to annoy people, but simply because it's too easy to miss where a mocked dependency is "automatically" used. I don't want extension authors to copy this anti-pattern therefore I also don't think that we should provide cleanups in testing framework.
    Simple alternative: ``` git grep -r GeneralUtility::setContainer typo3/sysext/*/Tests/Unit ```
  5. Stefan Bürk commented on patch set 1 03/30/25, 20:50

    Reply

    > I don't want extension authors to copy this anti-pattern therefore I also don't think that we should provide cleanups in testing framework. Me neither. But the question is, if we should add it as integration test stating it / failing tests eventually with the message that this test should be converted to a functional. However, thanks for your input on this. The current test is not ment to be done in that way and was just to investigate the failure and see the current impact as base for discussions.

    Reply

    Yes, but again - the "code" also acted as base for discusson if we want to add that as integration test into the TF (done here manually), with a adjusted message, so extension/project developers are made aware of this fact (and recommend replacing the test with a functional test instead).
  6. Stefan Bürk marked it work in progress 04/24/25, 15:36