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/Ajax/
  • core/tests/Drupal/Tests/Core/Authentication/
  • core/tests/Drupal/Tests/Core/Block/
  • core/tests/Drupal/Tests/Core/Breadcrumb/
  • core/tests/Drupal/Tests/Core/Condition/
  • core/tests/Drupal/Tests/Core/Controller/
  • core/tests/Drupal/Tests/Core/Cron/
  • core/tests/Drupal/Tests/Core/DependencyInjection/

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3579854

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
smustgrave’s picture

Status: Needs review » Needs work

Ajax = 0
Authentication = 0
Block = still get 6, for example

Test Triggered PHPUnit Notice (Drupal\Tests\Core\Block\BlockManagerTest::testHandlePluginNotFound)
No expectations were configured for the mock object for Drupal\Core\StringTranslation\TranslationInterface. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Block\BlockManagerTest::testHandlePluginNotFound)
No expectations were configured for the mock object for Drupal\Core\StringTranslation\TranslationInterface. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Post Condition Method Called (Drupal\Tests\Core\Block\BlockManagerTest::verifyProphecyDoubles)

Breadcrumb = 0
Condition = 0
Controller = still get 1

Mock Object Created (Symfony\Component\DependencyInjection\ContainerInterface)
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Controller\ControllerBaseTest::testGetConfig)
No expectations were configured for the mock object for Drupal\Core\Config\ImmutableConfig. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Controller\ControllerBaseTest::testGetConfig)
No expectations were configured for the mock object for Drupal\Core\Config\Config. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Controller\ControllerBaseTest::testGetConfig)
No expectations were configured for the mock object for Drupal\Core\Config\ImmutableConfig. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Controller\ControllerBaseTest::testGetConfig)
No expectations were configured for the mock object for Drupal\Core\Config\Config. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Test Triggered PHPUnit Notice (Drupal\Tests\Core\Controller\ControllerBaseTest::testGetConfig)
No expectations were configured for the mock object for Drupal\Core\Config\ConfigFactoryInterface. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check.
Post Condition Method Called (Drupal\Tests\Core\Controller\ControllerBaseTest::verifyProphecyDoubles)

Cron = 0
DependencyInjection = 0

Appears to just be the 2 mentioned components

dcam’s picture

Status: Needs work » Needs review

Thank you for pointing those out. I need to be more diligent about checking for notices covered by other MRs and making note of them.

As discussed in Slack, the remaining notices are caused by functions in UnitTestCase. They are covered by the changes in #3578904: Convert expectation-less test mocks to stubs - UnitTestCase. When I run the tests without the #3578904 changes I get the same notices. If I include the #3578904 changes, then I get no notices.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Applying that MR block and controller are all clear.

smustgrave’s picture

Title: Convert expectation-less test mocks to stubs - batch 4 » Convert expectation-less test mocks to stubs - Ajax, Authentication, Block, Breadcrumb, Condition, Controller, Cron, and DI
catch’s picture

Status: Reviewed & tested by the community » Needs work

This looks fine, the dummy expectations are slightly odd but no worse than what we have now, so I think it's fine to unblock phpunit 12.

Needs a rebase.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Rebased

catch’s picture

Status: Reviewed & tested by the community » Fixed

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 cc7870ba on main
    task: #3579854 Convert expectation-less test mocks to stubs - Ajax,...

Status: Fixed » Closed (fixed)

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