WordPress.org

Make WordPress Core

Changeset 49037


Ignore:
Timestamp:
09/23/2020 01:52:02 PM (10 months ago)
Author:
SergeyBiryukov
Message:

Tests: Backport two changes from PHPUnit 9.3:

  • Replace the Match interface with ParametersMatch, to avoid parse errors due to match being a reserved keyword in PHP 8.
  • Replace ReflectionParameter::getClass() usage, which is deprecated in PHP 8.

This allows tests relying on the getMockForAbstractClass() and getMockBuilder() methods to run again on PHP 8.

When the test suite is updated for compatibility with PHPUnit 9.x, these overrides can be removed.

Follow-up to [48972].

See #50913, #50902.

Location:
trunk
Files:
21 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/composer.json

    r47927 r49037  
    1717        "wp-coding-standards/wpcs": "~2.3.0",
    1818        "phpcompatibility/phpcompatibility-wp": "^2.1.0",
    19         "phpunit/phpunit": "^5.7 || ^6.5 || ^7.5"
     19        "phpunit/phpunit": "^7.5"
     20    },
     21    "autoload-dev": {
     22        "files": [
     23            "tests/phpunit/includes/phpunit7/MockObject/Builder/NamespaceMatch.php",
     24            "tests/phpunit/includes/phpunit7/MockObject/Builder/ParametersMatch.php",
     25            "tests/phpunit/includes/phpunit7/MockObject/InvocationMocker.php",
     26            "tests/phpunit/includes/phpunit7/MockObject/MockMethod.php"
     27        ],
     28        "exclude-from-classmap": [
     29            "vendor/phpunit/phpunit/src/Framework/MockObject/Builder/NamespaceMatch.php",
     30            "vendor/phpunit/phpunit/src/Framework/MockObject/Builder/ParametersMatch.php",
     31            "vendor/phpunit/phpunit/src/Framework/MockObject/InvocationMocker.php",
     32            "vendor/phpunit/phpunit/src/Framework/MockObject/MockMethod.php"
     33        ]
    2034    },
    2135    "scripts": {
  • trunk/phpcs.xml.dist

    r48072 r49037  
    135135    <exclude-pattern>/src/wp-includes/Text/*</exclude-pattern>
    136136
     137    <exclude-pattern>/tests/phpunit/includes/phpunit7/MockObject/*</exclude-pattern>
    137138    <exclude-pattern>/tests/phpunit/includes/speed-trap-listener\.php</exclude-pattern>
    138139
  • trunk/tests/phpunit/includes/phpunit7/testcase.php

    r48972 r49037  
    3838        static::assertThat( $actual, $constraint, $message );
    3939    }
    40 
    41     /**
    42      * Returns a mock object for the specified abstract class with all abstract
    43      * methods of the class mocked. Concrete methods to mock can be specified with
    44      * the last parameter.
    45      *
    46      * This method replaces the native PHPUnit method to avoid parse errors
    47      * due to `match` being a reserved keyword in PHP 8.
    48      *
    49      * To run on PHP 8, the tests using this method require PHPUnit 9.3 or later.
    50      *
    51      * When the test suite is updated for compatibility with PHPUnit 9.x,
    52      * this override can be removed.
    53      *
    54      * @since 5.6.0
    55      *
    56      * @param string $original_class_name
    57      * @param string $mock_class_name
    58      * @param bool   $call_original_constructor
    59      * @param bool   $call_original_clone
    60      * @param bool   $call_autoload
    61      * @param array  $mocked_methods
    62      * @param bool   $clone_arguments
    63      *
    64      * @throws \ReflectionException
    65      * @throws RuntimeException
    66      * @throws Exception
    67      *
    68      * @return MockObject
    69      */
    70     public function getMockForAbstractClass( $original_class_name, array $arguments = array(), $mock_class_name = '', $call_original_constructor = true, $call_original_clone = true, $call_autoload = true, $mocked_methods = array(), $clone_arguments = false ): PHPUnit\Framework\MockObject\MockObject {
    71         if ( PHP_VERSION_ID >= 80000 && version_compare( tests_get_phpunit_version(), '9.3', '<' ) ) {
    72             $this->markTestSkipped( 'To run on PHP 8, this test requires PHPUnit 9.3 or later.' );
    73         }
    74 
    75         return parent::getMockForAbstractClass( $original_class_name, $arguments, $mock_class_name, $call_original_constructor, $call_original_clone, $call_autoload, $mocked_methods, $clone_arguments );
    76     }
    77 
    78     /**
    79      * Returns a builder object to create mock objects using a fluent interface.
    80      *
    81      * This method replaces the native PHPUnit method to avoid parse errors
    82      * due to `match` being a reserved keyword in PHP 8.
    83      *
    84      * To run on PHP 8, the tests using this method require PHPUnit 9.3 or later.
    85      *
    86      * When the test suite is updated for compatibility with PHPUnit 9.x,
    87      * this override can be removed.
    88      *
    89      * @since 5.6.0
    90      *
    91      * @param string|string[] $class_name
    92      */
    93     public function getMockBuilder( $class_name ): PHPUnit\Framework\MockObject\MockBuilder {
    94         if ( PHP_VERSION_ID >= 80000 && version_compare( tests_get_phpunit_version(), '9.3', '<' ) ) {
    95             $this->markTestSkipped( 'To run on PHP 8, this test requires PHPUnit 9.3 or later.' );
    96         }
    97 
    98         return parent::getMockBuilder( $class_name );
    99     }
    10040}
Note: See TracChangeset for help on using the changeset viewer.