gin0115 / wpunit-helpers
A series of helper classes, functions and traits for testing with WPUnit for WordPress
Installs: 46 995
Dependents: 24
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 2
Requires
- php: >=7.1.0
- automattic/jetpack-constants: *
- pinkcrab/function-constructors: *
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: *
- php-stubs/wordpress-stubs: ^6.0 || ^5.9
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: ^6.1
- symfony/var-dumper: *
- szepeviktor/phpstan-wordpress: ^1.0
- vlucas/phpdotenv: ^5.4
- wp-coding-standards/wpcs: *
- wp-phpunit/wp-phpunit: ^6.1
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
- dev-main
- 1.1.1
- 1.1.0
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-develop
- dev-release/1.0.7
- dev-feature/resolve-dependencies
- dev-feature/gh18-object-tests-not-using-actual-static-properties
- dev-feature/gh21-add-comment-meta-data-to-meta-data-inspector
- dev-feature/add-plugin-installed
- dev-gin0115-menu-page-inspector-force-reset-at-initialisation
- dev-feature/finish-missing-docs
This package is auto-updated.
Last update: 2024-11-26 18:33:35 UTC
README
Collection of helper functions, classes and traits for using WPUnit.
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);
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);
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);
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');
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']);
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']
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.
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'], * ] * ] */
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.
Change log
- 1.1.1 - Updates dependencies, added in missing docs, missing tests and renamed the
array_map_with
toiterable_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
andplugin_active
toWP_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.