Make WordPress Core

Changeset 51598


Ignore:
Timestamp:
08/10/2021 09:13:38 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Make the polyfills loading more flexible.

The PHPUnit Polyfills are, since [51559], a required dependency for the WP test suite and, by extension, for plugin/theme integration test suites which are based on and use (parts of) the WP core test suite.

However, plugin/theme integration test suites may not use a full WordPress installation.

This commit:

  • Removes the presumption that a full WP install, including vendor directory, will be available when the test bootstrap.php file is run.
  • Makes the loading of the PHPUnit Polyfills autoload file more flexible by:
    • Checking if the autoload class contained within the autoload file is already available before attempting to load the file.
      This allows for plugin/theme integration test suites to load the phpunitpolyfills-autoload.php file from any location, as long as it is loaded before the WP core test bootstrap.php file is run.
    • Allowing for the path to an arbitrary installation location for the PHPUnit Polyfills to be passed as a constant.
      As long as the provided location is a valid file path and the phpunitpolyfills-autoload.php file exists in the provided location, that file will be loaded.
      The constant can be declared in a plugin/theme integration test suite native test bootstrap file, or in the wp-tests-config.php file, or even in a phpunit.xml[.dist] file via <php><const name="WP_TESTS_PHPUNIT_POLYFILLS_PATH" value="path/to/yoast/phpunit-polyfills"/></php>.
  • Adds a version check for the PHPUnit Polyfills to prevent a mismatch between the version of the package expected by WordPress and the version used by plugins/themes.
    The version this checks for should be in line with the minimum version requirement for the PHPUnit Polyfills as declared in the composer.json file.
    This version number should only be updated when new features added in later PHPUnit Polyfills releases are actually used in the WP core test suite.
  • Adds appropriate error messages for every possible error condition.
  • Upgrades the PHPUnit Polyfills to version 1.0.1, which now includes a version constant.

Follow-up to [51559-51577].

Props jrf, hellofromTonya, swissspidy, jeherve, thomasplevy, SergeyBiryukov.
See #46149.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/composer.json

    r51574 r51598  
    1818        "wp-coding-standards/wpcs": "~2.3.0",
    1919        "phpcompatibility/phpcompatibility-wp": "~2.1.2",
    20         "yoast/phpunit-polyfills": "^1.0"
     20        "yoast/phpunit-polyfills": "^1.0.1"
    2121    },
    2222    "scripts": {
  • trunk/tests/phpunit/includes/bootstrap.php

    r51584 r51598  
    4747}
    4848
    49 // Check that the PHPUnit Polyfills autoloader exists.
    50 $phpunit_polyfills_autoloader = __DIR__ . '/../../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
    51 if ( ! file_exists( $phpunit_polyfills_autoloader ) ) {
    52     echo 'Error: You need to run `composer update` before running the tests.' . PHP_EOL;
    53     echo 'You can still use a PHPUnit phar to run them, but the dependencies do need to be installed.' . PHP_EOL;
    54     exit( 1 );
    55 }
     49/*
     50 * Load the PHPUnit Polyfills autoloader.
     51 *
     52 * The PHPUnit Polyfills are a requirement for the WP test suite.
     53 *
     54 * Plugin/theme integration tests can handle this in any of the following ways:
     55 * - When using a full WP install: run `composer update` for the WP install prior to running the tests.
     56 * - When using a partial WP test suite install:
     57 *   - Add a `yoast/phpunit-polyfills` (dev) requirement to their own `composer.json` file.
     58 *   - And then:
     59 *     - Either load the PHPUnit Polyfills autoload file prior to running the WP core bootstrap file.
     60 *     - Or declare a `WP_TESTS_PHPUNIT_POLYFILLS_PATH` constant pointing to the root directory
     61 *       of the PHPUnit Polyfills installation.
     62 *       This constant can be declared in the `phpunit.xml[.dist]` file like this:
     63 *       `<php><const name="WP_TESTS_PHPUNIT_POLYFILLS_PATH" value="path/to/yoast/phpunit-polyfills"/></php>
     64 *       or can be declared as a PHP constant in the `wp-tests-config.php` file or the plugin/theme
     65 *       test bootstrap file.
     66 */
     67if ( ! class_exists( 'Yoast\PHPUnitPolyfills\Autoload' ) ) {
     68    // Default location of the autoloader for WP core test runs.
     69    $phpunit_polyfills_autoloader = __DIR__ . '/../../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
     70    $phpunit_polyfills_error      = false;
     71
     72    // Allow for a custom installation location to be provided for plugin/theme integration tests.
     73    if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
     74        $phpunit_polyfills_path = WP_TESTS_PHPUNIT_POLYFILLS_PATH;
     75
     76        if ( is_string( WP_TESTS_PHPUNIT_POLYFILLS_PATH )
     77            && '' !== WP_TESTS_PHPUNIT_POLYFILLS_PATH
     78        ) {
     79            $phpunit_polyfills_path = rtrim( $phpunit_polyfills_path, '/\\' );
     80            $phpunit_polyfills_path = realpath( $phpunit_polyfills_path . '/phpunitpolyfills-autoload.php' );
     81            if ( false !== $phpunit_polyfills_path ) {
     82                $phpunit_polyfills_autoloader = $phpunit_polyfills_path;
     83            } else {
     84                $phpunit_polyfills_error = true;
     85            }
     86        } else {
     87            $phpunit_polyfills_error = true;
     88        }
     89    }
     90
     91    if ( $phpunit_polyfills_error || ! file_exists( $phpunit_polyfills_autoloader ) ) {
     92        echo 'Error: The PHPUnit Polyfills library is a requirement for running the WP test suite.' . PHP_EOL;
     93        if ( isset( $phpunit_polyfills_path ) ) {
     94            printf(
     95                'The PHPUnit Polyfills autoload file was not found in "%s"' . PHP_EOL,
     96                WP_TESTS_PHPUNIT_POLYFILLS_PATH
     97            );
     98            echo 'Please verify that the file path provided in the WP_TESTS_PHPUNIT_POLYFILLS_PATH constant is correct.' . PHP_EOL;
     99        } else {
     100            echo 'You need to run `composer update` before running the tests.' . PHP_EOL;
     101            echo 'Once the dependencies are installed, you can run the tests using the Composer-installed version of PHPUnit or using a PHPUnit phar file, but the dependencies do need to be installed whichever way the tests are run.' . PHP_EOL;
     102        }
     103        exit( 1 );
     104    }
     105
     106    require_once $phpunit_polyfills_autoloader;
     107}
     108unset( $phpunit_polyfills_autoloader, $phpunit_polyfills_error, $phpunit_polyfills_path );
     109
     110/*
     111 * Minimum version of the PHPUnit Polyfills package as declared in `composer.json`.
     112 * Only needs updating when new polyfill features start being used in the test suite.
     113 */
     114$phpunit_polyfills_minimum_version = '1.0.1';
     115if ( class_exists( '\Yoast\PHPUnitPolyfills\Autoload' )
     116    && ( defined( '\Yoast\PHPUnitPolyfills\Autoload::VERSION' ) === false
     117    || version_compare( \Yoast\PHPUnitPolyfills\Autoload::VERSION, $phpunit_polyfills_minimum_version, '<' ) )
     118) {
     119    printf(
     120        'Error: Version mismatch detected for the PHPUnit Polyfills. Please ensure that PHPUnit Polyfills %s or higher is loaded.' . PHP_EOL,
     121        $phpunit_polyfills_minimum_version
     122    );
     123    if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
     124        echo 'Please run `composer update` to install the latest version.' . PHP_EOL;
     125    }
     126    exit( 1 );
     127}
     128unset( $phpunit_polyfills_minimum_version );
    56129
    57130// If running core tests, check if all the required PHP extensions are loaded before running the test suite.
     
    199272    require __DIR__ . '/phpunit6/compat.php';
    200273}
    201 
    202 // Load the PHPUnit Polyfills autoloader (check for existence of the file is done earlier in the script).
    203 require_once $phpunit_polyfills_autoloader;
    204 unset( $phpunit_polyfills_autoloader );
    205274
    206275require __DIR__ . '/phpunit-adapter-testcase.php';
Note: See TracChangeset for help on using the changeset viewer.