Tests: Consistently use the expectException() method instead of the older @expectedException annotation.

See https://thephp.cc/news/2016/02/questioning-phpunit-best-practices

The method is available since PHPUnit 5.2, and WordPress currently supports PHPUnit 5.4 as the minimum version.

Follow-up to [48993].

See #51344.

git-svn-id: https://develop.svn.wordpress.org/trunk@48996 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-09-18 13:22:22 +00:00
parent f78acee893
commit d46af0956d
10 changed files with 22 additions and 27 deletions

View File

@ -10,11 +10,9 @@ require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
* @group ajax
*/
class Tests_Ajax_Delete_Plugin extends WP_Ajax_UnitTestCase {
/**
* @expectedException WPAjaxDieStopException
* @expectedExceptionMessage -1
*/
public function test_missing_nonce() {
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'delete-plugin' );
}

View File

@ -10,11 +10,9 @@ require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
* @group ajax
*/
class Tests_Ajax_Update_Plugin extends WP_Ajax_UnitTestCase {
/**
* @expectedException WPAjaxDieStopException
* @expectedExceptionMessage -1
*/
public function test_missing_nonce() {
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'update-plugin' );
}

View File

@ -52,10 +52,9 @@ class Tests_Comment_WpAllowComment extends WP_UnitTestCase {
$this->assertSame( 1, $result );
}
/**
* @expectedException WPDieException
*/
public function test_die_as_duplicate_if_comment_author_name_and_emails_match() {
$this->expectException( 'WPDieException' );
$now = time();
$comment_data = array(
'comment_post_ID' => self::$post_id,

View File

@ -179,10 +179,9 @@ EOT;
$this->assertSame( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
function test_json_encode_decode() {
$this->expectException( 'PHPUnit_Framework_Error_Deprecated' );
require_once ABSPATH . WPINC . '/class-json.php';
$json = new Services_JSON();
// Super basic test to verify Services_JSON is intact and working.

View File

@ -524,11 +524,10 @@ class Tests_DB extends WP_UnitTestCase {
$this->assertEmpty( $wpdb->check_database_version() );
}
/**
* @expectedException WPDieException
*/
function test_bail() {
global $wpdb;
$this->expectException( 'WPDieException' );
$wpdb->bail( 'Database is dead.' );
}

View File

@ -24,12 +24,13 @@ class Tests_Formatting_Redirect extends WP_UnitTestCase {
* @ticket 44317
*
* @dataProvider get_bad_status_codes
* @expectedException WPDieException
*
* @param string $location The path or URL to redirect to.
* @param int $status HTTP response status code to use.
*/
public function test_wp_redirect_bad_status_code( $location, $status ) {
$this->expectException( 'WPDieException' );
wp_redirect( $location, $status );
}

View File

@ -268,17 +268,18 @@ class Tests_TestHelpers extends WP_UnitTestCase {
/**
* @ticket 36166
* @expectedException WPDieException
*/
public function test_die_handler_should_handle_wp_error() {
$this->expectException( 'WPDieException' );
wp_die( new WP_Error( 'test', 'test' ) );
}
/**
* @ticket 46813
* @expectedException WPDieException
*/
public function test_die_handler_should_not_cause_doing_it_wrong_notice_without_wp_query_set() {
$this->expectException( 'WPDieException' );
unset( $GLOBALS['wp_query'] );
wp_die();

View File

@ -101,17 +101,17 @@ class Tests_Option_Option extends WP_UnitTestCase {
/**
* @ticket 23289
* @expectedException WPDieException
*/
function test_special_option_name_alloption() {
$this->expectException( 'WPDieException' );
delete_option( 'alloptions' );
}
/**
* @ticket 23289
* @expectedException WPDieException
*/
function test_special_option_name_notoptions() {
$this->expectException( 'WPDieException' );
delete_option( 'notoptions' );
}

View File

@ -163,7 +163,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
public function test_rejects_remove_requests() {
$request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
$this->setExpectedException( 'WPDieException' );
$this->expectException( 'WPDieException' );
$this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
wp_privacy_generate_personal_data_export_file( $request_id );
}
@ -174,7 +174,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
* @ticket 44233
*/
public function test_invalid_request_id() {
$this->setExpectedException( 'WPDieException' );
$this->expectException( 'WPDieException' );
$this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
wp_privacy_generate_personal_data_export_file( 123456789 );
}
@ -194,7 +194,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
)
);
$this->setExpectedException( 'WPDieException' );
$this->expectException( 'WPDieException' );
$this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' );
wp_privacy_generate_personal_data_export_file( $request_id );
}
@ -208,7 +208,7 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
// Create a file with the folder name to ensure the function cannot create a folder.
touch( untrailingslashit( self::$exports_dir ) );
$this->setExpectedException( 'WPDieException' );
$this->expectException( 'WPDieException' );
$this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' );
wp_privacy_generate_personal_data_export_file( self::$export_request_id );
}

View File

@ -256,7 +256,7 @@ class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCa
* @param string $expected_output The expected string exception output.
*/
private function _setup_expected_failure( $expected_output ) {
$this->setExpectedException( 'WPDieException' );
$this->expectException( 'WPDieException' );
$this->expectOutputString( $expected_output );
}