Unit testing PHP classes that add WordPress hooks can be difficult to test with phpunit. In this video, I show you how I do it. I structure my code so that each part of the process is in its own method and then I use Brain Monkey to mock the WordPress plugins API.
In this video, I write tests for a class that adds an action, and then in the callback for that action, conditionally adds a filter.
๐ฒ Video about using Mockery: https://www.youtube.com/watch?v=gKLuybs8rNU&list=PLMYaDTqH6yCLb8_AdlDR8Ci_3UZBwBiXQ&index=5
๐ฒ pre_get_posts documentation: https://developer.wordpress.org/reference/hooks/pre_get_posts/
๐ฒ Setting up tests with Brain Monkey: https://brain-wp.github.io/BrainMonkey/docs/wordpress-setup.html
๐ฒ Testing WordPress hooks with Brain Monkey: https://brain-wp.github.io/BrainMonkey/docs/wordpress-hooks-added.html
๐ฒ Unit testing classes that call functions from WordPress core: https://dev.to/shelob9/unit-testing-classes-that-call-functions-form-wordpress-core-3h5p
source