Docs: Improve documentation for tests/phpunit/includes/testcase-ajax.php.

Props andizer, SergeyBiryukov.
Fixes #47568.

git-svn-id: https://develop.svn.wordpress.org/trunk@47041 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-01-05 15:31:10 +00:00
parent 5f600eb32a
commit cd5b7df8b8
2 changed files with 38 additions and 35 deletions

View File

@ -5,13 +5,14 @@ class WP_Tests_Exception extends PHPUnit_Framework_Exception {
} }
/** /**
* General exception for wp_die() * General exception for wp_die().
*/ */
class WPDieException extends Exception {} class WPDieException extends Exception {}
/** /**
* Exception for cases of wp_die(), for ajax tests. * Exception for cases of wp_die(), for Ajax tests.
* This means there was an error (no output, and a call to wp_die) *
* This means there was an error (no output, and a call to wp_die).
* *
* @package WordPress * @package WordPress
* @subpackage Unit Tests * @subpackage Unit Tests
@ -20,11 +21,11 @@ class WPDieException extends Exception {}
class WPAjaxDieStopException extends WPDieException {} class WPAjaxDieStopException extends WPDieException {}
/** /**
* Exception for cases of wp_die(), for ajax tests. * Exception for cases of wp_die(), for Ajax tests.
* This means execution of the ajax function should be halted, but the unit *
* test can continue. The function finished normally and there was not an * This means the execution of the Ajax function should be halted, but the unit test
* error (output happened, but wp_die was called to end execution) This is * can continue. The function finished normally and there was no error (output happened,
* used with WP_Ajax_Response::send * but wp_die was called to end execution). This is used with WP_Ajax_Response::send().
* *
* @package WordPress * @package WordPress
* @subpackage Unit Tests * @subpackage Unit Tests

View File

@ -1,12 +1,4 @@
<?php <?php
/**
* Ajax test cases
*
* @package WordPress
* @subpackage UnitTests
* @since 3.4.0
*/
/** /**
* Ajax test case class * Ajax test case class
* *
@ -17,14 +9,14 @@
abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase { abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
/** /**
* Last AJAX response. This is set via echo -or- wp_die. * Last Ajax response. This is set via echo -or- wp_die.
* *
* @var string * @var string
*/ */
protected $_last_response = ''; protected $_last_response = '';
/** /**
* List of ajax actions called via GET * List of Ajax actions called via GET.
* *
* @var array * @var array
*/ */
@ -40,14 +32,14 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
); );
/** /**
* Saved error reporting level * Saved error reporting level.
* *
* @var int * @var int
*/ */
protected $_error_level = 0; protected $_error_level = 0;
/** /**
* List of ajax actions called via POST * List of Ajax actions called via POST.
* *
* @var array * @var array
*/ */
@ -139,8 +131,9 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Set up the test fixture. * Sets up the test fixture.
* Override wp_die(), pretend to be ajax, and suppress E_WARNINGs *
* Overrides wp_die(), pretends to be Ajax, and suppresses E_WARNINGs.
*/ */
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -162,8 +155,9 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Tear down the test fixture. * Tears down the test fixture.
* Reset $_POST, remove the wp_die() override, restore error reporting *
* Resets $_POST, removes the wp_die() override, restores error reporting.
*/ */
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
@ -178,7 +172,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Clear login cookies, unset the current user * Clears login cookies, unsets the current user.
*/ */
public function logout() { public function logout() {
unset( $GLOBALS['current_user'] ); unset( $GLOBALS['current_user'] );
@ -189,7 +183,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Return our callback handler * Returns our callback handler
* *
* @return callback * @return callback
*/ */
@ -198,8 +192,10 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Handler for wp_die() * Handler for wp_die().
*
* Save the output for analysis, stop execution by throwing an exception. * Save the output for analysis, stop execution by throwing an exception.
*
* Error conditions (no output, just die) will throw <code>WPAjaxDieStopException( $message )</code> * Error conditions (no output, just die) will throw <code>WPAjaxDieStopException( $message )</code>
* You can test for this with: * You can test for this with:
* <code> * <code>
@ -211,7 +207,11 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
* $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' ); * $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' );
* </code> * </code>
* *
* @param string $message * @param string $message The message to set.
*
* @throws WPAjaxDieStopException Thrown to stop further execution.
* @throws WPAjaxDieContinueException Thrown to stop execution of the Ajax function,
* but continue the unit test.
*/ */
public function dieHandler( $message ) { public function dieHandler( $message ) {
$this->_last_response .= ob_get_clean(); $this->_last_response .= ob_get_clean();
@ -228,10 +228,11 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Switch between user roles * Switches between user roles.
* E.g. administrator, editor, author, contributor, subscriber
* *
* @param string $role * E.g. administrator, editor, author, contributor, subscriber.
*
* @param string $role The role to set.
*/ */
protected function _setRole( $role ) { protected function _setRole( $role ) {
$post = $_POST; $post = $_POST;
@ -241,11 +242,12 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
} }
/** /**
* Mimic the ajax handling of admin-ajax.php * Mimics the Ajax handling of admin-ajax.php.
* Capture the output via output buffering, and if there is any, store
* it in $this->_last_response.
* *
* @param string $action * Captures the output via output buffering, and if there is any,
* stores it in $this->_last_response.
*
* @param string $action The action to handle.
*/ */
protected function _handleAjax( $action ) { protected function _handleAjax( $action ) {