How do I mock HTTP requests for PHPUnit?

If you take a look at WP_HTTP->request() (which all related functions wrap) it provides a filter hook for the purpose of overriding making a request in favor of returning arbitrary data as response: // Allow plugins to short-circuit the request $pre = apply_filters( ‘pre_http_request’, false, $r, $url ); if ( false !== $pre ) return … Read more

Unit testing in the WordPress backend (is_admin() is true)

According to this test, you use set_current_screen() to navigate to one of these in the setUp method. Alas, none of this is apparent if you look at the tremendously-helpful reference page for get_current_screen()… Example: <?php class AxisSetupTest extends WP_UnitTestCase { /** * @covers AxisWP::__construct */ function test_constructor() { // Assert // Admin $this->assertInternalType(‘integer’, has_action( ‘admin_enqueue_scripts’, … Read more

Testing hooks callback

Test in isolation When developing a plugin, the best way to test it is without loading the WordPress environment. If you write code that can be easily tested without WordPress, your code becomes better. Every component that is unit tested, should be tested in isolation: when you test a class, you only have to test … Read more