automattic/wordbless
Composer 安装命令:
composer require automattic/wordbless
包简介
WorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment
README 文档
README
WorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment
Usage
Require WorDBless
composer require --dev automattic/wordbless
Make sure to copy db.php
Add this script to your composer.json:
"scripts": { "post-install-cmd": "WorDBless\\Composer\\InstallDropin::copy", "post-update-cmd": "WorDBless\\Composer\\InstallDropin::copy" },
Alternatively, you can manually copy src/dbless-wpdb.php to the wordpress folder created in your project under wp-content/db.php.
Initialize it in your bootstrap file
In your PHP Unit bootstrap file add:
require_once __DIR__ . '/../vendor/autoload.php'; // adjust the path as needed \WorDBless\Load::load();
Optionally specify an install path
The WordPress Core installer package allows customizing the path that is going to be used to install WordPress into. You can specify it in your composer.json file:
"extra": { "wordpress-install-dir": "vendor/wordpress" }
Keep in mind that you will have to define the ABSPATH constant in your project before calling the Load::load() method. The value must point to the same directory as wordpress-install-dir and must end with a trailing slash.
require_once __DIR__ . '/vendor/autoload.php'; // adjust the path as needed // Point ABSPATH to the custom WordPress install directory before WorDBless loads. if ( ! \defined( 'ABSPATH' ) ) { \define( 'ABSPATH', __DIR__ . '/vendor/wordpress/' ); } \WorDBless\Load::load();
That's it! You can now use WordPress core functions in your tests!
Writing tests
Extend the BaseTestCase in order to have all the setup and teardown in place.
class My_Tests extends \WorDBless\BaseTestCase { public function test_add() { add_option( 'test', 123 ); $this->assertEquals( 123, get_option( 'test' ) ); } }
Note WorDBless uses @before and @after annotations rather than overriding PHPUnit's setUp and tearDown methods.
If you choose not to extend this base class, no problem. Just remember that WorDBless won't be set up or torn down for you. Check BaseTestCase::set_up_wordbless() and BaseTestCase::teardown_wordbless() to see how to do it for yourself.
What will work and what will not work?
Disclaimer: This is still experimental, so all testing is very welcome.
Basically any WordPress core function will work. Things like wp_parse_args, add_query_arg, etc.
Hooks and filters will work.
Most of the things that uses the database WILL NOT work, unless you believe in magic.
What magic WorDBless adds?
Options
Manipulating options will work. get_option, update_option, add_option and delete_option should work fine!
Posts and Post meta
Manipulating (creating, updating, deleting) posts and attachments will work. Fetching a single post will also work. Here is a non-exaustive list of functions supported:
wp_insert_postwp_update_postwp_delete_postwp_trash_postwp_untrash_postget_postget_post_metaadd_post_metaupdate_post_metadelete_post_metaget_metadata_by_midupdate_metadata_by_middelete_metadata_by_midwp_insert_attachmentwp_get_attachment_image- and almost anything related to the manipulation of one attachment
Note: Fetching posts using WP_Query will not work (yet)! To fetch a post, use get_post( $id ).
Users and capabilities
You can create, edit and delete users.
Here is a non-exaustive list of functions supported:
wp_insert_userwp_update_userwp_delete_userget_userdatanew WP_User( $id )to fetch a useruser_cancurrent_user_canset_current_userget_current_user_idwp_get_current_userget_user_metaupdate_user_metaadd_user_metadelete_user_meta
Posts can be assigned to users and proper capabilities will be correctly checked. When deleting a user, reassigning posts to other user will also work.
Note: Fetching users using WP_Users_Query will not work! To fetch a user, use get_userdata(), get_user_by or WP_User class.
Populating default options
By default, only siteurl and home options are populated with http://example.org.
If you want, you can add more options to be loaded by default.
Just declare a dbless_default_options() function in your bootstrap and make it return an array where the keys are option names and values, options values.
Examples
Here's a simple example, using only few WordPress functions:
And here a more complex example, using WorDBless to test REST endpoints, create users and play with hooks:
Running our CI locally
First install phive globally on your computer.
Then issue the following single command.
composer run-script ci
automattic/wordbless 适用场景与选型建议
automattic/wordbless 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.22M 次下载、GitHub Stars 达 145, 最近一次更新时间为 2020 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 automattic/wordbless 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 automattic/wordbless 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.22M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 147
- 点击次数: 11
- 依赖项目数: 31
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2020-07-07