alleyinteractive/pest-plugin-wordpress

WordPress Pest Integration

v0.5.0 2023-12-08 17:54 UTC

This package is auto-updated.

Last update: 2024-06-11 23:18:57 UTC


README

Supports integrating Pest with your WordPress code base through the Mantle Framework. Read about the Mantle Testing Framework here.

If you want to start testing your application with Pest, visit the main Pest Repository.

Pest was created by Nuno Maduro under the Sponsorware license. It got open-sourced and is now licensed under the MIT license.

Overview

The WordPress Pest Plugin allows WordPress to be tested using the Pest testing framework. Tests can be written in a very simple manner to 'bring the joy of testing to PHP'.

Example Test

Getting Started

The WordPress Pest Plugin does not require the Mantle Framework to be used on your site (though having the framework greatly enhances your ability to use Pest).

Install the WordPress plugin via the Composer package manager:

composer require alleyinteractive/pest-plugin-wordpress --dev

Setting up Pest

Note: if you are using the Mantle Framework, skip ahead to Using With the Mantle Framework.

Let's get started integrating your project with Mantle and Pest. This guide assumes that your project is placed inside an existing WordPress installation as a plugin or a theme. Read more information about setting up the test framework here.

The default configuration will install WordPress using a localhost database named wordpress_unit_tests with the username/password pair of root/root. All constants can be overridden using the wp-tests-config.php file or your unit test's bootstrap file.

Assuming you do not have Pest setup in your project, create a tests folder and run the pest --init command:

./vendor/bin/pest --init

Replacing the Pest Test Case

Open up the tests/Pest.php file in your project the above command created for you. Look for a line that looks like this:

// uses(Tests\TestCase::class)->in('Feature');

Replace that with the following:

uses(\Mantle\Testkit\Test_Case::class)->in(__DIR__);

// Install WordPress via Mantle.
\Mantle\Testing\install();

Mantle also uses composer-wordpress-autoloader so you will need to load vendor/wordpress-autoload.php instead of just vendor/autoload.php.

Finally, you can run Pest directly from the command line:

./vendor/bin/pest

You can now use the Mantle Testing Framework with Pest to test your WordPress plugin with ease and simplicity. Your IDE will be able to type-hint you as well to allow you to use the testing framework.

Using with the Mantle Framework

Requiring the WordPress Pest Plugin on an existing Mantle project will allow you to install Pest with a few commands.

Install the WordPress plugin via the Composer package manager and run the mantle pest:install WP-CLI command:

composer require alleyinteractive/pest-plugin-wordpress --dev

wp mantle pest:install

That's it! Pest is installed successfully on your Mantle project. You can run your pest tests through Pest now:

./vendor/bin/pest

Mantle can also generate a Pest-friendly test by running the pest:test WP-CLI command:

wp mantle pest:test <TestName>

Writing Tests

More information can be found on the Testing Framework page.

use function Pest\PestPluginWordPress\from;
use function Pest\PestPluginWordPress\get;

it( 'should load the homepage', function () {
    get( '/' )
        ->assertStatus( 200 )
        ->assertSee( 'home' );
} );

it( 'should load with a referrer', function () {
    from( 'https://laravel.com/' )
        ->get( '/' )
        ->assertStatus( 200 );
});