Opened 7 weeks ago
Last modified 7 weeks ago
#66212 new task (blessed)
Tests: Replace mock builder methods removed in PHPUnit 10 and 12
| Reported by: | lancewillett | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Build/Test Tools | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | tests |
Description
Split from #62004.
PHPUnit 10 removed setMethods() and withConsecutive(). PHPUnit 12 removed getMockForAbstractClass(). Trunk still uses them:
setMethods(): 23 calls in 10 files, inadmin/,filesystem/wpFilesystemDirect/,includes/,pomo/andrest-api/withConsecutive(): 7 calls inadmin/wpUpgrader.phpgetMockForAbstractClass(): 2 calls, inadmin/wpPrivacyRequestsTable.phpandwidgets/wpWidgetMedia.php
The replacements must keep working on PHPUnit 9. For setMethods(), himanshupathak95 wrote one approach in https://github.com/WordPress/wordpress-develop/pull/11844 (commit 7e451a013e): call onlyMethods() when it exists and fall back to setMethods().
Change History (1)
This ticket was mentioned in PR #13828 on WordPress/wordpress-develop by @irozum.
7 weeks ago
#1
- Keywords has-patch has-unit-tests added; needs-patch removed
Note:
See TracTickets
for help on using tickets.
Replaces three PHPUnit mock-builder methods that no longer exist in newer PHPUnit releases, so the test suite keeps working once the environment moves off PHPUnit 9. All replacements keep the exact same test behavior on the currently-installed PHPUnit 9.6.37.
setMethods()→onlyMethods()(removed in PHPUnit 10, 23 calls across 10 files) — a direct swap, sinceonlyMethods()has existed since PHPUnit 8.3 and behaves identically for the array-of-method-names usage found throughout this codebase. Also removed the now-stale "setMethods()is deprecated..." comments left on several of these call sites.withConsecutive()→willReturnCallback()(removed in PHPUnit 10, 7 calls intests/phpunit/tests/admin/wpUpgrader.php) — added a small private helper,get_consecutive_calls_callback(), that returns a closure asserting each successive call's arguments against an expected sequence (matching only as many leading arguments as were previously specified, mirroringwithConsecutive()'s own subset-matching behavior) and returning a fixed value. All 7 call sites now share this one helper instead of duplicating the logic.getMockForAbstractClass()→getMockBuilder()->onlyMethods()->getMock()(removed in PHPUnit 12, 2 calls intests/phpunit/tests/admin/wpPrivacyRequestsTable.phpandtests/phpunit/tests/widgets/wpWidgetMedia.php) —getMockForAbstractClass()only auto-stubs a class's abstract methods (any explicitly-listed$mockedMethodsaside), leaving concrete methods with their real implementation. The replacement reproduces that exactly:WP_Privacy_Requests_Tablehas no abstract methods of its own, soonlyMethods( array() )mocks nothing, leaving the instance fully real (matching how the test already uses it — calling real methods on it directly, never stubbing behavior).WP_Widget_Mediahas exactly one abstract method,render_media(), which was already the sole entry in$mocked_methods, soonlyMethods( array( 'render_media' ) )is a faithful equivalent.Verified by running all touched test classes (436 tests, 1318 assertions) plus PHPCS and PHPStan, all green.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.