Skip to main content
Add note about this being integration testing vs unit testing
Source Link
J.D.
  • 3.9k
  • 17
  • 36

If you use wp_die() you can utilize the tools included with WordPress's PHPUnit test suite. The WP_Ajax_UnitTestCase provides the _handleAjax() method that will hook into the actions called by wp_die() and throw an exception, which will prevent die() from being called. I've written a tutorial on how to use WP_Ajax_UnitTestCase, which explains all of the features it provides, but here is a basic example:

class My_Ajax_Test extends WP_Ajax_UnitTestCase {
 
    /**
     * Test that the callback saves the value for administrators.
     */
    public function test_some_option_is_saved() {
 
        // Fulfill requirements of the callback here...
        $_POST['_wpnonce'] = wp_create_nonce( 'my_nonce' );
        $_POST['option_value'] = 'yes';
 
        try {
            $this->_handleAjax( 'my_ajax_action' );
        } catch ( WPAjaxDieStopException $e ) {
            // We expected this, do nothing.
        }
 
        // Check that the exception was thrown.
        $this->assertTrue( isset( $e ) );

        // Check that the callback did whatever it is expected to do...
        $this->assertEquals( 'yes', get_option( 'some_option' ) );
    }
}

Note that technically speaking, this is integration testing rather than unit testing, as it tests a complete cross-section of the callback's functionality, not just a single unit. This is how WordPress does it, but depending on the complexity of your callback's code, you may also want to create true unit tests for it as well, probably abstracting portions of it out into other functions that you can mock.

If you use wp_die() you can utilize the tools included with WordPress's PHPUnit test suite. The WP_Ajax_UnitTestCase provides the _handleAjax() method that will hook into the actions called by wp_die() and throw an exception, which will prevent die() from being called. I've written a tutorial on how to use WP_Ajax_UnitTestCase, which explains all of the features it provides, but here is a basic example:

class My_Ajax_Test extends WP_Ajax_UnitTestCase {
 
    /**
     * Test that the callback saves the value for administrators.
     */
    public function test_some_option_is_saved() {
 
        // Fulfill requirements of the callback here...
        $_POST['_wpnonce'] = wp_create_nonce( 'my_nonce' );
        $_POST['option_value'] = 'yes';
 
        try {
            $this->_handleAjax( 'my_ajax_action' );
        } catch ( WPAjaxDieStopException $e ) {
            // We expected this, do nothing.
        }
 
        // Check that the exception was thrown.
        $this->assertTrue( isset( $e ) );

        // Check that the callback did whatever it is expected to do...
        $this->assertEquals( 'yes', get_option( 'some_option' ) );
    }
}

If you use wp_die() you can utilize the tools included with WordPress's PHPUnit test suite. The WP_Ajax_UnitTestCase provides the _handleAjax() method that will hook into the actions called by wp_die() and throw an exception, which will prevent die() from being called. I've written a tutorial on how to use WP_Ajax_UnitTestCase, which explains all of the features it provides, but here is a basic example:

class My_Ajax_Test extends WP_Ajax_UnitTestCase {
 
    public function test_some_option_is_saved() {
 
        // Fulfill requirements of the callback here...
        $_POST['_wpnonce'] = wp_create_nonce( 'my_nonce' );
        $_POST['option_value'] = 'yes';
 
        try {
            $this->_handleAjax( 'my_ajax_action' );
        } catch ( WPAjaxDieStopException $e ) {
            // We expected this, do nothing.
        }
 
        // Check that the exception was thrown.
        $this->assertTrue( isset( $e ) );

        // Check that the callback did whatever it is expected to do...
        $this->assertEquals( 'yes', get_option( 'some_option' ) );
    }
}

Note that technically speaking, this is integration testing rather than unit testing, as it tests a complete cross-section of the callback's functionality, not just a single unit. This is how WordPress does it, but depending on the complexity of your callback's code, you may also want to create true unit tests for it as well, probably abstracting portions of it out into other functions that you can mock.

Source Link
J.D.
  • 3.9k
  • 17
  • 36

If you use wp_die() you can utilize the tools included with WordPress's PHPUnit test suite. The WP_Ajax_UnitTestCase provides the _handleAjax() method that will hook into the actions called by wp_die() and throw an exception, which will prevent die() from being called. I've written a tutorial on how to use WP_Ajax_UnitTestCase, which explains all of the features it provides, but here is a basic example:

class My_Ajax_Test extends WP_Ajax_UnitTestCase {
 
    /**
     * Test that the callback saves the value for administrators.
     */
    public function test_some_option_is_saved() {
 
        // Fulfill requirements of the callback here...
        $_POST['_wpnonce'] = wp_create_nonce( 'my_nonce' );
        $_POST['option_value'] = 'yes';
 
        try {
            $this->_handleAjax( 'my_ajax_action' );
        } catch ( WPAjaxDieStopException $e ) {
            // We expected this, do nothing.
        }
 
        // Check that the exception was thrown.
        $this->assertTrue( isset( $e ) );

        // Check that the callback did whatever it is expected to do...
        $this->assertEquals( 'yes', get_option( 'some_option' ) );
    }
}