From 722ae3d4a072d0447c3cdda9c2869e7f5227a622 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 16 Aug 2020 02:14:29 +0200 Subject: [PATCH] TRAC 46149 - Assertions | WAIT FOR 47381 | Tests: replace `expectException()` for PHP native errors ... with calls to the dedicated PHPUnit 8.4.0+ 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.0. The dedicated `expectDeprecation()`, `expectDeprecationMessage()`, `expectDeprecationMessageMatches()`, `expectNotice()`, `expectNoticeMessage()`, `expectNoticeMessageMatches()`, `expectWarning()`, `expectWarningMessage()`, `expectWarningMessageMatches()`, `expectError()`, `expectErrorMessage()`, `expectErrorMessageMatches()` methods were introduced as replacement in PHPUnit 8.4 and should be used instead. These new PHPUnit methods are all polyfilled by the PHPUnit Polyfills and switching to these will future proof the tests some more. Ref: * https://github.com/sebastianbergmann/phpunit/blob/8.4.3/ChangeLog-8.4.md#840---2019-10-04 * https://github.com/sebastianbergmann/phpunit/issues/3775 --- tests/phpunit/tests/compat.php | 2 +- tests/phpunit/tests/dependencies/scripts.php | 13 ++++++++----- tests/phpunit/tests/locale.php | 4 ++-- .../rest-api/rest-block-directory-controller.php | 2 +- .../tests/rest-api/rest-plugins-controller.php | 2 +- .../tests/rest-api/rest-schema-sanitization.php | 4 ++-- .../tests/rest-api/rest-schema-validation.php | 4 ++-- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php index 2f93c4663d750..703f06011804d 100644 --- a/tests/phpunit/tests/compat.php +++ b/tests/phpunit/tests/compat.php @@ -180,7 +180,7 @@ function test_hash_hmac_output() { } 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 36cdbd13089ac..e2d0f6957bda8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -1423,12 +1423,15 @@ public function test_wp_enqueue_code_editor_when_simple_array_will_be_passed() { */ 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' ); - } else { - // As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now). + if ( PHP_VERSION_ID < 80000 ) { + $this->expectWarning(); + } else { + /* 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' ); - } + } } if ( ! is_array( $l10n_data ) ) { diff --git a/tests/phpunit/tests/locale.php b/tests/phpunit/tests/locale.php index 0ddc7575e9df2..252ea2d87a85a 100644 --- a/tests/phpunit/tests/locale.php +++ b/tests/phpunit/tests/locale.php @@ -27,9 +27,9 @@ public function test_get_weekday() { 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 122c44e3e2d64..ef70ecb319d08 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 @@ public function test_get_items_wdotorg_unavailable() { $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 3e2873f55c232..b9051115c4da5 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 @@ public function test_create_item_wdotorg_unreachable() { $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 66412701bfb9e..ec73674e955f0 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 @@ public function test_format_validation_is_skipped_if_non_string_type() { */ 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 f90f39511a0b3..0728a7c08ccaa 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 @@ public function test_format_validation_is_skipped_if_non_string_type() { */ 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' );