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/term/cache.php

    r47122 r48937  
    2020        $hierarchy      = _get_term_hierarchy( 'category' );
    2121
    22         $this->assertEquals( array( $term_id1 => array( $term_id1_child ) ), $hierarchy );
     22        $this->assertSame( array( $term_id1 => array( $term_id1_child ) ), $hierarchy );
    2323
    2424        // Add another Parent => Child.
     
    2727        $hierarchy      = _get_term_hierarchy( 'category' );
    2828
    29         $this->assertEquals(
     29        $this->assertSame(
    3030            array(
    3131                $term_id1 => array( $term_id1_child ),
     
    4646        $post  = get_post( $post_id );
    4747        $cats1 = get_the_category( $post->ID );
    48         $this->assertEquals( $term->name, reset( $cats1 )->name );
     48        $this->assertSame( $term->name, reset( $cats1 )->name );
    4949
    5050        wp_update_term( $term->term_id, 'category', array( 'name' => 'Bar' ) );
     
    8484
    8585            $terms = get_terms( $tax, array( 'hide_empty' => false ) );
    86             $this->assertEquals( $i, count( $terms ) );
     86            $this->assertSame( $i, count( $terms ) );
    8787            if ( $i > 1 ) {
    8888                $hierarchy = _get_term_hierarchy( $tax );
    8989                $this->assertNotEmpty( $hierarchy );
    90                 $this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
     90                $this->assertSame( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
    9191            }
    9292
     
    126126        // No new queries should have fired.
    127127        $this->assertSame( $num_queries, $wpdb->num_queries );
    128         $this->assertEquals( $term_object, $term_object_2 );
     128        $this->assertSame( $term_object, $term_object_2 );
    129129    }
    130130
     
    258258        $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    259259        $num_queries++;
    260         $this->assertEquals( 'Taco', $term->name );
    261         $this->assertEquals( $num_queries, $wpdb->num_queries );
     260        $this->assertSame( 'Taco', $term->name );
     261        $this->assertSame( $num_queries, $wpdb->num_queries );
    262262
    263263        // This should now hit cache.
    264264        $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    265         $this->assertEquals( 'Taco', $term->name );
    266         $this->assertEquals( $num_queries, $wpdb->num_queries );
     265        $this->assertSame( 'Taco', $term->name );
     266        $this->assertSame( $num_queries, $wpdb->num_queries );
    267267
    268268        $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
    269         $this->assertEquals( $num_queries, $wpdb->num_queries );
     269        $this->assertSame( $num_queries, $wpdb->num_queries );
    270270    }
    271271
     
    289289        $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    290290        $num_queries++;
    291         $this->assertEquals( 'Taco', $term->name );
    292         $this->assertEquals( $num_queries, $wpdb->num_queries );
     291        $this->assertSame( 'Taco', $term->name );
     292        $this->assertSame( $num_queries, $wpdb->num_queries );
    293293
    294294        // This should now hit cache.
    295295        $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    296         $this->assertEquals( 'Taco', $term->name );
    297         $this->assertEquals( $num_queries, $wpdb->num_queries );
     296        $this->assertSame( 'Taco', $term->name );
     297        $this->assertSame( $num_queries, $wpdb->num_queries );
    298298
    299299        // Update the tag which invalidates the cache.
     
    304304        $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    305305        $num_queries++;
    306         $this->assertEquals( 'No Taco', $term->name );
    307         $this->assertEquals( $num_queries, $wpdb->num_queries );
     306        $this->assertSame( 'No Taco', $term->name );
     307        $this->assertSame( $num_queries, $wpdb->num_queries );
    308308    }
    309309
     
    327327        get_term_by( 'name', 'Burrito', 'post_tag' );
    328328        $num_queries++;
    329         $this->assertEquals( $num_queries, $wpdb->num_queries );
     329        $this->assertSame( $num_queries, $wpdb->num_queries );
    330330
    331331        // This should now hit cache.
    332332        $term = get_term_by( 'name', 'Burrito', 'post_tag' );
    333         $this->assertEquals( $num_queries, $wpdb->num_queries );
     333        $this->assertSame( $num_queries, $wpdb->num_queries );
    334334
    335335        $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
    336         $this->assertEquals( $num_queries, $wpdb->num_queries );
     336        $this->assertSame( $num_queries, $wpdb->num_queries );
    337337    }
    338338
     
    356356        get_term_by( 'name', 'Burrito', 'post_tag' );
    357357        $num_queries++;
    358         $this->assertEquals( $num_queries, $wpdb->num_queries );
     358        $this->assertSame( $num_queries, $wpdb->num_queries );
    359359
    360360        // This should now hit cache.
    361361        get_term_by( 'name', 'Burrito', 'post_tag' );
    362         $this->assertEquals( $num_queries, $wpdb->num_queries );
     362        $this->assertSame( $num_queries, $wpdb->num_queries );
    363363
    364364        // Update the tag which invalidates the cache.
     
    369369        get_term_by( 'name', 'burrito', 'post_tag' );
    370370        $num_queries++;
    371         $this->assertEquals( $num_queries, $wpdb->num_queries );
     371        $this->assertSame( $num_queries, $wpdb->num_queries );
    372372    }
    373373
     
    394394        // Verify the term is cached.
    395395        $term2 = get_term_by( 'name', 'Burrito', 'post_tag' );
    396         $this->assertEquals( $num_queries, $wpdb->num_queries );
     396        $this->assertSame( $num_queries, $wpdb->num_queries );
    397397        $this->assertEquals( $term1, $term2 );
    398398
     
    405405        // Verify that the cached term still matches the initial cached term.
    406406        $term3 = get_term_by( 'name', 'Burrito', 'post_tag' );
    407         $this->assertEquals( $num_queries, $wpdb->num_queries );
     407        $this->assertSame( $num_queries, $wpdb->num_queries );
    408408        $this->assertEquals( $term1, $term3 );
    409409
     
    436436        $this->assertTrue( $term instanceof WP_Term );
    437437        $this->assertSame( $term_id, $term->term_id );
    438         $this->assertEquals( $num_queries, $wpdb->num_queries );
     438        $this->assertSame( $num_queries, $wpdb->num_queries );
    439439
    440440        $term_meta = get_term_meta( $term_id, 'foo', true );
    441441        $num_queries++;
    442442        $this->assertSame( $term_meta, 'bar' );
    443         $this->assertEquals( $num_queries, $wpdb->num_queries );
     443        $this->assertSame( $num_queries, $wpdb->num_queries );
    444444    }
    445445
Note: See TracChangeset for help on using the changeset viewer.