Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/url.php

    r47122 r48937  
    7878        $admin_url_https = admin_url( $url );
    7979
    80         $this->assertEquals( $siteurl_http . $expected, $admin_url_http );
    81         $this->assertEquals( $siteurl_https . $expected, $admin_url_https );
     80        $this->assertSame( $siteurl_http . $expected, $admin_url_http );
     81        $this->assertSame( $siteurl_https . $expected, $admin_url_https );
    8282    }
    8383
     
    146146        $home_url_https = home_url( $url );
    147147
    148         $this->assertEquals( $homeurl_http . $expected, $home_url_http );
    149         $this->assertEquals( $homeurl_https . $expected, $home_url_https );
     148        $this->assertSame( $homeurl_http . $expected, $home_url_http );
     149        $this->assertSame( $homeurl_https . $expected, $home_url_https );
    150150    }
    151151
     
    208208        // home_url() should return http when in the admin.
    209209        $_SERVER['HTTPS'] = 'on';
    210         $this->assertEquals( $home, home_url() );
     210        $this->assertSame( $home, home_url() );
    211211
    212212        $_SERVER['HTTPS'] = 'off';
    213         $this->assertEquals( $home, home_url() );
     213        $this->assertSame( $home, home_url() );
    214214
    215215        // If not in the admin, is_ssl() should determine the scheme.
    216216        set_current_screen( 'front' );
    217         $this->assertEquals( $home, home_url() );
     217        $this->assertSame( $home, home_url() );
    218218        $_SERVER['HTTPS'] = 'on';
    219219        $home             = str_replace( 'http://', 'https://', $home );
    220         $this->assertEquals( $home, home_url() );
     220        $this->assertSame( $home, home_url() );
    221221
    222222        // Test with https in home.
     
    229229        // home_url() should return whatever scheme is set in the home option when in the admin.
    230230        $_SERVER['HTTPS'] = 'on';
    231         $this->assertEquals( $home, home_url() );
     231        $this->assertSame( $home, home_url() );
    232232
    233233        $_SERVER['HTTPS'] = 'off';
    234         $this->assertEquals( $home, home_url() );
     234        $this->assertSame( $home, home_url() );
    235235
    236236        // If not in the admin, is_ssl() should determine the scheme unless https hard-coded in home.
    237237        set_current_screen( 'front' );
    238         $this->assertEquals( $home, home_url() );
    239         $_SERVER['HTTPS'] = 'on';
    240         $this->assertEquals( $home, home_url() );
     238        $this->assertSame( $home, home_url() );
     239        $_SERVER['HTTPS'] = 'on';
     240        $this->assertSame( $home, home_url() );
    241241        $_SERVER['HTTPS'] = 'off';
    242         $this->assertEquals( $home, home_url() );
     242        $this->assertSame( $home, home_url() );
    243243
    244244        update_option( 'home', set_url_scheme( $home, 'http' ) );
     
    255255
    256256        // home_url() should return http when in the admin.
    257         $this->assertEquals( 0, strpos( $home, 'http://' ) );
    258         $_SERVER['HTTPS'] = 'on';
    259         $this->assertEquals( $home, network_home_url() );
     257        $this->assertSame( 0, strpos( $home, 'http://' ) );
     258        $_SERVER['HTTPS'] = 'on';
     259        $this->assertSame( $home, network_home_url() );
    260260
    261261        $_SERVER['HTTPS'] = 'off';
    262         $this->assertEquals( $home, network_home_url() );
     262        $this->assertSame( $home, network_home_url() );
    263263
    264264        // If not in the admin, is_ssl() should determine the scheme.
    265265        set_current_screen( 'front' );
    266         $this->assertEquals( $home, network_home_url() );
     266        $this->assertSame( $home, network_home_url() );
    267267        $_SERVER['HTTPS'] = 'on';
    268268        $home             = str_replace( 'http://', 'https://', $home );
    269         $this->assertEquals( $home, network_home_url() );
     269        $this->assertSame( $home, network_home_url() );
    270270
    271271        $GLOBALS['current_screen'] = $screen;
     
    308308        $i            = 0;
    309309        foreach ( $links as $link ) {
    310             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'https' ) );
    311             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'http' ) );
    312             $this->assertEquals( $relative_links[ $i ], set_url_scheme( $link, 'relative' ) );
     310            $this->assertSame( $https_links[ $i ], set_url_scheme( $link, 'https' ) );
     311            $this->assertSame( $http_links[ $i ], set_url_scheme( $link, 'http' ) );
     312            $this->assertSame( $relative_links[ $i ], set_url_scheme( $link, 'relative' ) );
    313313
    314314            $_SERVER['HTTPS'] = 'on';
    315             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link ) );
     315            $this->assertSame( $https_links[ $i ], set_url_scheme( $link ) );
    316316
    317317            $_SERVER['HTTPS'] = 'off';
    318             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link ) );
     318            $this->assertSame( $http_links[ $i ], set_url_scheme( $link ) );
    319319
    320320            force_ssl_admin( true );
    321             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'admin' ) );
    322             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
    323             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login' ) );
    324             $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
     321            $this->assertSame( $https_links[ $i ], set_url_scheme( $link, 'admin' ) );
     322            $this->assertSame( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
     323            $this->assertSame( $https_links[ $i ], set_url_scheme( $link, 'login' ) );
     324            $this->assertSame( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
    325325
    326326            force_ssl_admin( false );
    327             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'admin' ) );
    328             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login_post' ) );
    329             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login' ) );
    330             $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'rpc' ) );
     327            $this->assertSame( $http_links[ $i ], set_url_scheme( $link, 'admin' ) );
     328            $this->assertSame( $http_links[ $i ], set_url_scheme( $link, 'login_post' ) );
     329            $this->assertSame( $http_links[ $i ], set_url_scheme( $link, 'login' ) );
     330            $this->assertSame( $http_links[ $i ], set_url_scheme( $link, 'rpc' ) );
    331331
    332332            $i++;
     
    349349        $p = get_adjacent_post();
    350350        $this->assertInstanceOf( 'WP_Post', $p );
    351         $this->assertEquals( $post_id, $p->ID );
     351        $this->assertSame( $post_id, $p->ID );
    352352
    353353        // The same again to make sure a cached query returns the same result.
    354354        $p = get_adjacent_post();
    355355        $this->assertInstanceOf( 'WP_Post', $p );
    356         $this->assertEquals( $post_id, $p->ID );
     356        $this->assertSame( $post_id, $p->ID );
    357357
    358358        // Test next.
    359359        $p = get_adjacent_post( false, '', false );
    360         $this->assertEquals( '', $p );
     360        $this->assertSame( '', $p );
    361361
    362362        unset( $GLOBALS['post'] );
     
    399399
    400400        $p = get_adjacent_post();
    401         $this->assertEquals( $p1, $p->ID );
     401        $this->assertSame( $p1, $p->ID );
    402402
    403403        $GLOBALS['post'] = $orig_post;
     
    437437
    438438        $p = get_adjacent_post();
    439         $this->assertEquals( $p1, $p->ID );
     439        $this->assertSame( $p1, $p->ID );
    440440
    441441        $GLOBALS['post'] = $orig_post;
     
    481481
    482482        $p = get_adjacent_post();
    483         $this->assertEquals( $p1, $p->ID );
     483        $this->assertSame( $p1, $p->ID );
    484484
    485485        $GLOBALS['post'] = $orig_post;
     
    507507
    508508        foreach ( $functions as $function ) {
    509             $this->assertEquals(
     509            $this->assertSame(
    510510                call_user_func( $function, '/' ) . '../',
    511511                call_user_func( $function, '../' )
    512512            );
    513             $this->assertEquals(
     513            $this->assertSame(
    514514                call_user_func( $function, '/' ) . 'something...here',
    515515                call_user_func( $function, 'something...here' )
     
    519519        // These functions accept a blog ID argument.
    520520        foreach ( array( 'get_site_url', 'get_home_url', 'get_admin_url' ) as $function ) {
    521             $this->assertEquals(
     521            $this->assertSame(
    522522                call_user_func( $function, null, '/' ) . '../',
    523523                call_user_func( $function, null, '../' )
    524524            );
    525             $this->assertEquals(
     525            $this->assertSame(
    526526                call_user_func( $function, null, '/' ) . 'something...here',
    527527                call_user_func( $function, null, 'something...here' )
Note: See TracChangeset for help on using the changeset viewer.