mirror of
git://develop.git.wordpress.org/
synced 2025-01-16 20:38:35 +01:00
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.3 branch. See #53911. git-svn-id: https://develop.svn.wordpress.org/branches/5.3@51845 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
4f96938bb7
commit
a8d651d09a
15
.github/workflows/phpunit-tests.yml
vendored
15
.github/workflows/phpunit-tests.yml
vendored
@ -23,7 +23,6 @@ on:
|
||||
|
||||
env:
|
||||
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: ${{ true }}
|
||||
COMPOSER_INSTALL: ${{ false }}
|
||||
# Controls which NPM script to use for running PHPUnit tests. Options ar `php` and `php-composer`.
|
||||
PHPUNIT_SCRIPT: php
|
||||
LOCAL_PHP_MEMCACHED: ${{ false }}
|
||||
@ -139,11 +138,9 @@ jobs:
|
||||
|
||||
- name: Get composer cache directory
|
||||
id: composer-cache
|
||||
if: ${{ env.COMPOSER_INSTALL == true }}
|
||||
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
|
||||
|
||||
- name: Cache Composer dependencies
|
||||
if: ${{ env.COMPOSER_INSTALL == true }}
|
||||
uses: actions/cache@26968a09c0ea4f3e233fdddbafd1166051a095f6 # v2.1.4
|
||||
env:
|
||||
cache-name: cache-composer-dependencies
|
||||
@ -152,10 +149,16 @@ jobs:
|
||||
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||
|
||||
- name: Install Composer dependencies
|
||||
if: ${{ env.COMPOSER_INSTALL == true }}
|
||||
run: |
|
||||
docker-compose run --rm php composer --version
|
||||
docker-compose run --rm php composer install
|
||||
if [ ${{ env.LOCAL_PHP }} == '7.1-fpm' ]; then
|
||||
docker-compose run --rm php composer update
|
||||
git checkout -- composer.lock
|
||||
elif [[ ${{ env.LOCAL_PHP }} == '5.6.20-fpm' || ${{ env.LOCAL_PHP }} == '5.6-fpm' || ${{ env.LOCAL_PHP }} == '7.0-fpm' ]]; then
|
||||
docker-compose run --rm php composer require --dev phpunit/phpunit:"^5.7" --update-with-dependencies
|
||||
git checkout -- composer.lock composer.json
|
||||
else
|
||||
docker-compose run --rm php composer install
|
||||
fi
|
||||
|
||||
- name: Docker debug information
|
||||
run: |
|
||||
|
@ -15,7 +15,9 @@
|
||||
"require-dev": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7.0",
|
||||
"wp-coding-standards/wpcs": "~2.1.0",
|
||||
"phpcompatibility/phpcompatibility-wp": "^2.1.0"
|
||||
"phpcompatibility/phpcompatibility-wp": "^2.1.0",
|
||||
"phpunit/phpunit": "^5.7.21 || ^6.5 || ^7.5",
|
||||
"yoast/phpunit-polyfills": "^1.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source",
|
||||
|
1751
composer.lock
generated
1751
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -51,6 +51,113 @@ if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS && ! is_dir( ABSPATH )
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Load the PHPUnit Polyfills autoloader.
|
||||
*
|
||||
* The PHPUnit Polyfills are a requirement for the WP test suite.
|
||||
*
|
||||
* For running the Core tests, the Make WordPress Core handbook contains step-by-step instructions
|
||||
* on how to get up and running for a variety of supported workflows:
|
||||
* {@link https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/#test-running-workflow-options}
|
||||
*
|
||||
* Plugin/theme integration tests can handle this in any of the following ways:
|
||||
* - When using a full WP install: run `composer install` for the WP install prior to running the tests.
|
||||
* - When using a partial WP test suite install:
|
||||
* - Add a `yoast/phpunit-polyfills` (dev) requirement to the plugin/theme's own `composer.json` file.
|
||||
* - And then:
|
||||
* - Either load the PHPUnit Polyfills autoload file prior to running the WP core bootstrap file.
|
||||
* - Or declare a `WP_TESTS_PHPUNIT_POLYFILLS_PATH` constant containing the absolute path to the
|
||||
* root directory of the PHPUnit Polyfills installation.
|
||||
* If the constant is used, it is strongly recommended to declare this constant in the plugin/theme's
|
||||
* own test bootstrap file.
|
||||
* The constant MUST be declared prior to calling this file.
|
||||
*/
|
||||
if ( ! class_exists( 'Yoast\PHPUnitPolyfills\Autoload' ) ) {
|
||||
// Default location of the autoloader for WP core test runs.
|
||||
$phpunit_polyfills_autoloader = dirname( dirname( dirname( __DIR__ ) ) ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
|
||||
$phpunit_polyfills_error = false;
|
||||
|
||||
// Allow for a custom installation location to be provided for plugin/theme integration tests.
|
||||
if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
|
||||
$phpunit_polyfills_path = WP_TESTS_PHPUNIT_POLYFILLS_PATH;
|
||||
|
||||
if ( is_string( WP_TESTS_PHPUNIT_POLYFILLS_PATH )
|
||||
&& '' !== WP_TESTS_PHPUNIT_POLYFILLS_PATH
|
||||
) {
|
||||
// Be tolerant to the path being provided including the filename.
|
||||
if ( substr( $phpunit_polyfills_path, -29 ) !== 'phpunitpolyfills-autoload.php' ) {
|
||||
$phpunit_polyfills_path = rtrim( $phpunit_polyfills_path, '/\\' );
|
||||
$phpunit_polyfills_path = $phpunit_polyfills_path . '/phpunitpolyfills-autoload.php';
|
||||
}
|
||||
|
||||
$phpunit_polyfills_autoloader = $phpunit_polyfills_path;
|
||||
} else {
|
||||
$phpunit_polyfills_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $phpunit_polyfills_error || ! file_exists( $phpunit_polyfills_autoloader ) ) {
|
||||
echo 'Error: The PHPUnit Polyfills library is a requirement for running the WP test suite.' . PHP_EOL;
|
||||
if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
|
||||
printf(
|
||||
'The PHPUnit Polyfills autoload file was not found in "%s"' . PHP_EOL,
|
||||
WP_TESTS_PHPUNIT_POLYFILLS_PATH
|
||||
);
|
||||
echo 'Please verify that the file path provided in the WP_TESTS_PHPUNIT_POLYFILLS_PATH constant is correct.' . PHP_EOL;
|
||||
echo 'The WP_TESTS_PHPUNIT_POLYFILLS_PATH constant should contain an absolute path to the root directory'
|
||||
. ' of the PHPUnit Polyfills library.' . PHP_EOL;
|
||||
} elseif ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
|
||||
echo 'You need to run `composer install` before running the tests.' . PHP_EOL;
|
||||
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;
|
||||
} else {
|
||||
echo 'If you are trying to run plugin/theme integration tests, make sure the PHPUnit Polyfills library'
|
||||
. ' (https://github.com/Yoast/PHPUnit-Polyfills) is available and either load the autoload file'
|
||||
. ' of this library in your own test bootstrap before calling the WP Core test bootstrap file;'
|
||||
. ' or set the absolute path to the PHPUnit Polyfills library in a "WP_TESTS_PHPUNIT_POLYFILLS_PATH"'
|
||||
. ' constant to allow the WP Core bootstrap to load the Polyfills.' . PHP_EOL . PHP_EOL;
|
||||
echo 'If you are trying to run the WP Core tests, make sure to set the "WP_RUN_CORE_TESTS" constant'
|
||||
. ' to 1 and run `composer install` before running the tests.' . PHP_EOL;
|
||||
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;
|
||||
}
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
require_once $phpunit_polyfills_autoloader;
|
||||
}
|
||||
unset( $phpunit_polyfills_autoloader, $phpunit_polyfills_error, $phpunit_polyfills_path );
|
||||
|
||||
/*
|
||||
* Minimum version of the PHPUnit Polyfills package as declared in `composer.json`.
|
||||
* Only needs updating when new polyfill features start being used in the test suite.
|
||||
*/
|
||||
$phpunit_polyfills_minimum_version = '1.0.1';
|
||||
if ( class_exists( '\Yoast\PHPUnitPolyfills\Autoload' )
|
||||
&& ( defined( '\Yoast\PHPUnitPolyfills\Autoload::VERSION' ) === false
|
||||
|| version_compare( Yoast\PHPUnitPolyfills\Autoload::VERSION, $phpunit_polyfills_minimum_version, '<' ) )
|
||||
) {
|
||||
printf(
|
||||
'Error: Version mismatch detected for the PHPUnit Polyfills.'
|
||||
. ' Please ensure that PHPUnit Polyfills %s or higher is loaded. Found version: %s' . PHP_EOL,
|
||||
$phpunit_polyfills_minimum_version,
|
||||
defined( '\Yoast\PHPUnitPolyfills\Autoload::VERSION' ) ? Yoast\PHPUnitPolyfills\Autoload::VERSION : '1.0.0 or lower'
|
||||
);
|
||||
if ( defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
|
||||
printf(
|
||||
'Please ensure that the PHPUnit Polyfill installation in "%s" is updated to version %s or higher.' . PHP_EOL,
|
||||
WP_TESTS_PHPUNIT_POLYFILLS_PATH,
|
||||
$phpunit_polyfills_minimum_version
|
||||
);
|
||||
} elseif ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
|
||||
echo 'Please run `composer install` to install the latest version.' . PHP_EOL;
|
||||
}
|
||||
exit( 1 );
|
||||
}
|
||||
unset( $phpunit_polyfills_minimum_version );
|
||||
|
||||
tests_reset__SERVER();
|
||||
|
||||
define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
|
||||
|
@ -1,6 +1,22 @@
|
||||
<?php
|
||||
|
||||
require_once dirname( dirname( __FILE__ ) ) . '/abstract-testcase.php';
|
||||
use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
|
||||
|
||||
require_once dirname( __DIR__ ) . '/abstract-testcase.php';
|
||||
|
||||
/**
|
||||
* Defines a basic fixture to run multiple tests.
|
||||
@ -13,20 +29,47 @@ require_once dirname( dirname( __FILE__ ) ) . '/abstract-testcase.php';
|
||||
*/
|
||||
class WP_UnitTestCase extends WP_UnitTestCase_Base {
|
||||
|
||||
use AssertAttributeHelper;
|
||||
use AssertClosedResource;
|
||||
use AssertEqualsSpecializations;
|
||||
use AssertFileDirectory;
|
||||
use AssertFileEqualsSpecializations;
|
||||
use AssertionRenames;
|
||||
use AssertIsType;
|
||||
use AssertNumericType;
|
||||
use AssertObjectEquals;
|
||||
use AssertStringContains;
|
||||
use EqualToSpecializations;
|
||||
use ExpectException;
|
||||
use ExpectExceptionMessageMatches;
|
||||
use ExpectExceptionObject;
|
||||
use ExpectPHPException;
|
||||
|
||||
/**
|
||||
* Asserts that a condition is not false.
|
||||
*
|
||||
* This method has been backported from a more recent PHPUnit version, as tests running on PHP 5.2 use
|
||||
* PHPUnit 3.6.x.
|
||||
*
|
||||
* @since 4.7.4
|
||||
*
|
||||
* @param bool $condition Condition to check.
|
||||
* @param string $message Optional. Message to display when the assertion fails.
|
||||
*
|
||||
* @throws PHPUnit_Framework_AssertionFailedError
|
||||
* Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public static function assertNotFalse( $condition, string $message = '' ): void {
|
||||
self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
|
||||
public static function set_up_before_class() {
|
||||
static::setUpBeforeClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `tearDownAfterClass()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public static function tear_down_after_class() {
|
||||
static::tearDownAfterClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `setUp()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public function set_up() {
|
||||
static::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `tearDown()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public function tear_down() {
|
||||
static::tearDown();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,22 @@
|
||||
<?php
|
||||
|
||||
require_once dirname( __FILE__ ) . '/abstract-testcase.php';
|
||||
use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
|
||||
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
|
||||
|
||||
require_once __DIR__ . '/abstract-testcase.php';
|
||||
|
||||
/**
|
||||
* Defines a basic fixture to run multiple tests.
|
||||
@ -13,20 +29,47 @@ require_once dirname( __FILE__ ) . '/abstract-testcase.php';
|
||||
*/
|
||||
class WP_UnitTestCase extends WP_UnitTestCase_Base {
|
||||
|
||||
use AssertAttributeHelper;
|
||||
use AssertClosedResource;
|
||||
use AssertEqualsSpecializations;
|
||||
use AssertFileDirectory;
|
||||
use AssertFileEqualsSpecializations;
|
||||
use AssertionRenames;
|
||||
use AssertIsType;
|
||||
use AssertNumericType;
|
||||
use AssertObjectEquals;
|
||||
use AssertStringContains;
|
||||
use EqualToSpecializations;
|
||||
use ExpectException;
|
||||
use ExpectExceptionMessageMatches;
|
||||
use ExpectExceptionObject;
|
||||
use ExpectPHPException;
|
||||
|
||||
/**
|
||||
* Asserts that a condition is not false.
|
||||
*
|
||||
* This method has been backported from a more recent PHPUnit version, as tests running on PHP 5.2 use
|
||||
* PHPUnit 3.6.x.
|
||||
*
|
||||
* @since 4.7.4
|
||||
*
|
||||
* @param bool $condition Condition to check.
|
||||
* @param string $message Optional. Message to display when the assertion fails.
|
||||
*
|
||||
* @throws PHPUnit_Framework_AssertionFailedError
|
||||
* Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public static function assertNotFalse( $condition, $message = '' ) {
|
||||
self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
|
||||
public static function set_up_before_class() {
|
||||
static::setUpBeforeClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `tearDownAfterClass()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public static function tear_down_after_class() {
|
||||
static::tearDownAfterClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `setUp()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public function set_up() {
|
||||
static::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for the `tearDown()` method for forward-compatibility with WP 5.9.
|
||||
*/
|
||||
public function tear_down() {
|
||||
static::tearDown();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user