Skip to content

Commit d46af09

Browse files
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
1 parent f78acee commit d46af09

10 files changed

Lines changed: 22 additions & 27 deletions

File tree

tests/phpunit/tests/ajax/DeletePlugin.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
* @group ajax
1111
*/
1212
class Tests_Ajax_Delete_Plugin extends WP_Ajax_UnitTestCase {
13-
/**
14-
* @expectedException WPAjaxDieStopException
15-
* @expectedExceptionMessage -1
16-
*/
13+
1714
public function test_missing_nonce() {
15+
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
1816
$this->_handleAjax( 'delete-plugin' );
1917
}
2018

tests/phpunit/tests/ajax/UpdatePlugin.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
* @group ajax
1111
*/
1212
class Tests_Ajax_Update_Plugin extends WP_Ajax_UnitTestCase {
13-
/**
14-
* @expectedException WPAjaxDieStopException
15-
* @expectedExceptionMessage -1
16-
*/
13+
1714
public function test_missing_nonce() {
15+
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
1816
$this->_handleAjax( 'update-plugin' );
1917
}
2018

tests/phpunit/tests/comment/wpAllowComment.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ public function test_allow_comment_if_comment_author_emails_differ() {
5252
$this->assertSame( 1, $result );
5353
}
5454

55-
/**
56-
* @expectedException WPDieException
57-
*/
5855
public function test_die_as_duplicate_if_comment_author_name_and_emails_match() {
56+
$this->expectException( 'WPDieException' );
57+
5958
$now = time();
6059
$comment_data = array(
6160
'comment_post_ID' => self::$post_id,

tests/phpunit/tests/compat.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,9 @@ function test_hash_hmac_output() {
179179
$this->assertSame( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
180180
}
181181

182-
/**
183-
* @expectedException PHPUnit_Framework_Error_Deprecated
184-
*/
185182
function test_json_encode_decode() {
183+
$this->expectException( 'PHPUnit_Framework_Error_Deprecated' );
184+
186185
require_once ABSPATH . WPINC . '/class-json.php';
187186
$json = new Services_JSON();
188187
// Super basic test to verify Services_JSON is intact and working.

tests/phpunit/tests/db.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,10 @@ function test_check_database_version() {
524524
$this->assertEmpty( $wpdb->check_database_version() );
525525
}
526526

527-
/**
528-
* @expectedException WPDieException
529-
*/
530527
function test_bail() {
531528
global $wpdb;
529+
530+
$this->expectException( 'WPDieException' );
532531
$wpdb->bail( 'Database is dead.' );
533532
}
534533

tests/phpunit/tests/formatting/redirect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ function home_url() {
2424
* @ticket 44317
2525
*
2626
* @dataProvider get_bad_status_codes
27-
* @expectedException WPDieException
2827
*
2928
* @param string $location The path or URL to redirect to.
3029
* @param int $status HTTP response status code to use.
3130
*/
3231
public function test_wp_redirect_bad_status_code( $location, $status ) {
32+
$this->expectException( 'WPDieException' );
33+
3334
wp_redirect( $location, $status );
3435
}
3536

tests/phpunit/tests/includes/helpers.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,18 @@ protected function mock_incorrect_usage() {
268268

269269
/**
270270
* @ticket 36166
271-
* @expectedException WPDieException
272271
*/
273272
public function test_die_handler_should_handle_wp_error() {
273+
$this->expectException( 'WPDieException' );
274+
274275
wp_die( new WP_Error( 'test', 'test' ) );
275276
}
276277

277278
/**
278279
* @ticket 46813
279-
* @expectedException WPDieException
280280
*/
281281
public function test_die_handler_should_not_cause_doing_it_wrong_notice_without_wp_query_set() {
282+
$this->expectException( 'WPDieException' );
282283
unset( $GLOBALS['wp_query'] );
283284

284285
wp_die();

tests/phpunit/tests/option/option.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ function test_bad_option_names() {
101101

102102
/**
103103
* @ticket 23289
104-
* @expectedException WPDieException
105104
*/
106105
function test_special_option_name_alloption() {
106+
$this->expectException( 'WPDieException' );
107107
delete_option( 'alloptions' );
108108
}
109109

110110
/**
111111
* @ticket 23289
112-
* @expectedException WPDieException
113112
*/
114113
function test_special_option_name_notoptions() {
114+
$this->expectException( 'WPDieException' );
115115
delete_option( 'notoptions' );
116116
}
117117

tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private function remove_exports_dir() {
163163
public function test_rejects_remove_requests() {
164164
$request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
165165

166-
$this->setExpectedException( 'WPDieException' );
166+
$this->expectException( 'WPDieException' );
167167
$this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
168168
wp_privacy_generate_personal_data_export_file( $request_id );
169169
}
@@ -174,7 +174,7 @@ public function test_rejects_remove_requests() {
174174
* @ticket 44233
175175
*/
176176
public function test_invalid_request_id() {
177-
$this->setExpectedException( 'WPDieException' );
177+
$this->expectException( 'WPDieException' );
178178
$this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
179179
wp_privacy_generate_personal_data_export_file( 123456789 );
180180
}
@@ -194,7 +194,7 @@ public function test_rejects_requests_with_bad_email_addresses() {
194194
)
195195
);
196196

197-
$this->setExpectedException( 'WPDieException' );
197+
$this->expectException( 'WPDieException' );
198198
$this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' );
199199
wp_privacy_generate_personal_data_export_file( $request_id );
200200
}
@@ -208,7 +208,7 @@ public function test_detect_cannot_create_folder() {
208208
// Create a file with the folder name to ensure the function cannot create a folder.
209209
touch( untrailingslashit( self::$exports_dir ) );
210210

211-
$this->setExpectedException( 'WPDieException' );
211+
$this->expectException( 'WPDieException' );
212212
$this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' );
213213
wp_privacy_generate_personal_data_export_file( self::$export_request_id );
214214
}

tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function filter_register_custom_personal_data_exporters( $exporters ) {
256256
* @param string $expected_output The expected string exception output.
257257
*/
258258
private function _setup_expected_failure( $expected_output ) {
259-
$this->setExpectedException( 'WPDieException' );
259+
$this->expectException( 'WPDieException' );
260260
$this->expectOutputString( $expected_output );
261261
}
262262

0 commit comments

Comments
 (0)