Problem/Motivation

MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10. See: https://github.com/sebastianbergmann/phpunit/pull/3687. We would like to support for PHPUnit 10 in D9. Therefore the use of the method needs to be replaced.

Proposed resolution

Replace the use of ::setMethods() with the use of ::onlyMethods() and/or ::addMethods() instead.
The method ::onlyMethods() is only for the mocking of methods that are implemented by the class.
The method ::addMethods() is only for the mocking of methods that are NOT implemented by the class.

This issue only replaced the use of the method ::setMethods() in core. The method has no deprecation warning from PHPUnit itself. @mondrake has opened an issue for for missing a deprecation warning, see: https://github.com/sebastianbergmann/phpunit/issues/4775.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

Original report:

drupal-check -d aggregator/
113/113 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

------ -------------------------------------------------------------------------------------------
Line tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
------ -------------------------------------------------------------------------------------------
86 Call to deprecated method setMethods() of class PHPUnit\Framework\MockObject\MockBuilder:
https://github.com/sebastianbergmann/phpunit/pull/3687
------ -------------------------------------------------------------------------------------------

Issue fork drupal-3130606

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

kapilkumar0324 created an issue. See original summary.

kapilv’s picture

StatusFileSize
new944 bytes
kapilv’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 3130606-2.patch, failed testing. View results

longwave’s picture

We call setMethods() in many more places, so we should fix them all at once instead of just this one instance.

However, we don't even support PHPUnit 9 yet, and #3127141: Use PHPUnit 9 for PHP 7.4+, while keeping support for PHPUnit 8.4 in PHP 7.3 didn't seem to trigger this deprecation message either from what I can see.

shaktik’s picture

Status: Needs work » Needs review
StatusFileSize
new945 bytes

Hi @longwave,

can we fixed Component-based: as an example of an aggregator module, it's a bit difficult to solve all (Modules) in a single patch.

Rkumar’s picture

@shaktik If we are fixing any deprecation improvement then why not pick all at once. module by module will increase no of issues as well as efforts.

Rkumar’s picture

Component: aggregator.module » phpunit
shaktik’s picture

Assigned: Unassigned » shaktik
shaktik’s picture

StatusFileSize
new127.16 KB

Hi @Rkumar,

replace all deprecated method setMethods(), Kindly check

shaktik’s picture

Assigned: shaktik » Unassigned

Hi @Rkumar,

replace all deprecated method setMethods(), Kindly check

shaktik’s picture

Title: phpunit: 9.1 Call to deprecated method setMethods() of aggregator https://github.com/sebastianbergmann/phpunit/pull/3687 » phpunit: 9.1 Call to deprecated method setMethods() of all files https://github.com/sebastianbergmann/phpunit/pull/3687
shaktik’s picture

StatusFileSize
new125.08 KB
shaktik’s picture

StatusFileSize
new43.88 KB

Status: Needs review » Needs work
hardik_patel_12’s picture

Assigned: Unassigned » hardik_patel_12
hardik_patel_12’s picture

Assigned: hardik_patel_12 » Unassigned
pavnish’s picture

Assigned: Unassigned » pavnish
pavnish’s picture

Assigned: pavnish » Unassigned

Sorry .I am stuck in another task which will take more time so i am un-assign this issue .

shaktik’s picture

Status: Needs work » Needs review
StatusFileSize
new43.86 KB
new7.94 KB

fixing Test Case.

Status: Needs review » Needs work

The last submitted patch, 20: 3130606-20.patch, failed testing. View results

shaktik’s picture

StatusFileSize
new36.63 KB
new7.23 KB
shaktik’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 22: 3130606-22.patch, failed testing. View results

shaktik’s picture

Status: Needs work » Needs review
StatusFileSize
new6.8 KB
new33.07 KB

I tried my best and replaced almost all the instances but a few instances are still available which is not working after replacement, please check.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Title: phpunit: 9.1 Call to deprecated method setMethods() of all files https://github.com/sebastianbergmann/phpunit/pull/3687 » MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10
Issue summary: View changes
Issue tags: +Deprecated assertions, +PHPUnit 10
Parent issue: » #3217904: [meta] Support PHPUnit 10 in Drupal 11

Reparenting, tagging, updating IS

mondrake’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Needs reroll.

mondrake’s picture

Issue summary: View changes

mondrake’s picture

Issue tags: -Needs reroll

Changed to MR workflow and rerolled.

mondrake’s picture

Priority: Normal » Critical

Bumping to critical because as of PHPUnit 9.6 a Warning will be thrown and testing will fail if the conversion is not done or the Warning not silenced.

See milestone of https://github.com/sebastianbergmann/phpunit/issues/4775

mondrake’s picture

Status: Needs work » Needs review

Converted usage of ::setMethods() to either ::onlyMethods() or ::addMethods().

The usage of ::onlyMethods([]) smells like we do not need a mock, but I did not make changes, maybe follow-ups?

For instance, in ImageTest, this seems to work fine:

diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php
index 30c37e9669..b2d9636129 100644
--- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php
+++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php
@@ -210,10 +210,7 @@ public function testSave() {
       ->method('save')
       ->will($this->returnValue(TRUE));
 
-    $image = $this->getMockBuilder('Drupal\Core\Image\Image')
-      ->onlyMethods([])
-      ->setConstructorArgs([$toolkit, $this->image->getSource()])
-      ->getMock();
+    $image = new Image($toolkit, $this->image->getSource());
 
     $file_system = $this->prophesize(FileSystemInterface::class);
     $file_system->chmod($this->image->getSource())
@@ -255,10 +252,7 @@ public function testChmodFails() {
       ->method('save')
       ->will($this->returnValue(TRUE));
 
-    $image = $this->getMockBuilder('Drupal\Core\Image\Image')
-      ->onlyMethods([])
-      ->setConstructorArgs([$toolkit, $this->image->getSource()])
-      ->getMock();
+    $image = new Image($toolkit, $this->image->getSource());
 
     $file_system = $this->prophesize(FileSystemInterface::class);
     $file_system->chmod($this->image->getSource())
daffie’s picture

Status: Needs review » Needs work

Should there not be a deprecation message test for the method setMethods()?

mondrake’s picture

Status: Needs work » Needs review

#35 not in Drupal, no. This is a PHPUnit deprecation, they should deal with it. Only thing when we have a PHPUnit release that will throw a Warning here (likely, 9.6.0), we can intercept that Warning and convert it to a deprecation via Drupal\Tests\Traits\PhpUnitWarnings.

longwave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs followup

All the changes look correct to me and review points by @daffie were addressed, nothing to add from me.

Agree that onlyMethods([]) is a smell, in most cases it looks like we are mocking the object under test e.g. where ForumNodeBreadcrumbBuilderTest creates a mock ForumNodeBreadcrumbBuilder. I think we should be using the real object in most if not all of these cases, so tagged "needs followup" to investigate this.

daffie’s picture

Issue summary: View changes

All code changes look good to me.
I have updated the IS.
I have installed the patch and I could not find any more use of the method setMethods() in core.
We cannot do anything for a deprecation warning, as there it not one added for it by the PHPUnit project.
For me it is RTBC.

  • catch committed 1b66ab0 on 9.3.x
    Issue #3130606 by shaktik, mondrake, KapilV, longwave, daffie:...
catch’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!

mondrake’s picture

catch’s picture

Title: MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10 » [backport] MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10
Status: Fixed » Patch (to be ported)
Issue tags: +Needs reroll

Hadn't actually done the 9.2.x cherry-pick when I wrote #40, then got distracted, and this doesn't cherry-pick cleanly, so re-opening for backport.

mondrake’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Needs followup, -Needs reroll
StatusFileSize
new112.61 KB

Rerolled for 9.2

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Visually compared with diff -u <(curl https://git.drupalcode.org/project/drupal/-/merge_requests/1188.diff) <(curl https://www.drupal.org/files/issues/2021-09-15/3130606-44.patch), only fuzz in a couple of tests that meant it wouldn't apply directly.

catch’s picture

Title: [backport] MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10 » MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10
Status: Reviewed & tested by the community » Fixed

Committed fbb8e86 and pushed to 9.2.x. Thanks!

  • catch committed fbb8e86 on 9.2.x
    Issue #3130606 by shaktik, mondrake, KapilV, longwave, daffie:...
catch’s picture

Status: Reviewed & tested by the community » Fixed
mondrake’s picture

Title: [backport] MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10 » MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10

Status: Fixed » Closed (fixed)

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

paulocs’s picture

Unfortunately this issue #3194768: The plugin system should only consider NULL context values to be empty to facilitate the use of otherwise FALSE values introduced the deprecated setMethods method. I'll open a new issue as the method was inserted after this issue was committed.

paulocs’s picture