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
------ -------------------------------------------------------------------------------------------
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | 3130606-44.patch | 112.61 KB | mondrake |
Issue fork drupal-3130606
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:
- 3130606-mockbuildersetmethods-is-deprecated
changes, plain diff MR !1188
Comments
Comment #2
kapilv commentedComment #3
kapilv commentedComment #5
longwaveWe 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.
Comment #6
shaktikHi @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.
Comment #7
Rkumar commented@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.
Comment #8
Rkumar commentedComment #9
shaktikComment #10
shaktikHi @Rkumar,
replace all deprecated method setMethods(), Kindly check
Comment #11
shaktikHi @Rkumar,
replace all deprecated method setMethods(), Kindly check
Comment #12
shaktikComment #13
shaktikComment #14
shaktikComment #16
hardik_patel_12 commentedComment #17
hardik_patel_12 commentedComment #18
pavnish commentedComment #19
pavnish commentedSorry .I am stuck in another task which will take more time so i am un-assign this issue .
Comment #20
shaktikfixing Test Case.
Comment #22
shaktikComment #23
shaktikComment #25
shaktikI tried my best and replaced almost all the instances but a few instances are still available which is not working after replacement, please check.
Comment #28
mondrakeReparenting, tagging, updating IS
Comment #29
mondrakeNeeds reroll.
Comment #30
mondrakeComment #32
mondrakeChanged to MR workflow and rerolled.
Comment #33
mondrakeBumping 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
Comment #34
mondrakeConverted 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:Comment #35
daffie commentedShould there not be a deprecation message test for the method
setMethods()?Comment #36
mondrake#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.
Comment #37
longwaveAll 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.Comment #38
daffie commentedAll 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.
Comment #40
catchCommitted/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!
Comment #41
mondrakeFiled #3232714: Replace, in tests, mocks that do not configure doubles with their actual objects as a follow-up.
Comment #42
catchHadn'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.
Comment #44
mondrakeRerolled for 9.2
Comment #45
longwaveVisually 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.Comment #46
catchCommitted fbb8e86 and pushed to 9.2.x. Thanks!
Comment #48
catchComment #49
mondrakeComment #51
paulocsUnfortunately 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.
Comment #52
paulocsThe new issue: #3250629: MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10