From fe7295b8601dda0922d176cbc82872515d6e9769 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 6 Aug 2021 21:38:45 +0000 Subject: [PATCH] Tests: Replace `expectException()` for PHP native errors with calls to the dedicated PHPUnit 8.4+ methods. The old manner of testing these is soft deprecated as of PHPUnit 8.4, hard deprecated as of PHPUnit 9.0 and will be removed in PHPUnit 10.0. These dedicated methods introduced in PHPUnit 8.4 should be used as an alternative: * `expectDeprecation()` * `expectDeprecationMessage()` * `expectDeprecationMessageMatches()` * `expectNotice()` * `expectNoticeMessage()` * `expectNoticeMessageMatches()` * `expectWarning()` * `expectWarningMessage()` * `expectWarningMessageMatches()` * `expectError()` * `expectErrorMessage()` * `expectErrorMessageMatches()` These new PHPUnit methods are all polyfilled by the PHPUnit Polyfills and switching to these will future-proof the tests some more. References: * https://github.com/sebastianbergmann/phpunit/blob/8.4.3/ChangeLog-8.4.md#840---2019-10-04 * https://github.com/sebastianbergmann/phpunit/issues/3775 Follow-up to [51559-51562]. Props jrf. See #46149. git-svn-id: https://develop.svn.wordpress.org/trunk@51563 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/compat.php | 2 +- tests/phpunit/tests/dependencies/scripts.php | 8 ++++++-- tests/phpunit/tests/locale.php | 4 ++-- .../tests/rest-api/rest-block-directory-controller.php | 2 +- tests/phpunit/tests/rest-api/rest-plugins-controller.php | 2 +- tests/phpunit/tests/rest-api/rest-schema-sanitization.php | 4 ++-- tests/phpunit/tests/rest-api/rest-schema-validation.php | 4 ++-- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php index 766b8fe545..b9a6872bff 100644 --- a/tests/phpunit/tests/compat.php +++ b/tests/phpunit/tests/compat.php @@ -180,7 +180,7 @@ EOT; } function test_json_encode_decode() { - $this->expectException( 'PHPUnit_Framework_Error_Deprecated' ); + $this->expectDeprecation(); require_once ABSPATH . WPINC . '/class-json.php'; $json = new Services_JSON(); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 75d8ea0416..428e3bfca4 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -1424,9 +1424,13 @@ JS; public function test_wp_localize_script_data_formats( $l10n_data, $expected, $warning = false ) { if ( $warning ) { if ( PHP_VERSION_ID < 80000 ) { - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); + $this->expectWarning(); } else { - // As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now). + /* + * As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now). + * Once the PHPUnit version constraints have been widened and a _supported_ PHPUnit version is + * used to run the tests on PHP 8.x, this should be changed to `$this->expectError()`. + */ $this->expectException( 'Error' ); } } diff --git a/tests/phpunit/tests/locale.php b/tests/phpunit/tests/locale.php index 449471c5d5..47d657a942 100644 --- a/tests/phpunit/tests/locale.php +++ b/tests/phpunit/tests/locale.php @@ -27,9 +27,9 @@ class Tests_Locale extends WP_UnitTestCase { public function test_get_weekday_undefined_index() { if ( PHP_VERSION_ID >= 80000 ) { - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); + $this->expectWarning(); } else { - $this->expectException( 'PHPUnit_Framework_Error_Notice' ); + $this->expectNotice(); } $this->locale->get_weekday( 7 ); diff --git a/tests/phpunit/tests/rest-api/rest-block-directory-controller.php b/tests/phpunit/tests/rest-api/rest-block-directory-controller.php index 122c44e3e2..ef70ecb319 100644 --- a/tests/phpunit/tests/rest-api/rest-block-directory-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-directory-controller.php @@ -89,7 +89,7 @@ class WP_REST_Block_Directory_Controller_Test extends WP_Test_REST_Controller_Te $this->prevent_requests_to_host( 'api.wordpress.org' ); - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); + $this->expectWarning(); $response = rest_do_request( $request ); $this->assertErrorResponse( 'plugins_api_failed', $response, 500 ); } diff --git a/tests/phpunit/tests/rest-api/rest-plugins-controller.php b/tests/phpunit/tests/rest-api/rest-plugins-controller.php index 3e2873f55c..b9051115c4 100644 --- a/tests/phpunit/tests/rest-api/rest-plugins-controller.php +++ b/tests/phpunit/tests/rest-api/rest-plugins-controller.php @@ -536,7 +536,7 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase { $this->prevent_requests_to_host( 'api.wordpress.org' ); - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); + $this->expectWarning(); $response = rest_do_request( $request ); $this->assertErrorResponse( 'plugins_api_failed', $response, 500 ); } diff --git a/tests/phpunit/tests/rest-api/rest-schema-sanitization.php b/tests/phpunit/tests/rest-api/rest-schema-sanitization.php index a5c277ac04..7b3c076c6e 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-sanitization.php +++ b/tests/phpunit/tests/rest-api/rest-schema-sanitization.php @@ -457,9 +457,9 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase { */ public function test_format_validation_is_applied_if_missing_type() { if ( PHP_VERSION_ID >= 80000 ) { - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); // For the undefined index. + $this->expectWarning(); // For the undefined index. } else { - $this->expectException( 'PHPUnit_Framework_Error_Notice' ); + $this->expectNotice(); // For the undefined index. } $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' ); diff --git a/tests/phpunit/tests/rest-api/rest-schema-validation.php b/tests/phpunit/tests/rest-api/rest-schema-validation.php index 2fab13fe0a..58c5452de9 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-validation.php +++ b/tests/phpunit/tests/rest-api/rest-schema-validation.php @@ -157,9 +157,9 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { */ public function test_format_validation_is_applied_if_missing_type() { if ( PHP_VERSION_ID >= 80000 ) { - $this->expectException( 'PHPUnit_Framework_Error_Warning' ); // For the undefined index. + $this->expectWarning(); // For the undefined index. } else { - $this->expectException( 'PHPUnit_Framework_Error_Notice' ); + $this->expectNotice(); // For the undefined index. } $this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );