WordPress.org

Make WordPress Core

Changeset 48972


Ignore:
Timestamp:
09/13/2020 02:36:48 AM (11 months ago)
Author:
SergeyBiryukov
Message:

Tests: Replace the native PHPUnit getMockForAbstractClass() and getMockBuilder() methods.

This avoids parse errors in PHPUnit internals due to match being a reserved keyword in PHP 8.

To run on PHP 8, the tests relying on these methods require PHPUnit 9.3 or later.

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

See #50913, #50902.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/phpunit7/testcase.php

    r48953 r48972  
    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    }
    40100}
Note: See TracChangeset for help on using the changeset viewer.