Build/Test Tools: Unify the PHPUnit adapter TestCases.

This commit:
* Removes the PHPUnit 7 specific `TestCase`.
* Removes all existing polyfills from the PHPUnit 5.x `TestCase`.
* Imports all polyfill traits from the PHPUnit Polyfills package into the `WP_UnitTestCase` class and updates the DocBlock to reflect the actual function of the class.
 * Note: The list of polyfills needs to be verified and updated after each new release of the PHPUnit Polyfills package. Alternatively (recommended), one of the built-in `TestCase` classes from the PHPUnit Polyfills package can be used instead.

* Moves the `require` for the WP `abstract-testcase.php` to the `bootstrap.php` file.
* Adds a `require_once` for the PHPUnit Polyfills autoloader to the `bootstrap.php` file.
 * Note: while this isn't _strictly_ necessary when the tests are run via Composer, having the include in the bootstrap allows for the tests to also be run via a PHPUnit Phar, providing contributors with more flexibility.

Follow-up to [51559].

Props jrf, hellofromTonya, johnbillion, netweb, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51560 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-08-06 00:35:01 +00:00
parent 8ebd8ec0e4
commit 47303b1f95
3 changed files with 49 additions and 447 deletions

View File

@ -50,6 +50,14 @@ if ( version_compare( $phpunit_version, '5.7', '<' ) || version_compare( $phpuni
require_once __DIR__ . '/class-mockobject-autoload.php';
spl_autoload_register( 'MockObject_Autoload::load', true, true );
// Check that the PHPUnit Polyfills autoloader exists.
$phpunit_polyfills_autoloader = __DIR__ . '/../../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
if ( ! file_exists( $phpunit_polyfills_autoloader ) ) {
echo "Error: You need to run `composer update` before running the tests.\n";
echo "You can still use a PHPUnit phar to run them, but the dependencies do need to be installed.\n";
exit( 1 );
}
// If running core tests, check if all the required PHP extensions are loaded before running the test suite.
if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
$required_extensions = array(
@ -195,13 +203,12 @@ if ( version_compare( tests_get_phpunit_version(), '6.0', '>=' ) ) {
require __DIR__ . '/phpunit6/compat.php';
}
// Load separate WP_UnitTestCase classes for PHPUnit 7.5+ and older versions.
if ( version_compare( tests_get_phpunit_version(), '7.5', '>=' ) ) {
require __DIR__ . '/phpunit7/testcase.php';
} else {
require __DIR__ . '/testcase.php';
}
// Load the PHPUnit Polyfills autoloader (check for existence of the file is done earlier in the script).
require_once $phpunit_polyfills_autoloader;
unset( $phpunit_polyfills_autoloader );
require __DIR__ . '/abstract-testcase.php';
require __DIR__ . '/testcase.php';
require __DIR__ . '/testcase-rest-api.php';
require __DIR__ . '/testcase-rest-controller.php';
require __DIR__ . '/testcase-rest-post-type-controller.php';

View File

@ -1,14 +0,0 @@
<?php
require_once dirname( __DIR__ ) . '/abstract-testcase.php';
/**
* Defines a basic fixture to run multiple tests.
*
* Resets the state of the WordPress installation before and after every test.
*
* Includes utility functions and assertions useful for testing WordPress.
*
* All WordPress unit tests should inherit from this class.
*/
class WP_UnitTestCase extends WP_UnitTestCase_Base {}

View File

@ -1,439 +1,48 @@
<?php
require_once __DIR__ . '/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;
/**
* Defines a basic fixture to run multiple tests.
* Basic abstract test class with PHPUnit cross-version adapter layer.
*
* Resets the state of the WordPress installation before and after every test.
* This adapter layer polyfills all PHPUnit assertion and expectation
* methods which were added between PHPUnit 4.8 - 9.5 to allow tests
* to benefit from the full range of available PHPUnit methods.
*
* Includes utility functions and assertions useful for testing WordPress.
* See {@link https://github.com/Yoast/PHPUnit-Polyfills} for full
* documentation on the available polyfills.
*
* All WordPress unit tests should inherit from this class.
*/
class WP_UnitTestCase extends WP_UnitTestCase_Base {
/**
* Asserts that two variables are equal (with delta).
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.6.0
*
* @param mixed $expected First value to compare.
* @param mixed $actual Second value to compare.
* @param float $delta Allowed numerical distance between two values to consider them equal.
* @param string $message Optional. Message to display when the assertion fails.
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
static::assertEquals( $expected, $actual, $message, $delta );
}
/**
* Asserts that a variable is of type array.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsArray( $actual, $message = '' ) {
static::assertInternalType( 'array', $actual, $message );
}
/**
* Asserts that a variable is of type bool.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsBool( $actual, $message = '' ) {
static::assertInternalType( 'bool', $actual, $message );
}
/**
* Asserts that a variable is of type float.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsFloat( $actual, $message = '' ) {
static::assertInternalType( 'float', $actual, $message );
}
/**
* Asserts that a variable is of type int.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsInt( $actual, $message = '' ) {
static::assertInternalType( 'int', $actual, $message );
}
/**
* Asserts that a variable is of type numeric.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNumeric( $actual, $message = '' ) {
static::assertInternalType( 'numeric', $actual, $message );
}
/**
* Asserts that a variable is of type object.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsObject( $actual, $message = '' ) {
static::assertInternalType( 'object', $actual, $message );
}
/**
* Asserts that a variable is of type resource.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsResource( $actual, $message = '' ) {
static::assertInternalType( 'resource', $actual, $message );
}
/**
* Asserts that a variable is of type string.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsString( $actual, $message = '' ) {
static::assertInternalType( 'string', $actual, $message );
}
/**
* Asserts that a variable is of type scalar.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsScalar( $actual, $message = '' ) {
static::assertInternalType( 'scalar', $actual, $message );
}
/**
* Asserts that a variable is of type callable.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsCallable( $actual, $message = '' ) {
static::assertInternalType( 'callable', $actual, $message );
}
/**
* Asserts that a variable is of type iterable.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* Support for `iterable` was only added to `Assert::assertNotInternalType()`
* in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
* to that functionality until WordPress test suite requires PHPUnit 7.1.0
* as the minimum version.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsIterable( $actual, $message = '' ) {
static::assertTrue( is_iterable( $actual ), $message );
}
/**
* Asserts that a variable is not of type array.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotArray( $actual, $message = '' ) {
static::assertNotInternalType( 'array', $actual, $message );
}
/**
* Asserts that a variable is not of type bool.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotBool( $actual, $message = '' ) {
static::assertNotInternalType( 'bool', $actual, $message );
}
/**
* Asserts that a variable is not of type float.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotFloat( $actual, $message = '' ) {
static::assertNotInternalType( 'float', $actual, $message );
}
/**
* Asserts that a variable is not of type int.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotInt( $actual, $message = '' ) {
static::assertNotInternalType( 'int', $actual, $message );
}
/**
* Asserts that a variable is not of type numeric.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotNumeric( $actual, $message = '' ) {
static::assertNotInternalType( 'numeric', $actual, $message );
}
/**
* Asserts that a variable is not of type object.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotObject( $actual, $message = '' ) {
static::assertNotInternalType( 'object', $actual, $message );
}
/**
* Asserts that a variable is not of type resource.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotResource( $actual, $message = '' ) {
static::assertNotInternalType( 'resource', $actual, $message );
}
/**
* Asserts that a variable is not of type string.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotString( $actual, $message = '' ) {
static::assertNotInternalType( 'string', $actual, $message );
}
/**
* Asserts that a variable is not of type scalar.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotScalar( $actual, $message = '' ) {
static::assertNotInternalType( 'scalar', $actual, $message );
}
/**
* Asserts that a variable is not of type callable.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotCallable( $actual, $message = '' ) {
static::assertNotInternalType( 'callable', $actual, $message );
}
/**
* Asserts that a variable is not of type iterable.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* Support for `iterable` was only added to `Assert::assertNotInternalType()`
* in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
* to that functionality until WordPress test suite requires PHPUnit 7.1.0
* as the minimum version.
*
* @since 5.9.0
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertIsNotIterable( $actual, $message = '' ) {
static::assertFalse( is_iterable( $actual ), $message );
}
/**
* Asserts that a string haystack contains a needle.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
static::assertContains( $needle, $haystack, $message );
}
/**
* Asserts that a string haystack contains a needle (case-insensitive).
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
static::assertContains( $needle, $haystack, $message, true );
}
/**
* Asserts that a string haystack does not contain a needle.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
static::assertNotContains( $needle, $haystack, $message );
}
/**
* Asserts that a string haystack does not contain a needle (case-insensitive).
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
static::assertNotContains( $needle, $haystack, $message, true );
}
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;
}