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 Hooks with Brain Monkey
Testing a Action Fired
Testing a Filter Has a Callback
Testing a Callback is Registered to a Filter Hook
Brain Monkey’s Toolset Overview
Let’s dive into the all of the different functions (toolset) that are available for you in Brain Monkey. We’ll look at the documentation and its test suite.
Enable WordPress & Jump to Hook
To take full advantage of what PhpStorm has to offer, we need to enable WordPress integration. Once we do, then we get features like: Jump to where a hook (event) is being fired. Autocompletes for hook names. Filtering lookup for hooks.
Behind the Scenes of Redefining a Function
In the last episode, we talked about the philosophy of unit testing, i.e. testing in isolation without the function’s dependencies. In order to test in isolation, we need to know what each dependent function will do within the context of that test (i.e. given conditions) and then write a function that redefines the original in order to force it to do what it should. In this episode, you and I will dive into what is happening between the scenes in the PHP internals of user-defined names and memory). I’ll give you an overview to help you visualize how Brain Monkey[…]
The Problem – Why We Need to Simulate
Let’s explore both the philosophy of unit testing (i.e. testing in isolation) and the problem we face of redefining dependent functions in order to achieve a pure unit test.
Integration Bootstrap – Defining Constants
Notes | Transcript | Code | Playlist Notes Next, we need to define the constants we’ll need as the baseline for directory locations. Transcript Code