10quality/wpmvc-addon-testsuite

Test suite for WordPress MVC (WPMVC) framework add-ons.

dev-main 2023-02-10 22:23 UTC

This package is auto-updated.

Last update: 2024-10-11 02:08:08 UTC


README

Install

Composer command to install as required-dev:

composer install 10quality/wpmvc-addon-testsuite --dev

Test Case Class

Instead of the default PHPUnit test case use the following:

use WPMVC\Addons\PHPUnit\TestCase;

class MyTest extends TestCase
{
    // Your test methods
}

This classs will allow you to use the following assertiong methods:

Example:

use WPMVC\Addons\PHPUnit\TestCase;

class MyTest extends TestCase
{
    public function testAction()
    {
        // Run
        do_action( 'init' );
        // Assert
        $this->assertDidAction( 'init' );
    }
}

Test your addon

You addon main class needs a WordPress MVC main class (bridge) instance to work correctly. The TestCase class include the method getBridgeMock() that allows you to test your addon mocking the Bridge class.

Example:

use WPMVC\Addons\PHPUnit\TestCase;
use MyAddon;

class MyAddonTest extends TestCase
{
    public function testInit()
    {
        // Prepare
        $bridge = $this->getBridgeMock();
        $addon = new MyAddon( $bridge );
        // Run
        $addon->init();
        // Assert
        $this->assertAddedAction( 'init' );
        $this->assertHasRegisterScript( 'my-js' );
    }
}

The example above tests the method init() of the addon class MyAddon, which receives the $bridge initialized as a mock. The example asserts that an action hook has been added and a script has been registered during the method call.

You can mock the Bridge for your own benefit:

use WPMVC\Addons\PHPUnit\TestCase;
use WPMVC\Addons\PHPUnit\Mocks\Brige;
use MyAddon;

class MyAddonTest extends TestCase
{
    public function testInit()
    {
        // Prepare
        $bridge = $this->getMockBuilder( Brige::class )
            ->disableOriginalConstructor()
            ->getMock();
        $addon = new MyAddon( $bridge );
        // Run
        $addon->init();
        // Assert
        $this->assertAddedAction( 'init' );
        $this->assertHasRegisterScript( 'my-js' );
    }
}

Reset test suite data

You can reset test suite data by calling the function wpmvc_addon_phpunit_reset() inside the setUp or tearDown methods.

WordPress core functions mocked

The following WordPress core functions are mocked and included through composer:

  • __
  • _e
  • add_action
  • add_filter
  • add_query_arg
  • add_submenu_page
  • admin_url
  • apply_filters
  • do_action
  • esc_attr
  • esc_html
  • esc_html_e
  • esc_url
  • get_filesystem_method
  • get_locale
  • get_stylesheet_directory
  • home_url
  • request_filesystem_credentials
  • sanitize_text_field
  • site_url
  • submit_button
  • wp_enqueue_script
  • wp_enqueue_style
  • wp_register_script
  • wp_register_style
  • WP_Filesystem

WordPress core constants mocked

The following constants are mocked through composer if they are not defined:

  • ABSPATH

WordPress core classes mocked

The following WordPress classes are mocked and included through composer:

  • WP_Filesystem