WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (17 months 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/post/getPages.php

    r47122 r48937  
    2121
    2222        $pages = get_pages();
    23         $this->assertEquals( 3, count( $pages ) );
     23        $this->assertSame( 3, count( $pages ) );
    2424        $time1 = wp_cache_get( 'last_changed', 'posts' );
    2525        $this->assertNotEmpty( $time1 );
     
    3131        // Again. num_queries and last_changed should remain the same.
    3232        $pages = get_pages();
    33         $this->assertEquals( 3, count( $pages ) );
    34         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    35         $this->assertEquals( $num_queries, $wpdb->num_queries );
     33        $this->assertSame( 3, count( $pages ) );
     34        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
     35        $this->assertSame( $num_queries, $wpdb->num_queries );
    3636        foreach ( $pages as $page ) {
    3737            $this->assertInstanceOf( 'WP_Post', $page );
     
    4141        // different args to get_pages(). num_queries should bump by 1.
    4242        $pages = get_pages( array( 'number' => 2 ) );
    43         $this->assertEquals( 2, count( $pages ) );
    44         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    45         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     43        $this->assertSame( 2, count( $pages ) );
     44        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
     45        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    4646        foreach ( $pages as $page ) {
    4747            $this->assertInstanceOf( 'WP_Post', $page );
     
    5252        // Again. num_queries and last_changed should remain the same.
    5353        $pages = get_pages( array( 'number' => 2 ) );
    54         $this->assertEquals( 2, count( $pages ) );
    55         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    56         $this->assertEquals( $num_queries, $wpdb->num_queries );
     54        $this->assertSame( 2, count( $pages ) );
     55        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
     56        $this->assertSame( $num_queries, $wpdb->num_queries );
    5757        foreach ( $pages as $page ) {
    5858            $this->assertInstanceOf( 'WP_Post', $page );
     
    6161        // Do the first query again. The interim queries should not affect it.
    6262        $pages = get_pages();
    63         $this->assertEquals( 3, count( $pages ) );
    64         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
    65         $this->assertEquals( $num_queries, $wpdb->num_queries );
     63        $this->assertSame( 3, count( $pages ) );
     64        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
     65        $this->assertSame( $num_queries, $wpdb->num_queries );
    6666        foreach ( $pages as $page ) {
    6767            $this->assertInstanceOf( 'WP_Post', $page );
     
    7676        // last_changed bumped so num_queries should increment.
    7777        $pages = get_pages( array( 'number' => 2 ) );
    78         $this->assertEquals( 2, count( $pages ) );
    79         $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'posts' ) );
    80         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     78        $this->assertSame( 2, count( $pages ) );
     79        $this->assertSame( $time2, wp_cache_get( 'last_changed', 'posts' ) );
     80        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    8181        foreach ( $pages as $page ) {
    8282            $this->assertInstanceOf( 'WP_Post', $page );
     
    9696        // num_queries should bump after wp_delete_post() bumps last_changed.
    9797        $pages = get_pages();
    98         $this->assertEquals( 2, count( $pages ) );
    99         $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
    100         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     98        $this->assertSame( 2, count( $pages ) );
     99        $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
     100        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    101101        foreach ( $pages as $page ) {
    102102            $this->assertInstanceOf( 'WP_Post', $page );
     
    260260        add_post_meta( $posts[2], 'some-meta-key', '1' );
    261261
    262         $this->assertEquals(
     262        $this->assertSame(
    263263            1,
    264264            count(
     
    271271            )
    272272        );
    273         $this->assertEquals(
     273        $this->assertSame(
    274274            1,
    275275            count(
     
    282282            )
    283283        );
    284         $this->assertEquals( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
     284        $this->assertSame( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
    285285    }
    286286
     
    303303        $inc_result = wp_list_pluck( $include, 'ID' );
    304304        sort( $inc_result );
    305         $this->assertEquals( $inc, $inc_result );
     305        $this->assertSame( $inc, $inc_result );
    306306
    307307        $exclude    = get_pages( array( 'exclude' => $exc ) );
    308308        $exc_result = wp_list_pluck( $exclude, 'ID' );
    309309        sort( $exc_result );
    310         $this->assertEquals( $inc, $exc_result );
     310        $this->assertSame( $inc, $exc_result );
    311311    }
    312312
     
    377377        preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
    378378
    379         $this->assertEquals( 5, count( $matches[0] ) );
     379        $this->assertSame( 5, count( $matches[0] ) );
    380380    }
    381381
     
    638638        $this->go_to( "/?p=$post_id&post_type=$type" );
    639639
    640         $this->assertEquals( $post_id, get_queried_object_id() );
     640        $this->assertSame( $post_id, get_queried_object_id() );
    641641
    642642        $output = wp_list_pages(
     
    649649
    650650        $this->assertNotEmpty( $output );
    651         $this->assertEquals( 2, substr_count( $output, 'class="page_item ' ) );
     651        $this->assertSame( 2, substr_count( $output, 'class="page_item ' ) );
    652652        $this->assertContains( 'current_page_item', $output );
    653         $this->assertEquals( 1, substr_count( $output, 'current_page_item' ) );
     653        $this->assertSame( 1, substr_count( $output, 'current_page_item' ) );
    654654
    655655        _unregister_post_type( $type );
     
    716716        $pages = get_pages(); // Database gets queried.
    717717
    718         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     718        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    719719
    720720        $num_queries = $wpdb->num_queries;
     
    722722        $pages = get_pages(); // Database should not get queried.
    723723
    724         $this->assertEquals( $num_queries, $wpdb->num_queries );
     724        $this->assertSame( $num_queries, $wpdb->num_queries );
    725725    }
    726726}
Note: See TracChangeset for help on using the changeset viewer.