mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
The `assertInternalType()` and `assertNotInternalType()` methods are deprecated in PHPUnit 8 and removed in PHPUnit 9. While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+. These methods introduced in PHPUnit 7.5 should be used as an alternative: * `assertIsArray()` * `assertIsBool()` * `assertIsFloat()` * `assertIsInt()` * `assertIsNumeric()` * `assertIsObject()` * `assertIsResource()` * `assertIsString()` * `assertIsScalar()` * `assertIsCallable()` * `assertIsIterable()` * `assertIsNotArray()` * `assertIsNotBool()` * `assertIsNotFloat()` * `assertIsNotInt()` * `assertIsNotNumeric()` * `assertIsNotObject()` * `assertIsNotResource()` * `assertIsNotString()` * `assertIsNotScalar()` * `assertIsNotCallable()` * `assertIsNotIterable()` As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the `WP_UnitTestCase` class for PHPUnit < 7.5. Props pbearne, jrf, dd32, SergeyBiryukov. Fixes #53491. See #46149. git-svn-id: https://develop.svn.wordpress.org/trunk@51331 602fd350-edb4-49c9-b593-d223f7449a82
376 lines
12 KiB
PHP
376 lines
12 KiB
PHP
<?php
|
|
|
|
require_once __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 {
|
|
|
|
/**
|
|
* 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 );
|
|
}
|
|
}
|