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

    r48840 r48937  
    102102
    103103        $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
    104         $this->assertEquals( 1, count( $tt ) );
    105         $this->assertEquals( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
     104        $this->assertSame( 1, count( $tt ) );
     105        $this->assertSame( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
    106106
    107107        $three_tags = array( $tags[0], $tags[1], $tags[2] );
    108108        $tt         = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
    109         $this->assertEquals( 3, count( $tt ) );
    110         $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
     109        $this->assertSame( 3, count( $tt ) );
     110        $this->assertSame( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
    111111
    112112        $this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
     
    114114        $this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
    115115        $this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
    116         $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
     116        $this->assertSame( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
    117117
    118118        foreach ( $tags as $term_id ) {
     
    178178
    179179        $this->assertInternalType( 'array', $post->post_category );
    180         $this->assertEquals( 1, count( $post->post_category ) );
     180        $this->assertSame( 1, count( $post->post_category ) );
    181181        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    182182
     
    186186
    187187        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
    188         $this->assertEquals( 2, count( $post->post_category ) );
    189         $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ), $post->post_category );
     188        $this->assertSame( 2, count( $post->post_category ) );
     189        $this->assertSame( array( $term2['term_id'], $term1['term_id'] ), $post->post_category );
    190190
    191191        wp_set_post_categories( $post_id, $term3['term_id'], true );
    192         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
     192        $this->assertSame( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
    193193
    194194        $term4 = wp_insert_term( 'Burrito', 'category' );
    195195
    196196        wp_set_post_categories( $post_id, $term4['term_id'] );
    197         $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
     197        $this->assertSame( array( $term4['term_id'] ), $post->post_category );
    198198
    199199        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
    200         $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
     200        $this->assertSame( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
    201201
    202202        wp_set_post_categories( $post_id, array(), true );
    203         $this->assertEquals( 1, count( $post->post_category ) );
     203        $this->assertSame( 1, count( $post->post_category ) );
    204204        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    205205
    206206        wp_set_post_categories( $post_id, array() );
    207         $this->assertEquals( 1, count( $post->post_category ) );
     207        $this->assertSame( 1, count( $post->post_category ) );
    208208        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    209209    }
     
    225225
    226226        wp_set_post_categories( $post_id, $term['term_id'] );
    227         $this->assertEquals( $term['term_id'], $post->post_category[0] );
     227        $this->assertSame( $term['term_id'], $post->post_category[0] );
    228228
    229229        wp_set_post_categories( $post_id, array() );
     
    244244        $term = wp_insert_term( 'foo', $this->taxonomy );
    245245
    246         $this->assertEquals( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) );
    247         $this->assertEquals( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) );
    248         $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
    249         $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
     246        $this->assertSame( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) );
     247        $this->assertSame( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) );
     248        $this->assertSame( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
     249        $this->assertSame( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
    250250    }
    251251
Note: See TracChangeset for help on using the changeset viewer.