Assertions
Introduction
Mantle's Test Case provides some assertions above and beyond PHPUnit's, largely
influenced by WP_UnitTestCase
. Here's a run-through:
WP_Error
Assert the given item is/is not a WP_Error
:
$this->assertWPError( $actual, $message = '' );
$this->assertNotWPError( $actual, $message = '' );
General Assertions
assertEqualFields
Asserts that the given fields are present in the given object:
$this->assertEqualFields( $object, $fields );
assertDiscardWhitespace
Asserts that two values are equal, with whitespace differences discarded:
$this->assertDiscardWhitespace( $expected, $actual );
assertEqualsIgnoreEOL
Asserts that two values are equal, with EOL differences discarded:
$this->assertEqualsIgnoreEOL( $expected, $actual );
assertEqualSets
Asserts that the contents of two un-keyed, single arrays are equal, without accounting for the order of elements:
$this->assertEqualSets( $expected, $actual );
assertEqualSetsWithIndex
Asserts that the contents of two keyed, single arrays are equal, without accounting for the order of elements:
$this->assertEqualSetsWithIndex( $expected, $actual );
assertNonEmptyMultidimensionalArray
Asserts that the given variable is a multidimensional array, and that all arrays are non-empty:
$this->assertNonEmptyMultidimensionalArray( $array );
WordPress Query Assertions
assertQueryTrue
Assert that the global WordPress query is true for a given set of properties and false for the rest:
$this->assertQueryTrue( ...$prop );
assertQueriedObjectId
Assert that the global queried object ID is equal to the given ID:
$this->assertQueriedObjectId( int $id );
assertNotQueriedObjectId
Assert that the global queried object ID is not equal to the given ID:
$this->assertNotQueriedObjectId( int $id );
assertQueriedObject
Assert that the global queried object is equal to the given object:
$this->assertQueriedObject( $object );
assertNotQueriedObject
Assert that the global queried object is not equal to the given object:
$this->assertNotQueriedObject( $object );
assertQueriedObjectNull
Assert that the global queried object is null:
$this->assertQueriedObjectNull();
WordPress Post/Term Existence
assertPostExists
Assert if a post exists given a set of arguments.
$this->assertPostExists( array $args );
assertPostDoesNotExists
Assert if a post doesn't exist given a set of arguments.
$this->assertPostDoesNotExists( array $args );
assertTermExists
Assert if a term exists given a set of arguments.
$this->assertTermExists( array $args );
assertTermDoesNotExists
Assert if a term doesn't exists given a set of arguments.
$this->assertTermDoesNotExists( array $args );
assertUserExists
Assert if a user exists given a set of arguments.
$this->assertUserExists( array $args );
assertUserDoesNotExists
Assert if a user doesn't exists given a set of arguments.
$this->assertUserDoesNotExists( array $args );
assertPostHasTerm
Assert if a post has a specific term.
$this->assertPostHasTerm( $post, $term );
assertPostNotHasTerm
Assert if a post does not have a specific term (aliased to assertPostsDoesNotHaveTerm()
)
$this->assertPostNotHasTerm( $post, $term );
Asset Assertions
Assets that are registered and/or enqueued with WordPress can be asserted against using the following methods:
assertScriptStatus
Assert against the status of a script. $status
accepts 'enqueued',
'registered', 'queue', 'to_do', and 'done'.
$this->assertScriptStatus( string $handle, string $status );
assertStyleStatus
Assert against the status of a style. $status
accepts 'enqueued',
'registered', 'queue', 'to_do', and 'done'.
$this->assertStyleStatus( string $handle, string $status );
assertScriptEnqueued
Assert that a script is enqueued by handle.
$this->assertScriptEnqueued( string $handle );
assertScriptNotEnqueued
Assert that a script is not enqueued by handle.
$this->assertScriptNotEnqueued( string $handle );
assertStyleEnqueued
Assert that a style is enqueued by handle.
$this->assertStyleEnqueued( string $handle );
assertStyleNotEnqueued
Assert that a style is not enqueued by handle.
$this->assertStyleNotEnqueued( string $handle );
assertScriptRegistered
Assert that a script is registered.
$this->assertScriptRegistered( string $handle );
assertStyleRegistered
Assert that a style is registered.
$this->assertStyleRegistered( string $handle );
Block Assertions
Powered by Match Blocks.
assertStringMatchesBlock
Assert that a string matches a block with the given optional attributes.
$this->assertStringMatchesBlock( $string, $args );
assertStringNotMatchesBlock
Assert that a string does not match a block with the given optional attributes.
$this->assertStringNotMatchesBlock( $string, $args );
assertStringHasBlock
Assert that a string has a block with the given optional attributes.
$this->assertStringHasBlock( $string, $block_name, $attrs );
assertStringNotHasBlock
Assert that a string does not have a block with the given optional attributes.
$this->assertStringNotHasBlock( $string, $block_name, $attrs );
assertPostHasBlock
Assert that a post has a block in its content with the given optional attributes.
$this->assertPostHasBlock( $post, $block_name, $attrs );
assertPostNotHasBlock
Assert that a post does not have a block in its content with the given optional attributes.
$this->assertPostNotHasBlock( $post, $block_name, $attrs );
Mail Assertions
assertMailSent
Assert that an email was sent to the given address or callback.
$this->assertMailSent( 'example@example.com' );
You can also pass a callback to assert that an email was sent to any address
matching the callback. The callback will be passed an instance of
\Mantle\Testing\Mail\Mail_Message
.
use Mantle\Testing\Mail\Mail_Message;
$this->assertMailSent(
fn ( Mail_Message $message ) => $message->to === 'example@example.com' && $message->subject === 'Hello, world!',
);
assertMailNotSent
Assert that an email was not sent to the given address or callback.
$this->assertMailNotSent( 'example@example.com' );
You can also pass a callback to assert that an email was not sent to any address
matching the callback. The callback will be passed an instance of
\Mantle\Testing\Mail\Mail_Message
.
use Mantle\Testing\Mail\Mail_Message;
$this->assertMailNotSent(
fn ( Mail_Message $message ) => $message->to === 'example@example.com,
);
assertMailSentCount
Assert that the given number of emails were sent to the given address or any address matching the callback.
$this->assertMailSentCount( 3 );
$this->assertMailSentCount( 3, 'example@example.com' );
$this->assertMailSentCount( 3, fn ( Mail_Message $message ) => $message->to === 'example@example.com' );
HTTP Test Assertions
See HTTP Testing for a full list of HTTP test assertions.