Review main Merged

84386[TASK] Reduce GU::makeInstance calls in Unit Tests

Anja Leichsenring CR +2 V +2 Hard +115 −179 updated 06/04/24, 08:28

Commit message

Additional potential functionality (DI, XClass resolving, etc.) of
GU::makeInstance is usually not a wanted behaviour in unit tests.
For creation of system under test `new` should be used.
Dependencies, if necessary, should be mocks.
This patch reduces the obvious cases, more complicated ones will
be tackled with follow ups.

Review

  • Code review CR +2 Done
    • Benjamin Franzke +2
    • Oliver Klee +1
  • Verified V +2 Done
    • Benjamin Franzke +2
    • Oliver Klee +1

Issues 1

Files 30

Show 10 more files

Review log 2636

Show 611 earlier messages
  1. Anja Leichsenring uploaded patch set 1 05/24/24, 13:37

  2. core-ci voted on patch set 1 V +1 05/24/24, 13:44 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58164

  3. Anja Leichsenring uploaded patch set 2 V +1 05/24/24, 14:03

  4. core-ci voted on patch set 2 V -1 05/24/24, 14:09 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58165

  5. Anja Leichsenring uploaded patch set 3 V -1 05/24/24, 14:57

  6. core-ci voted on patch set 3 V +1 05/24/24, 15:02 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58166

  7. Anja Leichsenring uploaded patch set 4 V +1 05/24/24, 15:14

  8. core-ci voted on patch set 4 V +1 05/24/24, 15:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58167

  9. Anja Leichsenring uploaded patch set 5 V +1 05/24/24, 15:40

  10. Anja Leichsenring uploaded patch set 6 05/24/24, 15:42

  11. core-ci voted on patch set 5 V -1 05/24/24, 15:48 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58168

  1. core-ci voted on patch set 6 V -1 05/24/24, 15:48 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58169

  2. Anja Leichsenring rebased onto patch set 7 V -1 05/28/24, 05:09

  3. core-ci voted on patch set 7 V -1 05/28/24, 05:17 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58376

  4. Anja Leichsenring uploaded patch set 8 V -1 05/28/24, 06:49

  5. Anja Leichsenring updated the commit message in patch set 9 05/28/24, 06:50

  6. core-ci voted on patch set 8 V +1 05/28/24, 06:54 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58378

  7. core-ci voted on patch set 9 V +1 05/28/24, 06:56 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58379

  8. Benjamin Franzke commented on patch set 9 CR -1 05/28/24, 07:10

    WidgetGroupRegistryTest.php:88

    Can you explain why this method is removed? Rebase-mistake?
  9. Anja Leichsenring commented on patch set 9 05/28/24, 07:12

    WidgetGroupRegistryTest.php:88 Reply

    this test is rather useless as it tests the execution of Singleton Pattern implementation. We can rely on the framework to take care of the basic stuff.
  10. Benjamin Franzke commented on patch set 9 CR +1 V +1 05/28/24, 07:18

    WidgetGroupRegistryTest.php:88 Reply

    Acknowledged
  11. Oliver Klee voted on patch set 9 V +1 05/28/24, 08:48

  12. Oliver Klee commented on patch set 9 05/28/24, 08:52

    WidgetGroupRegistryTest.php:88 Reply

    I propose we replace the removed test with a test `isSingleton` that checks that the class is an instance of `SingletonInterface`.

    ContentObjectRendererTest.php:147

    Could we use a non-mocked instance here?
  13. Anja Leichsenring commented on patch set 9 05/28/24, 11:17

    WidgetGroupRegistryTest.php:88 Reply

    it's a core class and under control. A lot of things would break if it is no singleton, so why burden the unit tests with it?

    ContentObjectRendererTest.php:147 Reply

    why should we? It is a dependency, that does absolutely nothing but satisfying the surrounding code. No need to expand the system under test.
  14. Oliver Klee commented on patch set 9 05/28/24, 11:33

    WidgetGroupRegistryTest.php:88 Reply

    First of all, I don't consider additional tests a burden, but something of value. And I agree that probably other things would break if this class was no singleton. Still, in my experience having direct tests for a property/behavior of a class makes hunting down bugs easier (because then the test for the cause of the problem fails, not only other tests down the line).

    ContentObjectRendererTest.php:147 Reply

    I personally have gone from "mock all dependencies" to "only mock the dependencies where either it doesn't work with a mock, or where the dependency does not have much behavior" to keep things simpler while not creating new problems. (In my extensions, those are usually domain models that I don't mock, as opposed to services or repositories which I almost always mock in unit tests.) This would be consistent with other places in our unit tests where we use a direct `Context` instance of mocking it.
  15. Oliver Klee commented on patch set 9 06/01/24, 15:59

    Please note that I don't intent my comments as a blocker, but as suggestions to potentially improve this change, and (as with all review comments) as a chance to learn from each other.
  16. Benni Mack voted on patch set 9 CR +1 V +1 06/03/24, 20:46

  17. Benjamin Franzke commented on patch set 9 CR +2 V +2 06/04/24, 07:26

    WidgetGroupRegistryTest.php:88 Reply

    no, then we'd need to do that for every singleton.

    ContentObjectRendererTest.php:147 Reply

    out of scope.
  18. Christian Kuhn voted on patch set 9 CR +1 06/04/24, 07:44

  19. Benjamin Franzke commented on patch set 9 06/04/24, 07:57

    ContentObjectRendererTest.php:147 Reply

    to be fair: I think `new` would have been fine here as well, probably even better, yes. My comment was old (a draft) and I didn't fit here and I missed to recheck it before supplying the +2 vote. Anyway, I think we can keep using `createMock()` for now.
  20. Benjamin Franzke uploaded patch set 10 CR +1 V +1 06/04/24, 08:20

  21. Benjamin Franzke commented on patch set 10 06/04/24, 08:20

    ContentObjectRendererTest.php:147 Reply

    Changed to `new Context()`, I agree that's the better choice in this case as a mock is not needed/helpful.
  22. Oliver Klee voted on patch set 10 CR +1 V +1 06/04/24, 08:22

  23. Benjamin Franzke voted on patch set 10 CR +2 V +2 06/04/24, 08:25

  24. Benjamin Franzke merged patch set 11 06/04/24, 08:26

  25. core-ci voted on patch set 10 V +1 06/04/24, 08:28 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/58666