wordpress-develop/tests/phpunit/includes/testcase.php
Sergey Biryukov bb3bf22547 Tests: Use more appropriate assertions in various tests.
This replaces instances of `assertFalse( stripos( ... ) )` with `assertStringNotContainsString()` or `assertStringNotContainsStringIgnoringCase()` to use native PHPUnit functionality.

Going forward, these methods introduced in PHPUnit 7.5 should be used for similar assertions:

* `assertStringContainsString()`
* `assertStringContainsStringIgnoringCase()`
* `assertStringNotContainsString()`
* `assertStringNotContainsStringIgnoringCase()`

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.

Follow-up to [51335], [51337], [51367], [51397], [51403], [51404], [51436], [51438], [51448], [51449], [51451], [51453], [51454].

See #53363.

git-svn-id: https://develop.svn.wordpress.org/trunk@51461 602fd350-edb4-49c9-b593-d223f7449a82
2021-07-19 13:29:45 +00:00

440 lines
14 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 );
}
/**
* 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 );
}
}