Skip to content

Commit

Permalink
Tests: Replace expectException() for PHP native errors with calls t…
Browse files Browse the repository at this point in the history
…o 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
  • Loading branch information
SergeyBiryukov authored and SergeyBiryukov committed Aug 6, 2021
1 parent 9de96a0 commit c1aab9e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/tests/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1424,9 +1424,13 @@ 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' );
$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' );
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-plugins-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/rest-schema-sanitization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/rest-schema-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit c1aab9e

Please sign in to comment.