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:
composer require --dev doiftrue/unitest-wp-copy:7.1.*
composer require --dev 10up/wp_mockFor example, 7.1.* contains code copied from WordPress 7.1.
Add the test bootstrap
Create tests/bootstrap.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:
vendor/doiftrue/unitest-wp-copy/SYMBOLS-INFO.mdWrite a test
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:
\WP_Mock::userFunction( 'is_multisite' )->andReturn( true );
self::assertTrue( is_multisite() );Configure the runtime
Runtime configuration has two parts: