Make WordPress Core

Changeset 48952


Ignore:
Timestamp:
09/07/2020 03:12:17 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add a polyfill for assertEqualsWithDelta() to WP_UnitTestCase and use it where appropriate.

assertEqualsWithDelta() was added in PHPUnit 7.5, while WordPress still supports PHPUnit 5.4.x as the minimum version.

See #38266.

Location:
trunk/tests/phpunit
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/phpunit6/compat.php

    r46625 r48952  
    1212        class_alias( 'PHPUnit\Framework\Warning', 'PHPUnit_Framework_Warning' );
    1313        class_alias( 'PHPUnit\Framework\AssertionFailedError', 'PHPUnit_Framework_AssertionFailedError' );
     14        class_alias( 'PHPUnit\Framework\Constraint\IsEqual', 'PHPUnit_Framework_Constraint_IsEqual' );
    1415        class_alias( 'PHPUnit\Framework\TestSuite', 'PHPUnit_Framework_TestSuite' );
    1516        class_alias( 'PHPUnit\Framework\TestListener', 'PHPUnit_Framework_TestListener' );
  • trunk/tests/phpunit/includes/phpunit7/testcase.php

    r47198 r48952  
    1717         * Asserts that a condition is not false.
    1818         *
    19          * This method has been backported from a more recent PHPUnit version, as tests running on PHP 5.2 use
    20          * PHPUnit 3.6.x.
     19         * This method has been backported from a more recent PHPUnit version,
     20         * as tests running on PHP 5.2 use PHPUnit 3.6.x.
    2121         *
    2222         * @since 4.7.4
    … …  
    3030                self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
    3131        }
     32
     33        /**
     34         * Asserts that two variables are equal (with delta).
     35         *
     36         * This method has been backported from a more recent PHPUnit version,
     37         * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     38         *
     39         * @since 5.6.0
     40         *
     41         * @param mixed  $expected First value to compare.
     42         * @param mixed  $actual   Second value to compare.
     43         * @param float  $delta    Allowed numerical distance between two values to consider them equal.
     44         * @param string $message  Optional. Message to display when the assertion fails.
     45         *
     46         * @throws ExpectationFailedException
     47         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     48         */
     49        public static function assertEqualsWithDelta( $expected, $actual, float $delta, string $message = '' ): void {
     50                $constraint = new PHPUnit\Framework\Constraint\IsEqual(
     51                        $expected,
     52                        $delta
     53                );
     54
     55                static::assertThat( $actual, $constraint, $message );
     56        }
    3257}
  • trunk/tests/phpunit/includes/testcase.php

    r47198 r48952  
    1717         * Asserts that a condition is not false.
    1818         *
    19          * This method has been backported from a more recent PHPUnit version, as tests running on PHP 5.2 use
    20          * PHPUnit 3.6.x.
     19         * This method has been backported from a more recent PHPUnit version,
     20         * as tests running on PHP 5.2 use PHPUnit 3.6.x.
    2121         *
    2222         * @since 4.7.4
    … …  
    3030                self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
    3131        }
     32
     33        /**
     34         * Asserts that two variables are equal (with delta).
     35         *
     36         * This method has been backported from a more recent PHPUnit version,
     37         * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     38         *
     39         * @since 5.6.0
     40         *
     41         * @param mixed  $expected First value to compare.
     42         * @param mixed  $actual   Second value to compare.
     43         * @param float  $delta    Allowed numerical distance between two values to consider them equal.
     44         * @param string $message  Optional. Message to display when the assertion fails.
     45         *
     46         * @throws ExpectationFailedException
     47         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     48         */
     49        public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
     50                $constraint = new PHPUnit_Framework_Constraint_IsEqual(
     51                        $expected,
     52                        $delta
     53                );
     54
     55                static::assertThat( $actual, $constraint, $message );
     56        }
    3257}
  • trunk/tests/phpunit/tests/date/currentTime.php

    r47118 r48952  
    1717                $wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS;
    1818
    19                 $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( current_time( $format, true ) ), 'The dates should be equal', 2 );
    20                 $this->assertEquals( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( $format ) ), 'The dates should be equal', 2 );
     19                $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( $format, true ) ), 2, 'The dates should be equal' );
     20                $this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( $format ) ), 2, 'The dates should be equal' );
    2121        }
    2222
    … …  
    3131                $wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS;
    3232
    33                 $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( current_time( 'mysql', true ) ), 'The dates should be equal', 2 );
    34                 $this->assertEquals( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( 'mysql' ) ), 'The dates should be equal', 2 );
     33                $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( 'mysql', true ) ), 2, 'The dates should be equal' );
     34                $this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( 'mysql' ) ), 2, 'The dates should be equal' );
    3535        }
    3636
    … …  
    4545
    4646                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
    47                 $this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 );
     47                $this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' );
    4848                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
    49                 $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 );
     49                $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' );
    5050        }
    5151
    … …  
    7171                $current_time     = current_time( $format );
    7272
    73                 $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 'The dates should be equal', 2 );
    74                 $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 'The dates should be equal', 2 );
    75                 $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 'The dates should be equal', 2 );
    76                 $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 'The dates should be equal', 2 );
     73                $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 2, 'The dates should be equal' );
     74                $this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 2, 'The dates should be equal' );
     75                $this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 2, 'The dates should be equal' );
     76                $this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 2, 'The dates should be equal' );
    7777        }
    7878
    … …  
    8989
    9090                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
    91                 $this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 );
     91                $this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' );
    9292                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
    93                 $this->assertEquals( $timestamp, current_time( 'U', true ), 'The dates should be equal', 2 );
     93                $this->assertEqualsWithDelta( $timestamp, current_time( 'U', true ), 2, 'The dates should be equal' );
    9494
    9595                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
    96                 $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 );
     96                $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' );
    9797                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
    98                 $this->assertEquals( $wp_timestamp, current_time( 'U' ), 'The dates should be equal', 2 );
     98                $this->assertEqualsWithDelta( $wp_timestamp, current_time( 'U' ), 2, 'The dates should be equal' );
    9999
    100100                // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
    … …  
    114114                $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
    115115
    116                 $this->assertEquals( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 'The dates should be equal', 2 );
    117                 $this->assertEquals( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 'The dates should be equal', 2 );
     116                $this->assertEqualsWithDelta( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 2, 'The dates should be equal' );
     117                $this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 2, 'The dates should be equal' );
    118118        }
    119119}
  • trunk/tests/phpunit/tests/date/dateI18n.php

    r48937 r48952  
    1717                $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
    1818
    19                 $this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 );
     19                $this->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U', 'invalid' ), 5, 'The dates should be equal' );
    2020        }
    2121
    … …  
    3939
    4040        public function test_should_format_date() {
    41                 $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 );
     41                $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' );
    4242        }
    4343
    … …  
    4747
    4848        public function test_date_should_be_in_gmt() {
    49                 $this->assertEquals( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
     49                $this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' );
    5050        }
    5151
    … …  
    5353                update_option( 'timezone_string', 'America/Regina' );
    5454
    55                 $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 );
     55                $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' );
    5656        }
    5757
    … …  
    5959                update_option( 'timezone_string', 'America/Regina' );
    6060
    61                 $this->assertEquals( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
     61                $this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' );
    6262        }
    6363
    … …  
    122122                update_option( 'timezone_string', 'America/Regina' );
    123123
    124                 $this->assertEquals( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 'The dates should be equal', 2 );
     124                $this->assertEqualsWithDelta( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 2, 'The dates should be equal' );
    125125                $this->assertSame( $short, date_i18n( '\\' . $short ) );
    126126        }
    … …  
    149149                $wp_timestamp = $timestamp + $datetime->getOffset();
    150150
    151                 $this->assertEquals( $wp_timestamp, date_i18n( 'U' ), 'The dates should be equal', 2 );
    152                 $this->assertEquals( $timestamp, date_i18n( 'U', false, true ), 'The dates should be equal', 2 );
     151                $this->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U' ), 2, 'The dates should be equal' );
     152                $this->assertEqualsWithDelta( $timestamp, date_i18n( 'U', false, true ), 2, 'The dates should be equal' );
    153153                $this->assertSame( $wp_timestamp, date_i18n( 'U', $wp_timestamp ) );
    154154        }
  • trunk/tests/phpunit/tests/date/getFeedBuildDate.php

    r48937 r48952  
    6161                );
    6262
    63                 $this->assertEquals(
     63                $this->assertEqualsWithDelta(
    6464                        strtotime( $datetime_utc->format( DATE_RFC3339 ) ),
    6565                        strtotime( get_feed_build_date( DATE_RFC3339 ) ),
    66                         'Fall back to time of last post modified with no posts',
    67                         2
     66                        2,
     67                        'Fall back to time of last post modified with no posts'
    6868                );
    6969
    … …  
    7575                $wp_query->posts = array( $post_broken );
    7676
    77                 $this->assertEquals(
     77                $this->assertEqualsWithDelta(
    7878                        strtotime( $datetime_utc->format( DATE_RFC3339 ) ),
    7979                        strtotime( get_feed_build_date( DATE_RFC3339 ) ),
    80                         'Fall back to time of last post modified with broken post object',
    81                         2
     80                        2,
     81                        'Fall back to time of last post modified with broken post object'
    8282                );
    8383        }
  • trunk/tests/phpunit/tests/date/query.php

    r48937 r48952  
    525525
    526526                $message = "Expected {$expected}, got {$found}";
    527                 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
     527                $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
    528528        }
    529529
    … …  
    560560
    561561                $message = "Expected {$expected}, got {$found}";
    562                 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
     562                $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
    563563
    564564        }
    … …  
    584584
    585585                $message = "Expected {$expected}, got {$found}";
    586                 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
     586                $this->assertEqualsWithDelta( strtotime( $expected ), strtotime( $found ), 10, $message );
    587587        }
    588588
  • trunk/tests/phpunit/tests/formatting/date.php

    r48937 r48952  
    8686                $local = 'now';
    8787                $gmt   = gmdate( 'Y-m-d H:i:s' );
    88                 $this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 );
     88                $this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' );
    8989        }
    9090
    … …  
    9595                $local = 'now';
    9696                $gmt   = gmdate( 'Y-m-d H:i:s' );
    97                 $this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 );
     97                $this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' );
    9898        }
    9999
  • trunk/tests/phpunit/tests/media.php

    r48937 r48952  
    441441                // Now some values around.
    442442                $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
    443                 $this->assertEquals( 1.50000095367, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
     443                $this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
    444444
    445445                $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
    446                 $this->assertEquals( 1023.99902248, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
     446                $this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
    447447
    448448                $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
    449                 $this->assertEquals( 1.5009765625, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
     449                $this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
    450450
    451451                $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
    452                 $this->assertEquals( 1022.99902344, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
     452                $this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
    453453
    454454                // Edge.
  • trunk/tests/phpunit/tests/multisite/site.php

    r48939 r48952  
    410410
    411411                        // Compare the update time with the current time, allow delta < 2.
    412                         $this->assertEquals( $current_time, strtotime( $blog->last_updated ), 'The dates should be equal', 2 );
     412                        $this->assertEqualsWithDelta( $current_time, strtotime( $blog->last_updated ), 2, 'The dates should be equal' );
    413413                }
    414414
    … …  
    17931793
    17941794                        $site = get_site( $site_id );
    1795                         $this->assertEquals( strtotime( $first_date ), strtotime( $site->registered ), 'The dates should be equal', 2 );
    1796                         $this->assertEquals( strtotime( $first_date ), strtotime( $site->last_updated ), 'The dates should be equal', 2 );
     1795                        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
     1796                        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
    17971797
    17981798                        $second_date = current_time( 'mysql', true );
    … …  
    18011801
    18021802                        $site = get_site( $site_id );
    1803                         $this->assertEquals( strtotime( $first_date ), strtotime( $site->registered ), 'The dates should be equal', 2 );
    1804                         $this->assertEquals( strtotime( $second_date ), strtotime( $site->last_updated ), 'The dates should be equal', 2 );
     1803                        $this->assertEqualsWithDelta( strtotime( $first_date ), strtotime( $site->registered ), 2, 'The dates should be equal' );
     1804                        $this->assertEqualsWithDelta( strtotime( $second_date ), strtotime( $site->last_updated ), 2, 'The dates should be equal' );
    18051805                }
    18061806
  • trunk/tests/phpunit/tests/post.php

    r48939 r48952  
    13531353
    13541354                $post = get_post( $post_id );
    1355                 self::assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 'The dates should be equal', 2 );
     1355                self::assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 2, 'The dates should be equal' );
    13561356        }
    13571357
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r48939 r48952  
    46404640                $body     = $response->get_data();
    46414641
    4642                 $this->assertEquals( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 'The dates should be equal', 2 );
    4643                 $this->assertEquals( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 'The dates should be equal', 2 );
     4642                $this->assertEqualsWithDelta( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 2, 'The dates should be equal' );
     4643                $this->assertEqualsWithDelta( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 2, 'The dates should be equal' );
    46444644
    46454645                $this->assertSame( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
    … …  
    46834683                $body     = $response->get_data();
    46844684
    4685                 $this->assertEquals( strtotime( mysql_to_rfc3339( $new_time ) ), strtotime( $body['date'] ), 'The dates should be equal', 2 );
     4685                $this->assertEqualsWithDelta( strtotime( mysql_to_rfc3339( $new_time ) ), strtotime( $body['date'] ), 2, 'The dates should be equal' );
    46864686
    46874687                $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
    … …  
    47244724                $body     = $response->get_data();
    47254725
    4726                 $this->assertEquals( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 'The dates should be equal', 2 );
    4727                 $this->assertEquals( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 'The dates should be equal', 2 );
     4726                $this->assertEqualsWithDelta( strtotime( $get_body['date'] ), strtotime( $body['date'] ), 2, 'The dates should be equal' );
     4727                $this->assertEqualsWithDelta( strtotime( $get_body['date_gmt'] ), strtotime( $body['date_gmt'] ), 2, 'The dates should be equal' );
    47284728
    47294729                $this->assertNotEquals( '0000-00-00 00:00:00', get_post( $post->ID )->post_date_gmt );
Note: See TracChangeset for help on using the changeset viewer.