PHP Unit Test a Plugin AJAX Functionality with WP_Ajax_UnitTestCase
-
I have a plugin that adds registers a few AJAX callbacks as follows:
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { // Hooks here. For instance: add_action( 'wp_ajax_david_example', 'david_example' ); }//end ifwhere
david_exampleis:function david_example() { wp_send_json( 'Done!' ); }//end david_example()Now I want to create a PHP Unit Test to check that all AJAX callbacks work as expected, so I create the following test in
test-ajax.php:class SampleTest extends WP_Ajax_UnitTestCase { public function test_sample() { try { $this->_handleAjax( 'david_example' ); } catch ( WPAjaxDieContinueException $e ) { }//end try $this->assertTrue( isset( $e ) ); $this->assertEquals( '"Done!"', $e->_last_response ); }//end test_sample() }//end classUnfortunately, this test doesn’t work because
$this->assertTrue( isset( $e ) )fails –$eis never set. As far as I know, this occurs because the plugin was initialized before the constantDOING_AJAXwas set and, therefore, my AJAX callback was never registered.What am I doing wrong?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘PHP Unit Test a Plugin AJAX Functionality with WP_Ajax_UnitTestCase’ is closed to new replies.