Make WordPress Core

Changeset 51839


Ignore:
Timestamp:
09/21/2021 07:28:47 PM (3 years ago)
Author:
desrosj
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.7 branch.
See #53911.

Location:
branches/5.7
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/5.7

  • branches/5.7/.github/workflows/phpunit-tests.yml

    r50671 r51839  
    2222env:
    2323  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }}
    24   COMPOSER_INSTALL: ${{ false }}
    2524  # Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`.
    2625  PHPUNIT_SCRIPT: php
     
    138137      - name: Get composer cache directory
    139138        id: composer-cache
    140         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    141139        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    142140
    143141      - name: Cache Composer dependencies
    144         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    145142        uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4
    146143        env:
     
    151148
    152149      - name: Install Composer dependencies
    153         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    154150        run: |
    155151          docker-compose run --rm php composer --version
     
    161157            docker-compose run --rm php composer install --ignore-platform-reqs
    162158            echo "PHPUNIT_SCRIPT=php-composer" >> $GITHUB_ENV
     159          elif [ ${{ env.LOCAL_PHP }} == '7.1-fpm' ]; then
     160            docker-compose run --rm php composer update
     161            git checkout -- composer.lock
     162          elif [[ ${{ env.LOCAL_PHP }} == '5.6.20-fpm' || ${{ env.LOCAL_PHP }} == '7.0-fpm' ]]; then
     163            docker-compose run --rm php composer require --dev phpunit/phpunit:"^5.7" --update-with-dependencies
     164            git checkout -- composer.lock composer.json
    163165          else
    164166            docker-compose run --rm php composer install
  • branches/5.7/.github/workflows/test-coverage.yml

    r50601 r51839  
    1919env:
    2020  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }}
    21   COMPOSER_INSTALL: ${{ false }}
    2221  # Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`.
    2322  PHPUNIT_SCRIPT: php
     
    9695        run: npm ci
    9796
     97      - name: Get composer cache directory
     98        id: composer-cache
     99        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
     100
     101      - name: Cache Composer dependencies
     102        uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4
     103        env:
     104          cache-name: cache-composer-dependencies
     105        with:
     106          path: ${{ steps.composer-cache.outputs.dir }}
     107          key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
     108
     109      - name: Install Composer dependencies
     110        run: |
     111          docker-compose run --rm php composer --version
     112          docker-compose run --rm php composer install
     113
    98114      - name: Docker debug information
    99115        run: |
  • branches/5.7/composer.json

    r49571 r51839  
    1717        "wp-coding-standards/wpcs": "~2.3.0",
    1818        "phpcompatibility/phpcompatibility-wp": "^2.1.0",
    19         "phpunit/phpunit": "^7.5"
     19        "phpunit/phpunit": "^7.5",
     20        "yoast/phpunit-polyfills": "^1.0.1"
    2021    },
    2122    "autoload-dev": {
  • branches/5.7/composer.lock

    r49571 r51839  
    55        "This file is @generated automatically"
    66    ],
    7     "content-hash": "463db2b4afb439fb63d93173c0852e27",
     7    "content-hash": "d8bcbf1466f6595287a44721f525844d",
    88    "packages": [],
    99    "packages-dev": [
     
    18281828            ],
    18291829            "time": "2020-05-13T23:57:56+00:00"
     1830        },
     1831        {
     1832            "name": "yoast/phpunit-polyfills",
     1833            "version": "1.0.1",
     1834            "source": {
     1835                "type": "git",
     1836                "url": "https://github.com/Yoast/PHPUnit-Polyfills.git",
     1837                "reference": "f014fb21c2b0038fd329515d59025af42fb98715"
     1838            },
     1839            "dist": {
     1840                "type": "zip",
     1841                "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/f014fb21c2b0038fd329515d59025af42fb98715",
     1842                "reference": "f014fb21c2b0038fd329515d59025af42fb98715",
     1843                "shasum": ""
     1844            },
     1845            "require": {
     1846                "php": ">=5.4",
     1847                "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
     1848            },
     1849            "require-dev": {
     1850                "php-parallel-lint/php-console-highlighter": "^0.5",
     1851                "php-parallel-lint/php-parallel-lint": "^1.3.0",
     1852                "yoast/yoastcs": "^2.1.0"
     1853            },
     1854            "type": "library",
     1855            "extra": {
     1856                "branch-alias": {
     1857                    "dev-main": "1.x-dev",
     1858                    "dev-develop": "1.x-dev"
     1859                }
     1860            },
     1861            "autoload": {
     1862                "files": [
     1863                    "phpunitpolyfills-autoload.php"
     1864                ]
     1865            },
     1866            "notification-url": "https://packagist.org/downloads/",
     1867            "license": [
     1868                "BSD-3-Clause"
     1869            ],
     1870            "authors": [
     1871                {
     1872                    "name": "Team Yoast",
     1873                    "email": "support@yoast.com",
     1874                    "homepage": "https://yoast.com"
     1875                },
     1876                {
     1877                    "name": "Contributors",
     1878                    "homepage": "https://github.com/Yoast/PHPUnit-Polyfills/graphs/contributors"
     1879                }
     1880            ],
     1881            "description": "Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests",
     1882            "homepage": "https://github.com/Yoast/PHPUnit-Polyfills",
     1883            "keywords": [
     1884                "phpunit",
     1885                "polyfill",
     1886                "testing"
     1887            ],
     1888            "support": {
     1889                "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues",
     1890                "source": "https://github.com/Yoast/PHPUnit-Polyfills"
     1891            },
     1892            "time": "2021-08-09T16:28:08+00:00"
    18301893        }
    18311894    ],
     
    18391902    },
    18401903    "platform-dev": [],
    1841     "plugin-api-version": "1.1.0"
     1904    "plugin-api-version": "2.1.0"
    18421905}
  • branches/5.7/tests/phpunit/includes/bootstrap.php

    r49571 r51839  
    4545$phpunit_version = tests_get_phpunit_version();
    4646
    47 if ( version_compare( $phpunit_version, '5.4', '<' ) || version_compare( $phpunit_version, '8.0', '>=' ) ) {
     47if ( version_compare( $phpunit_version, '5.7.21', '<' ) || version_compare( $phpunit_version, '8.0', '>=' ) ) {
    4848    printf(
    49         "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",
     49        "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",
    5050        $phpunit_version
    5151    );
     
    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// If running core tests, check if all the required PHP extensions are loaded before running the test suite.
  • branches/5.7/tests/phpunit/includes/phpunit7/testcase.php

    r49037 r51839  
    11<?php
     2
     3use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
     4use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
     5use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
     6use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
     7use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
     8use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
     9use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
     10use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
     11use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
     12use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
     13use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
     14use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
     15use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
     16use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
     17use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
    218
    319require_once dirname( __DIR__ ) . '/abstract-testcase.php';
     
    1430class WP_UnitTestCase extends WP_UnitTestCase_Base {
    1531
     32    use AssertAttributeHelper;
     33    use AssertClosedResource;
     34    use AssertEqualsSpecializations;
     35    use AssertFileDirectory;
     36    use AssertFileEqualsSpecializations;
     37    use AssertionRenames;
     38    use AssertIsType;
     39    use AssertNumericType;
     40    use AssertObjectEquals;
     41    use AssertStringContains;
     42    use EqualToSpecializations;
     43    use ExpectException;
     44    use ExpectExceptionMessageMatches;
     45    use ExpectExceptionObject;
     46    use ExpectPHPException;
     47
    1648    /**
    17      * Asserts that two variables are equal (with delta).
    18      *
    19      * This method has been backported from a more recent PHPUnit version,
    20      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    21      *
    22      * @since 5.6.0
    23      *
    24      * @param mixed  $expected First value to compare.
    25      * @param mixed  $actual   Second value to compare.
    26      * @param float  $delta    Allowed numerical distance between two values to consider them equal.
    27      * @param string $message  Optional. Message to display when the assertion fails.
    28      *
    29      * @throws ExpectationFailedException
    30      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     49     * Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
    3150     */
    32     public static function assertEqualsWithDelta( $expected, $actual, float $delta, string $message = '' ): void {
    33         $constraint = new PHPUnit\Framework\Constraint\IsEqual(
    34             $expected,
    35             $delta
    36         );
     51    public static function set_up_before_class() {
     52        static::setUpBeforeClass();
     53    }
    3754
    38         static::assertThat( $actual, $constraint, $message );
     55    /**
     56     * Wrapper method for the `tearDownAfterClass()` method for forward-compatibility with WP 5.9.
     57     */
     58    public static function tear_down_after_class() {
     59        static::tearDownAfterClass();
     60    }
     61
     62    /**
     63     * Wrapper method for the `setUp()` method for forward-compatibility with WP 5.9.
     64     */
     65    public function set_up() {
     66        static::setUp();
     67    }
     68
     69    /**
     70     * Wrapper method for the `tearDown()` method for forward-compatibility with WP 5.9.
     71     */
     72    public function tear_down() {
     73        static::tearDown();
    3974    }
    4075}
  • branches/5.7/tests/phpunit/includes/testcase.php

    r48953 r51839  
    11<?php
     2
     3use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
     4use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
     5use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
     6use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
     7use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
     8use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
     9use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
     10use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
     11use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
     12use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
     13use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
     14use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
     15use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
     16use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
     17use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
    218
    319require_once __DIR__ . '/abstract-testcase.php';
     
    1430class WP_UnitTestCase extends WP_UnitTestCase_Base {
    1531
     32    use AssertAttributeHelper;
     33    use AssertClosedResource;
     34    use AssertEqualsSpecializations;
     35    use AssertFileDirectory;
     36    use AssertFileEqualsSpecializations;
     37    use AssertionRenames;
     38    use AssertIsType;
     39    use AssertNumericType;
     40    use AssertObjectEquals;
     41    use AssertStringContains;
     42    use EqualToSpecializations;
     43    use ExpectException;
     44    use ExpectExceptionMessageMatches;
     45    use ExpectExceptionObject;
     46    use ExpectPHPException;
     47
    1648    /**
    17      * Asserts that two variables are equal (with delta).
    18      *
    19      * This method has been backported from a more recent PHPUnit version,
    20      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    21      *
    22      * @since 5.6.0
    23      *
    24      * @param mixed  $expected First value to compare.
    25      * @param mixed  $actual   Second value to compare.
    26      * @param float  $delta    Allowed numerical distance between two values to consider them equal.
    27      * @param string $message  Optional. Message to display when the assertion fails.
    28      *
    29      * @throws ExpectationFailedException
    30      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     49     * Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
    3150     */
    32     public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
    33         $constraint = new PHPUnit_Framework_Constraint_IsEqual(
    34             $expected,
    35             $delta
    36         );
     51    public static function set_up_before_class() {
     52        static::setUpBeforeClass();
     53    }
    3754
    38         static::assertThat( $actual, $constraint, $message );
     55    /**
     56     * Wrapper method for the `tearDownAfterClass()` method for forward-compatibility with WP 5.9.
     57     */
     58    public static function tear_down_after_class() {
     59        static::tearDownAfterClass();
     60    }
     61
     62    /**
     63     * Wrapper method for the `setUp()` method for forward-compatibility with WP 5.9.
     64     */
     65    public function set_up() {
     66        static::setUp();
     67    }
     68
     69    /**
     70     * Wrapper method for the `tearDown()` method for forward-compatibility with WP 5.9.
     71     */
     72    public function tear_down() {
     73        static::tearDown();
    3974    }
    4075}
Note: See TracChangeset for help on using the changeset viewer.