gin0115 / wpunit-helpers
为 WordPress 使用 WPUnit 进行测试的一系列辅助类、函数和特性
1.1.1
2023-02-26 14:46 UTC
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-09-26 18:08:06 UTC
README
WPUnit 的辅助函数、类和特性的集合。
版本
1.0.8
设置
$ composer require --dev gin0115/wpunit-helpers
Meta Box Inspector
检查元框是否已正确注册,检查所有值并渲染视图回调。
$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
允许检查添加的页面和子页面。可以搜索以确保页面按预期添加,并可以渲染页面内容,用于集成风格测试。允许测试父页面和子页面。
$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
允许检查注册的元数据,无论是文章、术语、用户、评论还是添加的任何其他自定义元类型。
$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 依赖项
允许从远程来源快速简单地安装主题和插件。
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');
对象(反射包装器)
反射在测试中非常有用,尤其是在无法访问内部属性和方法来创建测试时。或者您需要模拟其他方式无法访问的过程的部分(内部 WP 状态等)。这些也适用于静态方法和属性
// 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
一组没有其他实际位置的函数。
// 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']
输出
有许多方法可以用来捕获函数或方法的输出。这对于测试函数或方法的输出非常有用。
Logable WPDB
这个扩展 wpdb
的简单类可用于应用程序测试,其中您需要模拟 WPDB 调用的返回值或记录所有 wpdb 调用。
支持所有公共和常用方法。
$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
这些特性是为与 WP Unit TestCase 一起使用而设计的。它们提供了一些辅助函数,以使测试更加容易。
变更日志
- 1.1.1 - 更新依赖项,添加缺失的文档,缺失的测试,并将
array_map_with
重命名为iterable_map_with
以更好地反映使用情况。 - 1.1.0 - 用 Function_Constructors 中的 compose() 替换了所有 pipe() 的实例。
- 1.0.7 - 更新 Function_Constructors 和测试的依赖项。
- 1.0.6 - 添加 Logable WPDB,扩展 Meta Data Inspector 以使用 Comment Meta,扩展到 PHP8.1 支持
- 1.0.5 - 更新 php8 的依赖项,并为
WP_Dependencies
添加了plugin_installed
和plugin_active
。 - 1.0.4 - 更新所有依赖项
- 1.0.3 - 清理在 1.0.2 中找到但不在 dev 中的错误
- 1.0.2 - 使用 menu_page_url 为菜单页面 URL,并给 Menu_Page_Inspector 提供了 find_group 函数,因为当前命名方式令人困惑
- 1.0.1 - 添加 Meta_Data_Inspector 以检查所有注册的元数据。
- 1.0.0 - 现在已经到位,但仍需要更多文档和一些额外的输出测试。