Make WordPress Core

Changeset 63303


Ignore:
Timestamp:
08/15/2026 11:40:37 PM (17 hours ago)
Author:
SergeyBiryukov
Message:

Tests: Use the modern PHPUnit stubbing and matcher shorthands.

Two test cases still used the legacy PHPUnit stub API. This replaces them with the modern shorthands, which are what the rest of the suite already uses.

tests/phpunit/tests/pomo/pluralForms.php

// Before
->will( $this->returnValue( 1 ) );
// After
->willReturn( 1 );

tests/phpunit/tests/rest-api/rest-server.php

// Before
->with( $this->equalTo( 400 ) );
// After
->with( 400 );

Why this is safe

willReturn() is a direct shorthand for will( $this->returnValue() ) and has been available since PHPUnit 5.4. The test suite enforces a minimum of PHPUnit 5.7.21 in tests/phpunit/includes/bootstrap.php, so it is safe on every supported version, and the suite already contains 83 willReturn() calls.

with() wraps any non-Constraint argument in an equalTo() constraint itself, so passing 400 directly is identical in behaviour. The suite already has 43 with() calls that pass values directly.

Note on the first change: that line was originally written as willReturn( 1 ) and switched to the long form in r41725 because of the PHPUnit versions supported back then. That constraint no longer applies, so this effectively reverts an obsolete workaround.

Developed in https://github.com/WordPress/wordpress-develop/pull/13052.

Follow-up to r34928, r41725.

Props Soean.
See #65819.

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/pomo/pluralForms.php

    r62174 r63303  
    234234                        ->method( 'execute' )
    235235                        ->with( $this->identicalTo( 2 ) )
    236                         ->will( $this->returnValue( 1 ) );
     236                        ->willReturn( 1 );
    237237
    238238                $first  = $mock->get( 2 );
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r62806 r63303  
    653653                $stub->expects( $this->once() )
    654654                        ->method( 'set_status' )
    655                         ->with( $this->equalTo( 400 ) );
     655                        ->with( 400 );
    656656
    657657                $data     = array(
Note: See TracChangeset for help on using the changeset viewer.