Make WordPress Core


Ignore:
Timestamp:
09/21/2021 10:48:47 PM (3 years ago)
Author:
hellofromTonya
Message:

Build/Test Tools: Introduce the PHPUnit Polyfills package for easier cross branch testing.

This backports the PHPUnit Polyfills package and related test infrastructure changes to make it easier for developers to continue testing on multiple versions WordPress while adding tests for newer versions of PHP, which require more modern PHPUnit practices.

One of the changes included is the addition of wrappers for the new snake_case fixture methods in PHPUnit. This allows the native camelCase standard in PHPUnit to be used, but allows for developers to transition to the new naming conventions.

Props hellofromTonya, jrf, SergeyBiryukov, johnbillion, netweb, schlessera, jeherve, lucatume, desrosj.
Merges [51559,51560,51810-51813,51828] to the 5.5 branch.
See #53911.

Location:
branches/5.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/tests/phpunit/includes/bootstrap.php

    r48592 r51843  
    4040$phpunit_version = tests_get_phpunit_version();
    4141
    42 if ( version_compare( $phpunit_version, '5.4', '<' ) || version_compare( $phpunit_version, '8.0', '>=' ) ) {
     42if ( version_compare( $phpunit_version, '5.7.21', '<' ) || version_compare( $phpunit_version, '8.0', '>=' ) ) {
    4343    printf(
    44         "Error: Looks like you're using PHPUnit %s. WordPress requires at least PHPUnit 5.4 and is currently only compatible with PHPUnit up to 7.x.\n",
     44        "Error: Looks like you're using PHPUnit %s. WordPress requires at least PHPUnit 5.7.21 and is currently only compatible with PHPUnit up to 7.x.\n",
    4545        $phpunit_version
    4646    );
     
    5353    exit( 1 );
    5454}
     55
     56/*
     57 * Load the PHPUnit Polyfills autoloader.
     58 *
     59 * The PHPUnit Polyfills are a requirement for the WP test suite.
     60 *
     61 * For running the Core tests, the Make WordPress Core handbook contains step-by-step instructions
     62 * on how to get up and running for a variety of supported workflows:
     63 * {@link https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/#test-running-workflow-options}
     64 *
     65 * Plugin/theme integration tests can handle this in any of the following ways:
     66 * - When using a full WP install: run `composer install` for the WP install prior to running the tests.
     67 * - When using a partial WP test suite install:
     68 *   - Add a `yoast/phpunit-polyfills` (dev) requirement to the plugin/theme's own `composer.json` file.
     69 *   - And then:
     70 *     - Either load the PHPUnit Polyfills autoload file prior to running the WP core bootstrap file.
     71 *     - Or declare a `WP_TESTS_PHPUNIT_POLYFILLS_PATH` constant containing the absolute path to the
     72 *       root directory of the PHPUnit Polyfills installation.
     73 *       If the constant is used, it is strongly recommended to declare this constant in the plugin/theme's
     74 *       own test bootstrap file.
     75 *       The constant MUST be declared prior to calling this file.
     76 */
     77if ( ! class_exists( 'Yoast\PHPUnitPolyfills\Autoload' ) ) {
     78    // Default location of the autoloader for WP core test runs.
     79    $phpunit_polyfills_autoloader = dirname( dirname( dirname( __DIR__ ) ) ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
     80    $phpunit_polyfills_error      = false;
     81
     82    // Allow for a custom installation location to be provided for plugin/theme integration tests.
     83    if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
     84        $phpunit_polyfills_path = WP_TESTS_PHPUNIT_POLYFILLS_PATH;
     85
     86        if ( is_string( WP_TESTS_PHPUNIT_POLYFILLS_PATH )
     87            && '' !== WP_TESTS_PHPUNIT_POLYFILLS_PATH
     88        ) {
     89            // Be tolerant to the path being provided including the filename.
     90            if ( substr( $phpunit_polyfills_path, -29 ) !== 'phpunitpolyfills-autoload.php' ) {
     91                $phpunit_polyfills_path = rtrim( $phpunit_polyfills_path, '/\\' );
     92                $phpunit_polyfills_path = $phpunit_polyfills_path . '/phpunitpolyfills-autoload.php';
     93            }
     94
     95            $phpunit_polyfills_autoloader = $phpunit_polyfills_path;
     96        } else {
     97            $phpunit_polyfills_error = true;
     98        }
     99    }
     100
     101    if ( $phpunit_polyfills_error || ! file_exists( $phpunit_polyfills_autoloader ) ) {
     102        echo 'Error: The PHPUnit Polyfills library is a requirement for running the WP test suite.' . PHP_EOL;
     103        if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
     104            printf(
     105                'The PHPUnit Polyfills autoload file was not found in "%s"' . PHP_EOL,
     106                WP_TESTS_PHPUNIT_POLYFILLS_PATH
     107            );
     108            echo 'Please verify that the file path provided in the WP_TESTS_PHPUNIT_POLYFILLS_PATH constant is correct.' . PHP_EOL;
     109            echo 'The WP_TESTS_PHPUNIT_POLYFILLS_PATH constant should contain an absolute path to the root directory'
     110                . ' of the PHPUnit Polyfills library.' . PHP_EOL;
     111        } elseif ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     112            echo 'You need to run `composer install` before running the tests.' . PHP_EOL;
     113            echo 'Once the dependencies are installed, you can run the tests using the Composer-installed version'
     114                . ' of PHPUnit or using a PHPUnit phar file, but the dependencies do need to be installed'
     115                . ' whichever way the tests are run.' . PHP_EOL;
     116        } else {
     117            echo 'If you are trying to run plugin/theme integration tests, make sure the PHPUnit Polyfills library'
     118                . ' (https://github.com/Yoast/PHPUnit-Polyfills) is available and either load the autoload file'
     119                . ' of this library in your own test bootstrap before calling the WP Core test bootstrap file;'
     120                . ' or set the absolute path to the PHPUnit Polyfills library in a "WP_TESTS_PHPUNIT_POLYFILLS_PATH"'
     121                . ' constant to allow the WP Core bootstrap to load the Polyfills.' . PHP_EOL . PHP_EOL;
     122            echo 'If you are trying to run the WP Core tests, make sure to set the "WP_RUN_CORE_TESTS" constant'
     123                . ' to 1 and run `composer install` before running the tests.' . PHP_EOL;
     124            echo 'Once the dependencies are installed, you can run the tests using the Composer-installed'
     125                . ' version of PHPUnit or using a PHPUnit phar file, but the dependencies do need to be'
     126                . ' installed whichever way the tests are run.' . PHP_EOL;
     127        }
     128        exit( 1 );
     129    }
     130
     131    require_once $phpunit_polyfills_autoloader;
     132}
     133unset( $phpunit_polyfills_autoloader, $phpunit_polyfills_error, $phpunit_polyfills_path );
     134
     135/*
     136 * Minimum version of the PHPUnit Polyfills package as declared in `composer.json`.
     137 * Only needs updating when new polyfill features start being used in the test suite.
     138 */
     139$phpunit_polyfills_minimum_version = '1.0.1';
     140if ( class_exists( '\Yoast\PHPUnitPolyfills\Autoload' )
     141    && ( defined( '\Yoast\PHPUnitPolyfills\Autoload::VERSION' ) === false
     142        || version_compare( Yoast\PHPUnitPolyfills\Autoload::VERSION, $phpunit_polyfills_minimum_version, '<' ) )
     143) {
     144    printf(
     145        'Error: Version mismatch detected for the PHPUnit Polyfills.'
     146        . ' Please ensure that PHPUnit Polyfills %s or higher is loaded. Found version: %s' . PHP_EOL,
     147        $phpunit_polyfills_minimum_version,
     148        defined( '\Yoast\PHPUnitPolyfills\Autoload::VERSION' ) ? Yoast\PHPUnitPolyfills\Autoload::VERSION : '1.0.0 or lower'
     149    );
     150    if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
     151        printf(
     152            'Please ensure that the PHPUnit Polyfill installation in "%s" is updated to version %s or higher.' . PHP_EOL,
     153            WP_TESTS_PHPUNIT_POLYFILLS_PATH,
     154            $phpunit_polyfills_minimum_version
     155        );
     156    } elseif ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
     157        echo 'Please run `composer install` to install the latest version.' . PHP_EOL;
     158    }
     159    exit( 1 );
     160}
     161unset( $phpunit_polyfills_minimum_version );
    55162
    56163$required_constants = array(
Note: See TracChangeset for help on using the changeset viewer.