Skip to content

Start here ​

This package runs selected WordPress core code inside ordinary PHPUnit tests.

Install the runtime ​

Use the package line that matches the WordPress version supported by your plugin. See WordPress versions for the supported lines and how package versioning works:

bash
composer require --dev doiftrue/unitest-wp-copy:7.1.*
composer require --dev 10up/wp_mock

For example, 7.1.* contains code copied from WordPress 7.1.

Add the test bootstrap ​

Create tests/bootstrap.php:

php
require_once dirname( __DIR__ ) . '/vendor/autoload.php';

\Unitest_WP_Copy\WP_Runtime::boot();
\WP_Mock::bootstrap();

WARNING

The order matters: "Unitest WP Copy" defines the WordPress functions, then WP_Mock adds mock handlers for supported functions.

Check available functions and classes ​

See SYMBOLS-INFO.md:

text
vendor/doiftrue/unitest-wp-copy/SYMBOLS-INFO.md

Write a test ​

php
use WP_Mock\Tools\TestCase;

final class CommentTest extends TestCase {

	public function test_renders_safe_html(): void {
		$html = wpautop(
			make_clickable(
				wp_kses_post(
					'Visit https://example.com <script>alert(1)</script> <b>today</b>'
				)
			)
		);

		self::assertStringNotContainsString( '<script>', $html );
		self::assertStringContainsString( '<a href="https://example.com"', $html );
		self::assertStringContainsString( '<b>today</b>', $html );
	}
}

INFO

This test runs the real WordPress implementations of wp_kses_post(), make_clickable(), and wpautop(). The WP_Mock test case handles setup and cleanup automatically.

Mock a WordPress boundary ​

INFO

Only functions listed as mockable support this. See SYMBOLS-INFO.md.

Some runtime functions can be controlled through WP_Mock:

php
\WP_Mock::userFunction( 'is_multisite' )->andReturn( true );

self::assertTrue( is_multisite() );

Configure the runtime ​

Runtime configuration has two parts:

  • Constants — immutable filesystem paths, URLs, environment type, debug mode, and other values defined before bootstrap.
  • Options — mutable site and network values stored in memory, including defaults, lookup order, mocking, and cleanup.

Released under the MIT License.