gin0115/wpunit-helpers

A series of helper classes, functions and traits for testing with WPUnit for WordPress


README

Collection of helper functions, classes and traits for using WPUnit.

Latest Stable Version Total Downloads License PHP Version Require GitHub contributors GitHub issues codecov Maintainability

Version

1.0.8

Setup

$ composer require --dev gin0115/wpunit-helpers

Meta Box Inspector

Check if meta boxes have been registered correctly, check all values and render the view callback.

$box = Meta_Box_Inspector::initialise()->find('my_meta_box_key');
$this->assertInstanceOf(Meta_Box_Entity::class, $box);
$this->assertEquals('My Title', $box->title);

Read More

Menu Page Inspector

Allows for the checking of added pages and sub pages. Can be searched to ensure pages are added as expected and can render the pages content, for intergration style tests. Allows for testing parent and child(sub) pages.

$page = Menu_Page_Inspector::initialise()->find_parent_page('parent_page_slug');
$this->assertInstanceOf(Menu_Page_Entity::class, $page);
$this->assertEquals('My Settings', $page->menu_title);

Read More

Meta Data Inspector

Allows for the checking of registered meta data, for either post, term, user, comment and any other custom meta type added.

$post_meta = Meta_Data_Inspector::initialise()->find_post_meta('post', 'my_key');
$this->assertInstanceOf(Meta_Data_Entity::class, $post_meta);
$this->assertEquals('This is my meta field', $post_meta->description);

Read More

WP Dependencies

Allows for the quick and simple installation of themes and plugins from remote sources.

WP_Dependencies::install_remote_plugin_from_zip(
    'https://the-url.tc/woocommerce.zip', 'path/to/test_wp/root/'
);
WP_Dependencies::activate_plugin('woocommerce/woocommerce.php');

Read More

Object (Reflection wrappers)

Reflection is super useful in testing, especially if you cant access internal properties and methods to create your tests. Or you need to mock parts of the process, which are otherwise not accessible (internal WP States etc). These also work on static methods/properties

//  Access protected & privates properties.
Objects::get_property($instnace, 'property');
// Set protected or private properties.
Objects::set_property($instnace, 'property', 'new value');
// Invoke private or protected method.
Objects::invoke_method($instance, 'method', ['the', 'args']);

Read More

Utils

A collection of functions that have no other real place.

// iterable_map_with allows array_map to be done with access to the key and as many other
// values you wish to pass.
$result = Utils::iterable_map_with( 

    function($key, $value, $spacer){
        return $key . $spacer . $value;
    }, 
    ['key1'=>'value1', 'key2' => 'value2'],
    ' -|- '

); 
var_dump($result); // ['key1 -|- value1', 'key2 -|- value2']

Read More

Output

There are a number of methods that can be used to capture output from a function or method. This is useful for testing output from a function or method.

Read More

Logable WPDB

This simple class which extends wpdb can be used for Application tests where you need to either mock the return from a WPDB call or log all wpdb calls made.

Supports all public and common methods.

$wpdb = new Logable_WPDB();

// Set a return value.
$wpdb->then_return = 1;

// Mock an insert 
$result = $wpdb->insert('table_name', ['col1'=>'value1'], ['%s']);

// Get back the set return value
var_dump($result);

// Access the useage log
$log = $wpdb->usage_log;
var_dump($log);
/**
 * ['insert' => 
 *   [0] => [
 *     'table' => 'table_name',
 *     'data' => ['col1'=>'value1'],
 *     'format' => ['%s'],
 *   ]
 * ]
 */

Read More

WP Unit TestCase Helper Traits

These traits are designed to be used with the WP Unit TestCase. They provide a number of helper functions to make testing easier.

Read More

Change log

  • 1.1.1 - Updates dependencies, added in missing docs, missing tests and renamed the array_map_with to iterable_map_with to better reflect the use case.
  • 1.1.0 - Replaced all instance of pipe() from Function_Constructors with compose() from Function_Constructors.
  • 1.0.7 - Updated dependencies for Function_Constructors and testing.
  • 1.0.6 - Added Logable WPDB, Extended Meta Data Inspector to make use of Comment Meta, Extended to PHP8.1 Support
  • 1.0.5 - Update dependencies for php8, also added plugin_installed and plugin_active to WP_Dependencies
  • 1.0.4 - Update all dependencies
  • 1.0.3 - Clear up issue with the errors found in 1.0.2 but not in dev
  • 1.0.2 - Uses menu_page_url for menu page urls and Menu_Page_Inspector given find_group function as the current naming is confusing
  • 1.0.1 - Added in Meta_Data_Inspector for checking all registered meta data.
  • 1.0.0 - Most in place now, still needs more docs and some extra tests on output.