Mocking WP_CLI static methods in unit tests

So I am using PEST as my testing framework, and if I just define my mocks in the helpers, they won’t be called correctly, because the WPCLI will probably be autoloaded after that on each test. So I added beforeEach(function () { $wpCliMock = \Mockery::mock(‘alias:WP_CLI’); $wpCliMock ->shouldReceive(‘success’) ->andReturnArg(0); $wpCliMock ->shouldReceive(‘error’) ->andReturnArg(0); }); Inside my tests, … Read more

How can I link tests to GitHub issues?

I don’t know of a library, but you could probably build one using TracTickets as a guide and using GitHub’s API. I’m actually answering to say don’t do this. WordPress actually no longer uses this method of skipping tests. See ticket #30284. I quote from there: Our previous convention was to write unit tests to … Read more

How to do an unit test for the admin dashboard

There are basically three options: Load that file with the admin-side code on-the-fly in setUp() or setUpBeforeClass(), so it will be loaded when those tests run. Load that file before the test suite is loaded and run, like in a bootstrap.php file or something, so it will be loaded for all the tests. Separate your … Read more

Write integration test for rest_pre_serve_request

The question actually has nothing specific to wordpress, but maybe it is worth answering. There is simply no code of your own in that sample that can be/is worth testing, therefor there isn’t much to unit test. Lets look at the details. add_filter is integration with core code, remove_filter is integration with core code and … Read more