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.

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 library with --display-phpunit-notices --display-phpunit-deprecations.
  4. Observe the PHPUnit notices that occur.

Proposed resolution

Fix notices in the tests for the following core libraries, which currently only represents 10 affected classes.

  • core/tests/Drupal/Tests/Core/Database/
  • core/tests/Drupal/Tests/Core/Routing/

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3579890

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

The changes to MetadataBubblingUrlGeneratorTest were merged into this test because that class is a child of UrlGeneratorTest. They need to go in together.

dcam’s picture

Title: Convert expectation-less test mocks to stubs - batch 10 » Convert expectation-less test mocks to stubs - Database, Routing
dcam’s picture

FYI: the recently-committed UnitTestCase changes have been merged into this MR, so they do not need to be applied separately for testing.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Database = OK (160 tests, 194 assertions)
Routing = OK (115 tests, 358 assertions)

All instances of any() appear replaced. Only one I wasn't 100% sure on was core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php and the use of new functions. But based on other issues that have been merged seems inline.

LGTM

  • catch committed 0c80e081 on main
    task: #3579890 Convert expectation-less test mocks to stubs - Database,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

The new methods are because it's not possible to set up a mock in ::setUp() then only use it in a subset of tests, because when a test method doesn't use it, the expectations of the mock won't be met. So in those cases it has to be set up in a method. The other way would be to split the test into two classes, so the methods that don't need it get their own setUp() method with different mocks, but that's more churn.

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.

Status: Fixed » Closed (fixed)

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