# Testing: Assertions

## Introduction[​](#introduction "Direct link to Introduction")

Mantle's Test Case provides some assertions above and beyond PHPUnit's, largely influenced by `WP_UnitTestCase`. Here's a run-through:

## `assertWPError` and `assertNotWPError`[​](#assertwperror-and-assertnotwperror "Direct link to assertwperror-and-assertnotwperror")

Assert the given item is/is not a `WP_Error`:

```php
$this->assertWPError( $actual, $message = '' );

$this->assertNotWPError( $actual, $message = '' );

```

## General Assertions[​](#general-assertions "Direct link to General Assertions")

### assertEqualFields[​](#assertequalfields "Direct link to assertEqualFields")

Asserts that the given fields are present in the given object:

```php
$this->assertEqualFields( $object, $fields );

```

### assertDiscardWhitespace[​](#assertdiscardwhitespace "Direct link to assertDiscardWhitespace")

Asserts that two values are equal, with whitespace differences discarded:

```php
$this->assertDiscardWhitespace( $expected, $actual );

```

### assertEqualsIgnoreEOL[​](#assertequalsignoreeol "Direct link to assertEqualsIgnoreEOL")

Asserts that two values are equal, with EOL differences discarded:

```php
$this->assertEqualsIgnoreEOL( $expected, $actual );

```

### assertEqualSets[​](#assertequalsets "Direct link to assertEqualSets")

Asserts that the contents of two un-keyed, single arrays are equal, without accounting for the order of elements:

```php
$this->assertEqualSets( $expected, $actual );

```

### assertEqualSetsWithIndex[​](#assertequalsetswithindex "Direct link to assertEqualSetsWithIndex")

Asserts that the contents of two keyed, single arrays are equal, without accounting for the order of elements:

```php
$this->assertEqualSetsWithIndex( $expected, $actual );

```

### assertNonEmptyMultidimensionalArray[​](#assertnonemptymultidimensionalarray "Direct link to assertNonEmptyMultidimensionalArray")

Asserts that the given variable is a multidimensional array, and that all arrays are non-empty:

```php
$this->assertNonEmptyMultidimensionalArray( $array );

```

## WordPress Query Assertions[​](#wordpress-query-assertions "Direct link to WordPress Query Assertions")

### assertQueryTrue[​](#assertquerytrue "Direct link to assertQueryTrue")

Assert that the global WordPress query is true for a given set of properties and false for the rest:

```php
$this->assertQueryTrue( ...$prop );

```

### assertQueriedObjectId[​](#assertqueriedobjectid "Direct link to assertQueriedObjectId")

Assert that the global queried object ID is equal to the given ID:

```php
$this->assertQueriedObjectId( int $id );

```

### assertNotQueriedObjectId[​](#assertnotqueriedobjectid "Direct link to assertNotQueriedObjectId")

Assert that the global queried object ID is not equal to the given ID:

```php
$this->assertNotQueriedObjectId( int $id );

```

### assertQueriedObject[​](#assertqueriedobject "Direct link to assertQueriedObject")

Assert that the global queried object is equal to the given object:

```php
$this->assertQueriedObject( $object );

```

### assertNotQueriedObject[​](#assertnotqueriedobject "Direct link to assertNotQueriedObject")

Assert that the global queried object is not equal to the given object:

```php
$this->assertNotQueriedObject( $object );

```

### assertQueriedObjectNull[​](#assertqueriedobjectnull "Direct link to assertQueriedObjectNull")

Assert that the global queried object is null:

```php
$this->assertQueriedObjectNull();

```

## WordPress Post/Term Existence[​](#wordpress-postterm-existence "Direct link to WordPress Post/Term Existence")

### assertPostExists[​](#assertpostexists "Direct link to assertPostExists")

Assert if a post exists given a set of arguments.

```php
$this->assertPostExists( array $args );

```

### assertPostDoesNotExists[​](#assertpostdoesnotexists "Direct link to assertPostDoesNotExists")

Assert if a post doesn't exist given a set of arguments.

```php
$this->assertPostDoesNotExists( array $args );

```

### assertTermExists[​](#asserttermexists "Direct link to assertTermExists")

Assert if a term exists given a set of arguments.

```php
$this->assertTermExists( array $args );

```

### assertTermDoesNotExists[​](#asserttermdoesnotexists "Direct link to assertTermDoesNotExists")

Assert if a term doesn't exists given a set of arguments.

```php
$this->assertTermDoesNotExists( array $args );

```

### assertUserExists[​](#assertuserexists "Direct link to assertUserExists")

Assert if a user exists given a set of arguments.

```php
$this->assertUserExists( array $args );

```

### assertUserDoesNotExists[​](#assertuserdoesnotexists "Direct link to assertUserDoesNotExists")

Assert if a user doesn't exists given a set of arguments.

```php
$this->assertUserDoesNotExists( array $args );

```

### assertPostHasTerm[​](#assertposthasterm "Direct link to assertPostHasTerm")

Assert if a post has a specific term.

```php
$this->assertPostHasTerm( $post, $term );

```

### assertPostNotHasTerm[​](#assertpostnothasterm "Direct link to assertPostNotHasTerm")

Assert if a post does not have a specific term (aliased to `assertPostsDoesNotHaveTerm()`)

```php
$this->assertPostNotHasTerm( $post, $term );

```

## Asset Assertions[​](#asset-assertions "Direct link to Asset Assertions")

Assets that are registered and/or enqueued with WordPress can be asserted against using the following methods:

### assertScriptStatus[​](#assertscriptstatus "Direct link to assertScriptStatus")

Assert against the status of a script. `$status` accepts 'enqueued', 'registered', 'queue', 'to\_do', and 'done'.

```php
$this->assertScriptStatus( string $handle, string $status );

```

### assertStyleStatus[​](#assertstylestatus "Direct link to assertStyleStatus")

Assert against the status of a style. `$status` accepts 'enqueued', 'registered', 'queue', 'to\_do', and 'done'.

```php
$this->assertStyleStatus( string $handle, string $status );

```

### assertScriptEnqueued[​](#assertscriptenqueued "Direct link to assertScriptEnqueued")

Assert that a script is enqueued by handle.

```php
$this->assertScriptEnqueued( string $handle );

```

### assertScriptNotEnqueued[​](#assertscriptnotenqueued "Direct link to assertScriptNotEnqueued")

Assert that a script is not enqueued by handle.

```php
$this->assertScriptNotEnqueued( string $handle );

```

### assertStyleEnqueued[​](#assertstyleenqueued "Direct link to assertStyleEnqueued")

Assert that a style is enqueued by handle.

```php
$this->assertStyleEnqueued( string $handle );

```

### assertStyleNotEnqueued[​](#assertstylenotenqueued "Direct link to assertStyleNotEnqueued")

Assert that a style is not enqueued by handle.

```php
$this->assertStyleNotEnqueued( string $handle );

```

### assertScriptRegistered[​](#assertscriptregistered "Direct link to assertScriptRegistered")

Assert that a script is registered.

```php
$this->assertScriptRegistered( string $handle );

```

### assertStyleRegistered[​](#assertstyleregistered "Direct link to assertStyleRegistered")

Assert that a style is registered.

```php
$this->assertStyleRegistered( string $handle );

```

## Block Assertions[​](#block-assertions "Direct link to Block Assertions")

Powered by [Match Blocks](https://github.com/alleyinteractive/wp-match-blocks).

### assertStringMatchesBlock[​](#assertstringmatchesblock "Direct link to assertStringMatchesBlock")

Assert that a string matches a block with the given optional attributes.

```php
$this->assertStringMatchesBlock( $string, $args );

```

### assertStringNotMatchesBlock[​](#assertstringnotmatchesblock "Direct link to assertStringNotMatchesBlock")

Assert that a string does not match a block with the given optional attributes.

```php
$this->assertStringNotMatchesBlock( $string, $args );

```

### assertStringHasBlock[​](#assertstringhasblock "Direct link to assertStringHasBlock")

Assert that a string has a block with the given optional attributes.

```php
$this->assertStringHasBlock( $string, $block_name, $attrs );

```

### assertStringNotHasBlock[​](#assertstringnothasblock "Direct link to assertStringNotHasBlock")

Assert that a string does not have a block with the given optional attributes.

```php
$this->assertStringNotHasBlock( string $string, array|string $block_name, array $attrs = [] );

```

### assertPostHasBlock[​](#assertposthasblock "Direct link to assertPostHasBlock")

Assert that a post has a block in its content with the given optional attributes.

```php
use Mantle\Database\Model\Post;

$this->assertPostHasBlock( Post|\WP_Post $post, string|array $block_name, array $attrs = [] );

// Example:
$this->assertPostHasBlock( $post, 'core/paragraph' );
$this->assertPostHasBlock( $post, 'core/paragraph', [ 'placeholder' => 'Hello, world!' ] );

// Example with multiple blocks:
$this->assertPostHasBlock( $post, [ 'core/paragraph', 'core/image' ] );
$this->assertPostHasBlock( $post, [ 'core/paragraph', 'core/image' ], [ 'placeholder' => 'Hello, world!' ] );

```

### assertPostNotHasBlock[​](#assertpostnothasblock "Direct link to assertPostNotHasBlock")

Assert that a post does not have a block in its content with the given optional attributes.

```php
use Mantle\Database\Model\Post;

$this->assertPostNotHasBlock( Post|\WP_Post $post, string|array $block_name, array $attrs );

// Example:
$this->assertPostNotHasBlock( $post, 'core/paragraph' );
$this->assertPostNotHasBlock( $post, 'core/paragraph', [ 'placeholder' => 'Hello, world!' ] );

// Example with multiple blocks (matching any of the blocks will fail):
$this->assertPostNotHasBlock( $post, [ 'core/paragraph', 'core/image' ] );
$this->assertPostNotHasBlock( $post, [ 'core/paragraph', 'core/image' ], [ 'placeholder' => 'Hello, world!' ] );

```

## Mail Assertions[​](#mail-assertions "Direct link to Mail Assertions")

### assertMailSent[​](#assertmailsent "Direct link to assertMailSent")

Assert that an email was sent to the given address or callback.

```php
$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`.

```php
use Mantle\Testing\Mail\Mail_Message;

$this->assertMailSent(
  fn ( Mail_Message $message ) => $message->to === 'example@example.com' && $message->subject === 'Hello, world!',
);

```

### assertMailNotSent[​](#assertmailnotsent "Direct link to assertMailNotSent")

Assert that an email was not sent to the given address or callback.

```php
$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`.

```php
use Mantle\Testing\Mail\Mail_Message;

$this->assertMailNotSent(
  fn ( Mail_Message $message ) => $message->to === 'example@example.com,
);

```

## assertMailSentCount[​](#assertmailsentcount "Direct link to assertMailSentCount")

Assert that the given number of emails were sent to the given address or any address matching the callback.

```php
$this->assertMailSentCount( 3 );
$this->assertMailSentCount( 3, 'example@example.com' );
$this->assertMailSentCount( 3, fn ( Mail_Message $message ) => $message->to === 'example@example.com' );

```

## HTTP Test Assertions[​](#http-test-assertions "Direct link to HTTP Test Assertions")

See [HTTP Testing](/docs/testing/requests.md#available-assertions) for a full list of HTTP test assertions.
