Problem/Motivation

PHPUnit 12.5.0+ is changing the way that mock objects work:

  • Mock objects that do not configure expectations should be converted to stubs. Otherwise, they throw a notice: No expectations were configured for the mock object for \Drupal\comment\CommentManagerInterface. You should refactor your test code and use a test stub instead.
  • $this->any() is deprecated. It was equivalent to having no expectation, in which case the object should be a stub (see the previous bullet) or a more exact number of expectations should be added.
  • with() is deprecated for stubs because it was deemed to be equally pointless.

The UnitTestCase base class is responsible for a huge number of notices throughout Core's tests, 556 notices! The mock objects created by getStringTranslationStub() are probably the main offender. But there are other mock objects that never have expectations set in there.

Because of the far-reaching implications of changing one of the base test classes I've chosen to only focus on UnitTestCase and any modifications to child classes that have to be made to support conversions. Luckly, that seems to be a very small number of classes!

Steps to reproduce

  1. Require PHPUnit 12 with Composer.
  2. Fix compatibility issues with PHPUnit 12 by applying this patch.
  3. Run the Unit tests for a module with --display-phpunit-notices --display-phpunit-deprecations.
  4. Observe the PHPUnit notices that occur.

If you do a comparison of before and after test runs, for example using the command phpunit --display-phpunit-notices --display-phpunit-deprecations core/modules/*/tests/src/Unit core/tests/Drupal/Tests/, then you should ONLY see the number of PHPUnit notices go down. My results were:

Before:
Tests: 17671, Assertions: 89409, Errors: 22, Failures: 14, PHPUnit Deprecations: 13, PHPUnit Notices: 4099, Skipped: 5.

After:
Tests: 17671, Assertions: 89416, Errors: 22, Failures: 14, PHPUnit Deprecations: 13, PHPUnit Notices: 3543, Skipped: 5.

Proposed resolution

Fix notices caused by the UnitTestCase base class.

Remaining tasks

Information on how to review these issues and the types of changes you're likely to find in them is included in the parent issue's comments.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3578904

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

dcam created an issue. See original summary.

dcam’s picture

Status: Active » Needs review

I'm pretty sure that test failure is random.

Anyway, I can't believe that changing all of those config and string translation mocks doesn't cause dozens of Unit tests to blow up, but apparently it's safe to do.

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Landed here from #3569420: Convert expectation-less test mocks to stubs - Field modules with the MR used from that MR I still got a few notices and @dcam pointed out they would be fixed by this issue. Applying this addressed all the notices and the change here focusing on core/tests/Drupal/Tests/UnitTestCase.php makes sense. Talked to dcam on slack briefly too and he explained why the views change was needed (some reason the comment wasn't appearing on first page load).

catch’s picture

Do we want a change record like 'Unit tests may need to call ::getContainerWithCacheTagsInvalidator() in test methods instead of the constructor' or something? Although I would imagine at most one or two contrib unit tests will fail given the small number of changes in core. Otherwise looks good to me.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
dcam’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

CR is well detailed.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Yes CR is great. Committed/pushed to main, thanks!

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 f9b29d69 on main
    task: #3578904 Convert expectation-less test mocks to stubs -...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.