From 8793eef7d32632f0fa6dea597f1a2c02957e15bc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 15 Aug 2020 19:28:34 +0200 Subject: [PATCH] Tests: replace `assertEquals()` with $delta with `assertEqualsWithDelta()` The `$delta` parameter of the `assertEquals()` method was soft deprecated in PHPUnit 7.5, hard deprecated in PHPUnit 8.0 and the functionality was removed in PHPUnit 9.0. The more specific `assertEqualsWithDelta()` (and `assertNotEqualsWithDelta()`) were introduced as replacements in PHPUnit 7.5. Ref: * https://github.com/sebastianbergmann/phpunit/blob/7.5.20/ChangeLog-7.5.md#750---2018-12-07 * https://github.com/sebastianbergmann/phpunit/issues/3340 --- tests/phpunit/tests/date/currentTime.php | 32 +++++++++---------- tests/phpunit/tests/date/dateI18n.php | 16 +++++----- tests/phpunit/tests/date/getFeedBuildDate.php | 12 +++---- tests/phpunit/tests/date/query.php | 6 ++-- tests/phpunit/tests/formatting/date.php | 4 +-- tests/phpunit/tests/media.php | 8 ++--- tests/phpunit/tests/post.php | 2 +- .../tests/rest-api/rest-posts-controller.php | 10 +++--- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/tests/phpunit/tests/date/currentTime.php b/tests/phpunit/tests/date/currentTime.php index 37606748517a1..85aa168bc7839 100644 --- a/tests/phpunit/tests/date/currentTime.php +++ b/tests/phpunit/tests/date/currentTime.php @@ -16,8 +16,8 @@ public function test_current_time_with_date_format_string() { $timestamp = time(); $wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS; - $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( current_time( $format, true ) ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( $format ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( $format, true ) ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( $format ) ), 2, 'The dates should be equal' ); } /** @@ -30,8 +30,8 @@ public function test_current_time_with_mysql_format() { $timestamp = time(); $wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS; - $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( current_time( 'mysql', true ) ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( 'mysql' ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( 'mysql', true ) ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( 'mysql' ) ), 2, 'The dates should be equal' ); } /** @@ -44,9 +44,9 @@ public function test_current_time_with_timestamp() { $wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS; // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC - $this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested - $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' ); } /** @@ -70,10 +70,10 @@ public function test_should_work_with_changed_timezone() { $current_time_gmt = current_time( $format, true ); $current_time = current_time( $format ); - $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 2, 'The dates should be equal' ); } /** @@ -88,14 +88,14 @@ public function test_should_return_wp_timestamp() { $wp_timestamp = $timestamp + $datetime->getOffset(); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC - $this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC - $this->assertEquals( $timestamp, current_time( 'U', true ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $timestamp, current_time( 'U', true ), 2, 'The dates should be equal' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested - $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested - $this->assertEquals( $wp_timestamp, current_time( 'U' ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'U' ), 2, 'The dates should be equal' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested $this->assertIsInt( current_time( 'timestamp' ) ); @@ -113,7 +113,7 @@ public function test_should_return_correct_local_time() { $datetime_utc = new DateTime( '@' . $timestamp ); $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) ); - $this->assertEquals( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 2, 'The dates should be equal' ); } } diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php index 84c43efc4c080..f7aebf4f9f5ec 100644 --- a/tests/phpunit/tests/date/dateI18n.php +++ b/tests/phpunit/tests/date/dateI18n.php @@ -16,7 +16,7 @@ public function test_should_return_current_time_on_invalid_timestamp() { $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) ); $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset(); - $this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 ); + $this->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U', 'invalid' ), 5 ); } /** @@ -38,7 +38,7 @@ public function test_should_handle_zero_timestamp() { } public function test_should_format_date() { - $this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 2, 'The dates should be equal' ); } public function test_should_use_custom_timestamp() { @@ -46,19 +46,19 @@ public function test_should_use_custom_timestamp() { } public function test_date_should_be_in_gmt() { - $this->assertEquals( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' ); } public function test_custom_timezone_setting() { update_option( 'timezone_string', 'America/Regina' ); - $this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s', time() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s', time() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 2, 'The dates should be equal' ); } public function test_date_should_be_in_gmt_with_custom_timezone_setting() { update_option( 'timezone_string', 'America/Regina' ); - $this->assertEquals( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' ); } public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() { @@ -121,7 +121,7 @@ public function test_gmt_offset_should_output_correct_timezone() { public function test_date_i18n_handles_shorthand_formats( $short, $full ) { update_option( 'timezone_string', 'America/Regina' ); - $this->assertEquals( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 2, 'The dates should be equal' ); $this->assertEquals( $short, date_i18n( '\\' . $short ) ); } @@ -148,8 +148,8 @@ public function test_should_return_wp_timestamp() { $timestamp = $datetime->getTimestamp(); $wp_timestamp = $timestamp + $datetime->getOffset(); - $this->assertEquals( $wp_timestamp, date_i18n( 'U' ), 'The dates should be equal', 2 ); - $this->assertEquals( $timestamp, date_i18n( 'U', false, true ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U' ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( $timestamp, date_i18n( 'U', false, true ), 2, 'The dates should be equal' ); $this->assertEquals( $wp_timestamp, date_i18n( 'U', $wp_timestamp ) ); } diff --git a/tests/phpunit/tests/date/getFeedBuildDate.php b/tests/phpunit/tests/date/getFeedBuildDate.php index dee5e30564652..165227068e832 100644 --- a/tests/phpunit/tests/date/getFeedBuildDate.php +++ b/tests/phpunit/tests/date/getFeedBuildDate.php @@ -60,11 +60,11 @@ public function test_should_fall_back_to_last_post_modified() { ) ); - $this->assertEquals( + $this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_RFC3339 ) ), strtotime( get_feed_build_date( DATE_RFC3339 ) ), - 'Fall back to time of last post modified with no posts', - 2 + 2, + 'Fall back to time of last post modified with no posts' ); $post_id_broken = $this->factory->post->create(); @@ -74,11 +74,11 @@ public function test_should_fall_back_to_last_post_modified() { $wp_query->posts = array( $post_broken ); - $this->assertEquals( + $this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_RFC3339 ) ), strtotime( get_feed_build_date( DATE_RFC3339 ) ), - 'Fall back to time of last post modified with broken post object', - 2 + 2, + 'Fall back to time of last post modified with broken post object' ); } } diff --git a/tests/phpunit/tests/date/query.php b/tests/phpunit/tests/date/query.php index 9cd41abd71df8..7aaf4530a6dd5 100644 --- a/tests/phpunit/tests/date/query.php +++ b/tests/phpunit/tests/date/query.php @@ -524,7 +524,7 @@ public function test_build_mysql_datetime( $datetime, $expected, $default_to_max $found = $q->build_mysql_datetime( $datetime, $default_to_max ); $message = "Expected {$expected}, got {$found}"; - $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); + $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message ); } public function mysql_datetime_input_provider() { @@ -559,7 +559,7 @@ public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expe $found = $q->build_mysql_datetime( $datetime, $default_to_max ); $message = "Expected {$expected}, got {$found}"; - $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); + $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message ); } @@ -583,7 +583,7 @@ public function test_build_mysql_datetime_with_relative_date() { $found = $q->build_mysql_datetime( '-1 day' ); $message = "Expected {$expected}, got {$found}"; - $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); + $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message ); } public function test_build_time_query_insufficient_time_values() { diff --git a/tests/phpunit/tests/formatting/date.php b/tests/phpunit/tests/formatting/date.php index fce5494f87725..acc035340c41c 100644 --- a/tests/phpunit/tests/formatting/date.php +++ b/tests/phpunit/tests/formatting/date.php @@ -85,7 +85,7 @@ function test_get_gmt_from_date_string_date() { update_option( 'timezone_string', 'Europe/London' ); $local = 'now'; $gmt = gmdate( 'Y-m-d H:i:s' ); - $this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' ); } /** @@ -94,7 +94,7 @@ function test_get_gmt_from_date_string_date() { function test_get_gmt_from_date_string_date_no_timezone() { $local = 'now'; $gmt = gmdate( 'Y-m-d H:i:s' ); - $this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' ); } /** diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index d4222e7022519..4017bcf9a34c1 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -440,16 +440,16 @@ function test_wp_convert_bytes_to_hr() { // Now some values around. $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb ); - $this->assertEquals( 1.50000095367, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 ); + $this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb ); - $this->assertEquals( 1023.99902248, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 ); + $this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb ); - $this->assertEquals( 1.5009765625, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 ); + $this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb ); - $this->assertEquals( 1022.99902344, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 ); + $this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); // Edge. $this->assertEquals( '-1B', wp_convert_bytes_to_hr( -1 ) ); diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index cea1d2fe8af9f..207c7496ff93f 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1439,7 +1439,7 @@ public function test_insert_post_should_respect_date_floating_post_status_arg_no ); $post = get_post( $post_id ); - self::assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 'The dates should be equal', 2 ); + self::assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 2, 'The dates should be equal' ); } /** diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 9cfcf7c52af22..f53e82d2d3d88 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -4639,8 +4639,8 @@ public function test_putting_same_publish_date_does_not_remove_floating_date() { $response = rest_get_server()->dispatch( $put ); $body = $response->get_data(); - $this->assertEquals( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 2, 'The dates should be equal' ); $this->assertEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt ); } @@ -4682,7 +4682,7 @@ public function test_putting_different_publish_date_removes_floating_date() { $response = rest_get_server()->dispatch( $put ); $body = $response->get_data(); - $this->assertEquals( strtotime( mysql_to_rfc3339( $new_time ) ), strtotime( $body['date'] ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( mysql_to_rfc3339( $new_time ) ), strtotime( $body['date'] ), 2, 'The dates should be equal' ); $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt ); } @@ -4723,8 +4723,8 @@ public function test_publishing_post_with_same_date_removes_floating_date() { $response = rest_get_server()->dispatch( $put ); $body = $response->get_data(); - $this->assertEquals( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 'The dates should be equal', 2 ); - $this->assertEquals( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 'The dates should be equal', 2 ); + $this->assertEqualsWithDelta( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 2, 'The dates should be equal' ); + $this->assertEqualsWithDelta( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 2, 'The dates should be equal' ); $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt ); }