Why?
WP_Mock and Brain Monkey are useful for checking whether code called a WordPress function with the expected arguments. They replace that function with a test double, so they do not test its real WordPress behavior.
This package runs selected original WordPress functions and classes in memory. It needs no WordPress installation, database, or web server. Tests can use real sanitization, escaping, block parsing, asset registries, hooks, and REST dispatch.
- WP_Mock or Brain Monkey — verify calls and mock WordPress boundaries.
- Unitest WP Copy — verify results produced by selected WordPress code.
- WordPress PHPUnit Test Suite — test code that needs a database, filesystem, or full WordPress lifecycle.
The tools can work together: use real logic where possible, mock external boundaries, and use integration tests only when a full installation is needed.
The following examples show what you can with this package (Unitest WP Copy).
Example 1: Test sanitization, escaping, and XSS protection
The application renders user input with WordPress functions:
function render_profile_card( string $name, string $bio, string $url ): string {
return sprintf(
'<article data-user="%s"><h2>%s</h2><div>%s</div><a href="%s">Website</a></article>',
esc_attr( sanitize_key( $name ) ),
esc_html( sanitize_text_field( $name ) ),
wp_kses_post( $bio ),
esc_url( $url )
);
}The test checks the final HTML, not only the function calls:
public function test_renders_safe_profile_card(): void {
$html = render_profile_card(
' Alice <script>alert(1)</script> ',
'<strong>Developer</strong><script>alert(2)</script>',
'javascript:alert(3)'
);
self::assertStringContainsString( '<h2>Alice</h2>', $html );
self::assertStringContainsString( '<strong>Developer</strong>', $html );
self::assertStringNotContainsString( '<script', $html );
self::assertStringNotContainsString( 'javascript:', $html );
}This covers real sanitization, KSES, escaping, and URL protocol filtering.
Example 2: Test block markup without the block editor
The application reads the structure of a callout block:
function get_callout_structure( string $content ): array {
$group = parse_blocks( $content )[0];
return [
'name' => $group['blockName'],
'class_name' => $group['attrs']['className'] ?? '',
'children' => array_column( $group['innerBlocks'], 'blockName' ),
];
}The test checks the names, attributes, and nesting returned by the WordPress block parser:
public function test_reads_the_callout_structure(): void {
$structure = get_callout_structure( <<<'HTML'
<!-- wp:group {"className":"callout"} -->
<!-- wp:heading {"level":3} --><h3>Important</h3><!-- /wp:heading -->
<!-- wp:paragraph --><p>Read this first.</p><!-- /wp:paragraph -->
<!-- /wp:group -->
HTML
);
self::assertSame( 'core/group', $structure['name'] );
self::assertSame( 'callout', $structure['class_name'] );
self::assertSame( [ 'core/heading', 'core/paragraph' ], $structure['children'] );
}This is useful for block migrations, transforms, and validation.
Example 3: Test asset registration and queue state
The application registers and enqueues an editor script:
function enqueue_editor_assets(): void {
wp_register_script(
'acme-editor',
'/assets/editor.js',
[ 'wp-element', 'wp-i18n' ],
'1.4.0',
[ 'in_footer' => true, 'strategy' => 'defer' ]
);
wp_enqueue_script( 'acme-editor' );
}The test checks the in-memory WordPress script registry:
public function test_registers_the_editor_script(): void {
enqueue_editor_assets();
$script = wp_scripts()->registered['acme-editor'];
self::assertTrue( wp_script_is( 'acme-editor', 'registered' ) );
self::assertTrue( wp_script_is( 'acme-editor', 'enqueued' ) );
self::assertSame( [ 'wp-element', 'wp-i18n' ], $script->deps );
self::assertSame( 'defer', wp_scripts()->get_data( 'acme-editor', 'strategy' ) );
}The same approach works with styles and script modules.
Example 4: Test a REST route without making an HTTP request
The application registers a route with an integer argument:
function register_items_route(): void {
register_rest_route(
'acme/v1',
'/items/(?P<id>[^/]+)',
[
'methods' => 'GET',
'permission_callback' => '__return_true',
'callback' => static fn( WP_REST_Request $request ) => [
'id' => $request['id'],
],
'args' => [
'id' => [
'type' => 'integer',
'validate_callback' => 'rest_validate_request_arg',
],
],
]
);
}The test dispatches the route in memory and checks real argument validation:
public function test_rejects_an_invalid_rest_item_id(): void {
register_items_route();
$response = rest_do_request(
new WP_REST_Request( 'GET', '/acme/v1/items/not-a-number' )
);
self::assertSame( 400, $response->get_status() );
self::assertSame( 'rest_invalid_param', $response->get_data()['code'] );
}
protected function tearDown(): void {
unset( $GLOBALS['wp_rest_server'] );
parent::tearDown();
}See Test REST API code for more examples and runtime limits.
Know the boundary
Only symbols listed in SYMBOLS-INFO.md are available. Use a WordPress integration test when the behavior depends on a real database, filesystem, installed plugins or themes, or a full WordPress lifecycle.