Skip to content

Commit fe7295b

Browse files
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 * sebastianbergmann/phpunit#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
1 parent 8146100 commit fe7295b

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
lines changed

tests/phpunit/tests/compat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function test_hash_hmac_output() {
180180
}
181181

182182
function test_json_encode_decode() {
183-
$this->expectException( 'PHPUnit_Framework_Error_Deprecated' );
183+
$this->expectDeprecation();
184184

185185
require_once ABSPATH . WPINC . '/class-json.php';
186186
$json = new Services_JSON();

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,9 +1424,13 @@ public function test_wp_enqueue_code_editor_when_simple_array_will_be_passed() {
14241424
public function test_wp_localize_script_data_formats( $l10n_data, $expected, $warning = false ) {
14251425
if ( $warning ) {
14261426
if ( PHP_VERSION_ID < 80000 ) {
1427-
$this->expectException( 'PHPUnit_Framework_Error_Warning' );
1427+
$this->expectWarning();
14281428
} else {
1429-
// As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now).
1429+
/*
1430+
* As this exception will only be set on PHP 8 in combination with PHPUnit 7, this will work (for now).
1431+
* Once the PHPUnit version constraints have been widened and a _supported_ PHPUnit version is
1432+
* used to run the tests on PHP 8.x, this should be changed to `$this->expectError()`.
1433+
*/
14301434
$this->expectException( 'Error' );
14311435
}
14321436
}

tests/phpunit/tests/locale.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function test_get_weekday() {
2727

2828
public function test_get_weekday_undefined_index() {
2929
if ( PHP_VERSION_ID >= 80000 ) {
30-
$this->expectException( 'PHPUnit_Framework_Error_Warning' );
30+
$this->expectWarning();
3131
} else {
32-
$this->expectException( 'PHPUnit_Framework_Error_Notice' );
32+
$this->expectNotice();
3333
}
3434

3535
$this->locale->get_weekday( 7 );

tests/phpunit/tests/rest-api/rest-block-directory-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function test_get_items_wdotorg_unavailable() {
8989

9090
$this->prevent_requests_to_host( 'api.wordpress.org' );
9191

92-
$this->expectException( 'PHPUnit_Framework_Error_Warning' );
92+
$this->expectWarning();
9393
$response = rest_do_request( $request );
9494
$this->assertErrorResponse( 'plugins_api_failed', $response, 500 );
9595
}

tests/phpunit/tests/rest-api/rest-plugins-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public function test_create_item_wdotorg_unreachable() {
536536

537537
$this->prevent_requests_to_host( 'api.wordpress.org' );
538538

539-
$this->expectException( 'PHPUnit_Framework_Error_Warning' );
539+
$this->expectWarning();
540540
$response = rest_do_request( $request );
541541
$this->assertErrorResponse( 'plugins_api_failed', $response, 500 );
542542
}

tests/phpunit/tests/rest-api/rest-schema-sanitization.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ public function test_format_validation_is_skipped_if_non_string_type() {
457457
*/
458458
public function test_format_validation_is_applied_if_missing_type() {
459459
if ( PHP_VERSION_ID >= 80000 ) {
460-
$this->expectException( 'PHPUnit_Framework_Error_Warning' ); // For the undefined index.
460+
$this->expectWarning(); // For the undefined index.
461461
} else {
462-
$this->expectException( 'PHPUnit_Framework_Error_Notice' );
462+
$this->expectNotice(); // For the undefined index.
463463
}
464464

465465
$this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );

tests/phpunit/tests/rest-api/rest-schema-validation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ public function test_format_validation_is_skipped_if_non_string_type() {
157157
*/
158158
public function test_format_validation_is_applied_if_missing_type() {
159159
if ( PHP_VERSION_ID >= 80000 ) {
160-
$this->expectException( 'PHPUnit_Framework_Error_Warning' ); // For the undefined index.
160+
$this->expectWarning(); // For the undefined index.
161161
} else {
162-
$this->expectException( 'PHPUnit_Framework_Error_Notice' );
162+
$this->expectNotice(); // For the undefined index.
163163
}
164164

165165
$this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );

0 commit comments

Comments
 (0)