Make WordPress Core

Changeset 51838


Ignore:
Timestamp:
09/21/2021 07:04:34 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.8 branch.
See #53911.

Location:
branches/5.8
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

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

    r51357 r51838  
    3131env:
    3232  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }}
    33   COMPOSER_INSTALL: ${{ false }}
    3433  # Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`.
    3534  PHPUNIT_SCRIPT: php
     
    134133      - name: Get composer cache directory
    135134        id: composer-cache
    136         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    137135        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    138136
    139137      - name: Cache Composer dependencies
    140         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    141138        uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4
    142139        env:
     
    147144
    148145      - name: Install Composer dependencies
    149         if: ${{ env.COMPOSER_INSTALL == true || env.LOCAL_PHP == '8.0-fpm' }}
    150146        run: |
    151147          docker-compose run --rm php composer --version
     
    157153            docker-compose run --rm php composer install --ignore-platform-reqs
    158154            echo "PHPUNIT_SCRIPT=php-composer" >> $GITHUB_ENV
     155          elif [ ${{ env.LOCAL_PHP }} == '7.1-fpm' ]; then
     156            docker-compose run --rm php composer update
     157            git checkout -- composer.lock
     158          elif [[ ${{ env.LOCAL_PHP }} == '5.6.20-fpm' || ${{ env.LOCAL_PHP }} == '7.0-fpm' ]]; then
     159            docker-compose run --rm php composer require --dev phpunit/phpunit:"^5.7" --update-with-dependencies
     160            git checkout -- composer.lock composer.json
    159161          else
    160162            docker-compose run --rm php composer install
  • branches/5.8/.github/workflows/test-coverage.yml

    r51357 r51838  
    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
     
    9897        run: npm ci
    9998
     99      - name: Get composer cache directory
     100        id: composer-cache
     101        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
     102
     103      - name: Cache Composer dependencies
     104        uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4
     105        env:
     106          cache-name: cache-composer-dependencies
     107        with:
     108          path: ${{ steps.composer-cache.outputs.dir }}
     109          key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
     110
     111      - name: Install Composer dependencies
     112        run: |
     113          docker-compose run --rm php composer --version
     114          docker-compose run --rm php composer install
     115
    100116      - name: Docker debug information
    101117        run: |
  • branches/5.8/composer.json

    r49571 r51838  
    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.8/composer.lock

    r49571 r51838  
    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.8/tests/phpunit/includes/bootstrap.php

    r50986 r51838  
    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.8/tests/phpunit/includes/phpunit7/testcase.php

    r50986 r51838  
    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';
     
    1228 * All WordPress unit tests should inherit from this class.
    1329 */
    14 class WP_UnitTestCase extends WP_UnitTestCase_Base {}
     30class WP_UnitTestCase extends WP_UnitTestCase_Base {
     31
     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
     48    /**
     49     * Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
     50     */
     51    public static function set_up_before_class() {
     52        static::setUpBeforeClass();
     53    }
     54
     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();
     74    }
     75}
  • branches/5.8/tests/phpunit/includes/testcase.php

    r50986 r51838  
    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         static::assertEquals( $expected, $actual, $message, $delta );
     51    public static function set_up_before_class() {
     52        static::setUpBeforeClass();
     53    }
     54
     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();
    3474    }
    3575}
Note: See TracChangeset for help on using the changeset viewer.