Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 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/term/getTerms.php

    r48663 r48937  
    116116        // last_changed and num_queries should bump.
    117117        $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
    118         $this->assertEquals( 3, count( $terms ) );
     118        $this->assertSame( 3, count( $terms ) );
    119119        $time1 = wp_cache_get( 'last_changed', 'terms' );
    120120        $this->assertNotEmpty( $time1 );
    121         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     121        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    122122
    123123        $num_queries = $wpdb->num_queries;
     
    125125        // Again. last_changed and num_queries should remain the same.
    126126        $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
    127         $this->assertEquals( 3, count( $terms ) );
    128         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    129         $this->assertEquals( $num_queries, $wpdb->num_queries );
     127        $this->assertSame( 3, count( $terms ) );
     128        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) );
     129        $this->assertSame( $num_queries, $wpdb->num_queries );
    130130    }
    131131
     
    145145        // num_queries should bump, last_changed should remain the same.
    146146        $terms = get_terms( 'post_tag', array( 'number' => 2 ) );
    147         $this->assertEquals( 2, count( $terms ) );
    148         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    149         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     147        $this->assertSame( 2, count( $terms ) );
     148        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) );
     149        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    150150
    151151        $num_queries = $wpdb->num_queries;
     
    153153        // Again. last_changed and num_queries should remain the same.
    154154        $terms = get_terms( 'post_tag', array( 'number' => 2 ) );
    155         $this->assertEquals( 2, count( $terms ) );
    156         $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
    157         $this->assertEquals( $num_queries, $wpdb->num_queries );
     155        $this->assertSame( 2, count( $terms ) );
     156        $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) );
     157        $this->assertSame( $num_queries, $wpdb->num_queries );
    158158    }
    159159
     
    180180        // last_changed and num_queries should bump after a term is deleted.
    181181        $terms = get_terms( 'post_tag' );
    182         $this->assertEquals( 2, count( $terms ) );
    183         $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
    184         $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     182        $this->assertSame( 2, count( $terms ) );
     183        $this->assertSame( $time2, wp_cache_get( 'last_changed', 'terms' ) );
     184        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    185185
    186186        $num_queries = $wpdb->num_queries;
     
    188188        // Again. last_changed and num_queries should remain the same.
    189189        $terms = get_terms( 'post_tag' );
    190         $this->assertEquals( 2, count( $terms ) );
    191         $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
    192         $this->assertEquals( $num_queries, $wpdb->num_queries );
     190        $this->assertSame( 2, count( $terms ) );
     191        $this->assertSame( $time2, wp_cache_get( 'last_changed', 'terms' ) );
     192        $this->assertSame( $num_queries, $wpdb->num_queries );
    193193
    194194        // @todo Repeat with term insert and update.
     
    201201        $term_id = self::factory()->tag->create();
    202202        $terms   = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) );
    203         $this->assertEquals( $term_id, reset( $terms )->term_id );
     203        $this->assertSame( $term_id, reset( $terms )->term_id );
    204204    }
    205205
     
    301301            )
    302302        );
    303         $this->assertEquals( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
     303        $this->assertSame( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
    304304
    305305        $exc_terms = get_terms(
     
    310310            )
    311311        );
    312         $this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
     312        $this->assertSame( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
    313313
    314314        // These should not generate query errors.
     
    364364        );
    365365
    366         $this->assertEquals( array( $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
     366        $this->assertSame( array( $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
    367367
    368368        _unregister_taxonomy( 'wptests_tax' );
     
    389389            )
    390390        );
    391         $this->assertEquals( array( $term_id1, $term_id11, $term_id2, $term_id22 ), $terms );
     391        $this->assertSame( array( $term_id1, $term_id11, $term_id2, $term_id22 ), $terms );
    392392
    393393        $terms = get_terms(
     
    400400        );
    401401
    402         $this->assertEquals( array( $term_id2, $term_id22 ), $terms );
     402        $this->assertSame( array( $term_id2, $term_id22 ), $terms );
    403403
    404404    }
     
    548548        }
    549549        $term = get_term( $cheddar, $tax );
    550         $this->assertEquals( 2, $term->count );
     550        $this->assertSame( 2, $term->count );
    551551
    552552        $brie    = self::factory()->term->create(
     
    560560        wp_set_post_terms( $post_id, $brie, $tax );
    561561        $term = get_term( $brie, $tax );
    562         $this->assertEquals( 1, $term->count );
     562        $this->assertSame( 1, $term->count );
    563563
    564564        $crackers = self::factory()->term->create(
     
    581581        }
    582582        $term = get_term( $butter, $tax );
    583         $this->assertEquals( 1, $term->count );
     583        $this->assertSame( 1, $term->count );
    584584
    585585        $multigrain = self::factory()->term->create(
     
    595595        }
    596596        $term = get_term( $multigrain, $tax );
    597         $this->assertEquals( 1, $term->count );
     597        $this->assertSame( 1, $term->count );
    598598
    599599        $fruit       = self::factory()->term->create(
     
    618618            )
    619619        );
    620         $this->assertEquals( 2, count( $terms ) );
    621         $this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
     620        $this->assertSame( 2, count( $terms ) );
     621        $this->assertSame( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
    622622    }
    623623
     
    652652        wp_set_post_terms( $post_id, $spread, $tax );
    653653        $term = get_term( $spread, $tax );
    654         $this->assertEquals( 1, $term->count );
     654        $this->assertSame( 1, $term->count );
    655655
    656656        $terms = get_terms(
     
    661661            )
    662662        );
    663         $this->assertEquals( 1, count( $terms ) );
    664         $this->assertEquals( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
     663        $this->assertSame( 1, count( $terms ) );
     664        $this->assertSame( array( 'Cheese' ), wp_list_pluck( $terms, 'name' ) );
    665665
    666666        _unregister_taxonomy( $tax );
     
    688688        wp_set_post_terms( $post_id, $t[7], $tax );
    689689        $term = get_term( $t[7], $tax );
    690         $this->assertEquals( 1, $term->count );
     690        $this->assertSame( 1, $term->count );
    691691
    692692        $terms = get_terms(
     
    697697            )
    698698        );
    699         $this->assertEquals( 1, count( $terms ) );
    700         $this->assertEquals( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
     699        $this->assertSame( 1, count( $terms ) );
     700        $this->assertSame( array( 'term1' ), wp_list_pluck( $terms, 'name' ) );
    701701
    702702        _unregister_taxonomy( $tax );
     
    717717            )
    718718        );
    719         $this->assertEquals( 1, count( $terms ) );
     719        $this->assertSame( 1, count( $terms ) );
    720720    }
    721721
     
    749749        );
    750750
    751         $this->assertEquals(
     751        $this->assertSame(
    752752            array(
    753753                $child  => 'Test 1',
     
    788788        );
    789789
    790         $this->assertEquals(
     790        $this->assertSame(
    791791            array(
    792792                $child  => 'test-1',
     
    861861        $term = get_term( $term['term_id'], 'category' );
    862862
    863         $this->assertEquals( $term->term_id, $term->parent );
     863        $this->assertSame( $term->term_id, $term->parent );
    864864        $this->assertInternalType( 'array', get_term_children( $term->term_id, 'category' ) );
    865865
     
    918918        );
    919919
    920         $this->assertEquals( array( $t1 ), $found );
     920        $this->assertSame( array( $t1 ), $found );
    921921    }
    922922
     
    938938        );
    939939
    940         $this->assertEquals( array( $t1, $t3 ), $found );
     940        $this->assertSame( array( $t1, $t3 ), $found );
    941941    }
    942942
     
    957957        );
    958958
    959         $this->assertEquals( array( $t1 ), $found );
     959        $this->assertSame( array( $t1 ), $found );
    960960    }
    961961
     
    18381838        _unregister_taxonomy( 'wptests_tax' );
    18391839
    1840         $this->assertEquals( array( $t4, $t1, $t2 ), $found );
     1840        $this->assertSame( array( $t4, $t1, $t2 ), $found );
    18411841    }
    18421842
     
    18841884        _unregister_taxonomy( 'wptests_tax' );
    18851885
    1886         $this->assertEquals( array( $t2, $t1, $t4, $t3 ), $found );
     1886        $this->assertSame( array( $t2, $t1, $t4, $t3 ), $found );
    18871887    }
    18881888
     
    19201920        );
    19211921
    1922         $this->assertEquals( array( $t1, $t2, $t3 ), $found );
     1922        $this->assertSame( array( $t1, $t2, $t3 ), $found );
    19231923    }
    19241924
     
    23092309
    23102310        // Verify that there are no children.
    2311         $this->assertEquals( 0, count( $terms ) );
     2311        $this->assertSame( 0, count( $terms ) );
    23122312    }
    23132313
     
    23752375
    23762376        // Verify that there are no children.
    2377         $this->assertEquals( 0, count( $terms ) );
     2377        $this->assertSame( 0, count( $terms ) );
    23782378    }
    23792379
     
    24882488
    24892489        // 'hierarchical=false' means that descendants are not fetched.
    2490         $this->assertEquals( 0, count( $terms ) );
     2490        $this->assertSame( 0, count( $terms ) );
    24912491    }
    24922492
     
    26272627        foreach ( $found as $f ) {
    26282628            if ( $t1 === $f->term_id ) {
    2629                 $this->assertEquals( 1, $f->count );
     2629                $this->assertSame( 1, $f->count );
    26302630            } elseif ( $t2 === $f->term_id ) {
    2631                 $this->assertEquals( 2, $f->count );
     2631                $this->assertSame( 2, $f->count );
    26322632            } else {
    2633                 $this->assertEquals( 1, $f->count );
     2633                $this->assertSame( 1, $f->count );
    26342634            }
    26352635        }
Note: See TracChangeset for help on using the changeset viewer.