Skip to content

Commit

Permalink
Tests: replace assertEquals() with $delta with `assertEqualsWithDel…
Browse files Browse the repository at this point in the history
…ta()`

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
* sebastianbergmann/phpunit#3340
  • Loading branch information
jrfnl committed Aug 16, 2020
1 parent c8eef9d commit 8793eef
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 45 deletions.
32 changes: 16 additions & 16 deletions tests/phpunit/tests/date/currentTime.php
Expand Up @@ -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' );
}

/**
Expand All @@ -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' );
}

/**
Expand All @@ -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' );
}

/**
Expand All @@ -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' );
}

/**
Expand All @@ -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' ) );
Expand All @@ -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' );
}
}
16 changes: 8 additions & 8 deletions tests/phpunit/tests/date/dateI18n.php
Expand Up @@ -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 );
}

/**
Expand All @@ -38,27 +38,27 @@ 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() {
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}

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() {
Expand Down Expand Up @@ -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 ) );
}

Expand All @@ -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 ) );
}

Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/tests/date/getFeedBuildDate.php
Expand Up @@ -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();
Expand All @@ -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'
);
}
}
6 changes: 3 additions & 3 deletions tests/phpunit/tests/date/query.php
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 );

}

Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/formatting/date.php
Expand Up @@ -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' );
}

/**
Expand All @@ -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' );
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/media.php
Expand Up @@ -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 ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/post.php
Expand Up @@ -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' );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
Expand Down

0 comments on commit 8793eef

Please sign in to comment.