{"id":15039747,"url":"https://github.com/automattic/wordbless","last_synced_at":"2025-05-15T10:02:46.246Z","repository":{"id":45859915,"uuid":"272457315","full_name":"Automattic/wordbless","owner":"Automattic","description":"WorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment","archived":false,"fork":false,"pushed_at":"2025-04-22T16:55:26.000Z","size":248,"stargazers_count":138,"open_issues_count":4,"forks_count":8,"subscribers_count":12,"default_branch":"trunk","last_synced_at":"2025-05-15T10:02:24.059Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Automattic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-15T14:15:03.000Z","updated_at":"2025-04-22T16:53:23.000Z","dependencies_parsed_at":"2024-06-18T13:37:20.079Z","dependency_job_id":"885deffe-af05-45c5-861a-c2a1913ade2f","html_url":"https://github.com/Automattic/wordbless","commit_stats":{"total_commits":54,"total_committers":10,"mean_commits":5.4,"dds":0.5925925925925926,"last_synced_commit":"a1fe6376b81e6d037190aa1a5dc684d51eb674cd"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fwordbless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fwordbless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fwordbless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Automattic%2Fwordbless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Automattic","download_url":"https://codeload.github.com/Automattic/wordbless/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319716,"owners_count":22051072,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:43:47.057Z","updated_at":"2025-05-15T10:02:40.105Z","avatar_url":"https://github.com/Automattic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WorDBless\n\n[![CI](https://github.com/Automattic/wordbless/workflows/CI/badge.svg)](https://github.com/Automattic/wordbless/actions?query=workflow%3ACI)\n\nWorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment\n\n## Usage\n\n### Require WorDBless\n\n```bash\ncomposer require --dev automattic/wordbless\n```\n\n### Make sure to copy db.php\n\nAdd this script to your `composer.json`:\n\n```json\n\t\"scripts\": {\n\t\t\"post-install-cmd\": \"WorDBless\\\\Composer\\\\InstallDropin::copy\",\n\t\t\"post-update-cmd\": \"WorDBless\\\\Composer\\\\InstallDropin::copy\"\n\t},\n```\n\nAlternatively, you can manually copy `src/dbless-wpdb.php` to the `wordpress` folder created in your project under `wp-content/db.php`.\n\n### Initialize it in your bootstrap file\n\nIn your PHP Unit bootstrap file add:\n\n```php\nrequire_once __DIR__ . '/../vendor/autoload.php'; // adjust the path as needed\n\n\\WorDBless\\Load::load();\n```\n\nThat's it! You can now use WordPress core functions in your tests!\n\n### Writing tests\n\nExtend the `BaseTestCase` in order to have all the setup and teardown in place.\n\n```php\nclass My_Tests extends \\WorDBless\\BaseTestCase {\n\n\tpublic function test_add() {\n\t\tadd_option( 'test', 123 );\n\t\t$this-\u003eassertEquals( 123, get_option( 'test' ) );\n\t}\n\n}\n```\nNote WorDBless uses `@before` and `@after` annotations rather than overriding PHPUnit's `setUp` and `tearDown` methods.\n\nIf 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.\n\n## What will work and what will not work?\n\nDisclaimer: This is still experimental, so all testing is very welcome.\n\nBasically any WordPress core function will work. Things like `wp_parse_args`, `add_query_arg`, etc.\n\nHooks and filters will work.\n\nMost of the things that uses the database WILL NOT work, unless you believe in magic.\n\n### What magic WorDBless adds?\n\n#### Options\n\nManipulating options will work. `get_option`, `update_option`, `add_option` and `delete_option` should work fine!\n\n#### Posts and Post meta\n\nManipulating (creating, updating, deleting) posts and attachments will work. Fetching a single post will also work. Here is a non-exaustive list of functions supported:\n\n* `wp_insert_post`\n* `wp_update_post`\n* `wp_delete_post`\n* `wp_trash_post`\n* `wp_untrash_post`\n* `get_post`\n* `get_post_meta`\n* `add_post_meta`\n* `update_post_meta`\n* `delete_post_meta`\n* `get_metadata_by_mid`\n* `update_metadata_by_mid`\n* `delete_metadata_by_mid`\n* `wp_insert_attachment`\n* `wp_get_attachment_image`\n* and almost anything related to the manipulation of one attachment\n\nNote: Fetching posts using `WP_Query` will not work (yet)! To fetch a post, use `get_post( $id )`.\n\n#### Users and capabilities\n\nYou can create, edit and delete users.\n\nHere is a non-exaustive list of functions supported:\n\n* `wp_insert_user`\n* `wp_update_user`\n* `wp_delete_user`\n* `get_userdata`\n* `new WP_User( $id )` to fetch a user\n* `user_can`\n* `current_user_can`\n* `set_current_user`\n* `get_current_user_id`\n* `wp_get_current_user`\n* `get_user_meta`\n* `update_user_meta`\n* `add_user_meta`\n* `delete_user_meta`\n\nPosts can be assigned to users and proper capabilities will be correctly checked. When deleting a user, reassigning posts to other user will also work.\n\nNote: Fetching users using `WP_Users_Query` will not work! To fetch a user, use `get_userdata()`, `get_user_by` or `WP_User` class.\n\n### Populating default options\n\nBy default, only `siteurl` and `home` options are populated with `http://example.org`.\n\nIf you want, you can add more options to be loaded by default. \n\nJust 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.\n\n## Examples\n\nHere's a simple example, using only few WordPress functions:\n\n[Jetpack Admin UI package](https://github.com/Automattic/jetpack/blob/master/projects/packages/admin-ui/tests/php)\n\nAnd here a more complex example, using WorDBless to test REST endpoints, create users and play with hooks:\n\n[Jetpack Backup package](https://github.com/Automattic/jetpack/blob/master/projects/packages/backup/tests/php)\n## Running our CI locally\n\nFirst [install phive](https://phar.io/#Install) globally on your computer.\n\nThen issue the following single command.\n\n```bash\ncomposer run-script ci\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomattic%2Fwordbless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fautomattic%2Fwordbless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fautomattic%2Fwordbless/lists"}