Our themes and plugins use hooks to run code when an event fires. We use WordPress’ functions to add or remove a hooked callback, fire the event, or check the status. How do you unit test those interactions without loading WordPress? Meet Brain Monkey. Brain Monkey gives you the power to unit test hooks by simulating WordPress. In this lab, you’ll walk through building unit tests for hooks.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Testing a Callback is Registered to an Action Hook
Testing a Filter Fired
Brain Monkey’s Hook Toolset
The Problem – Why We Need to Simulate WordPress
Testing Hooks with Brain Monkey
Testing a Action Fired
Testing a Filter Has a Callback
Testing a Callback is Registered to a Filter Hook
Beans Solves the “Removing a Hooked WordPress Action too Soon” Timing Issue
A common problem in WordPress is removing a hooked action (via remove_action) too soon, i.e. before the original action is added via add_action. The Beans framework solves this problem through its Actions API. It’s a major enhancement to the WordPress Plugin API. Let me show you.