From d46af0956db0097b6f7d071fb225e4d68266a381 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 18 Sep 2020 13:22:22 +0000 Subject: [PATCH] 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 --- tests/phpunit/tests/ajax/DeletePlugin.php | 6 ++---- tests/phpunit/tests/ajax/UpdatePlugin.php | 6 ++---- tests/phpunit/tests/comment/wpAllowComment.php | 5 ++--- tests/phpunit/tests/compat.php | 5 ++--- tests/phpunit/tests/db.php | 5 ++--- tests/phpunit/tests/formatting/redirect.php | 3 ++- tests/phpunit/tests/includes/helpers.php | 5 +++-- tests/phpunit/tests/option/option.php | 4 ++-- .../privacy/wpPrivacyGeneratePersonalDataExportFile.php | 8 ++++---- .../privacy/wpPrivacyProcessPersonalDataExportPage.php | 2 +- 10 files changed, 22 insertions(+), 27 deletions(-) diff --git a/tests/phpunit/tests/ajax/DeletePlugin.php b/tests/phpunit/tests/ajax/DeletePlugin.php index 871e859a8f..aa22790a42 100644 --- a/tests/phpunit/tests/ajax/DeletePlugin.php +++ b/tests/phpunit/tests/ajax/DeletePlugin.php @@ -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' ); } diff --git a/tests/phpunit/tests/ajax/UpdatePlugin.php b/tests/phpunit/tests/ajax/UpdatePlugin.php index 81649da032..c9690d4f7b 100644 --- a/tests/phpunit/tests/ajax/UpdatePlugin.php +++ b/tests/phpunit/tests/ajax/UpdatePlugin.php @@ -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' ); } diff --git a/tests/phpunit/tests/comment/wpAllowComment.php b/tests/phpunit/tests/comment/wpAllowComment.php index 5c5cb4e088..5939675c67 100644 --- a/tests/phpunit/tests/comment/wpAllowComment.php +++ b/tests/phpunit/tests/comment/wpAllowComment.php @@ -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, diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php index 25d174f3ca..cc6cf805e5 100644 --- a/tests/phpunit/tests/compat.php +++ b/tests/phpunit/tests/compat.php @@ -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. diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 718474455a..c43de15141 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -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.' ); } diff --git a/tests/phpunit/tests/formatting/redirect.php b/tests/phpunit/tests/formatting/redirect.php index 93aaee3d5f..425be5c940 100644 --- a/tests/phpunit/tests/formatting/redirect.php +++ b/tests/phpunit/tests/formatting/redirect.php @@ -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 ); } diff --git a/tests/phpunit/tests/includes/helpers.php b/tests/phpunit/tests/includes/helpers.php index 451ccaa64c..c33cc2f83a 100644 --- a/tests/phpunit/tests/includes/helpers.php +++ b/tests/phpunit/tests/includes/helpers.php @@ -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(); diff --git a/tests/phpunit/tests/option/option.php b/tests/phpunit/tests/option/option.php index f461a7db98..0f67fa9490 100644 --- a/tests/phpunit/tests/option/option.php +++ b/tests/phpunit/tests/option/option.php @@ -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' ); } diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php index 36f7945180..7142cb75b9 100644 --- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php +++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php @@ -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 ); } diff --git a/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php b/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php index f676f1ebce..40d92f64d7 100644 --- a/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php +++ b/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php @@ -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 ); }