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

    r47311 r48937  
    1111        $post = get_post( $id );
    1212        $this->assertInstanceOf( 'WP_Post', $post );
    13         $this->assertEquals( $id, $post->ID );
     13        $this->assertSame( $id, $post->ID );
    1414        $this->assertTrue( isset( $post->ancestors ) );
    15         $this->assertEquals( array(), $post->ancestors );
     15        $this->assertSame( array(), $post->ancestors );
    1616
    1717        // Unset and then verify that the magic method fills the property again.
    1818        unset( $post->ancestors );
    19         $this->assertEquals( array(), $post->ancestors );
     19        $this->assertSame( array(), $post->ancestors );
    2020
    2121        // Magic get should make meta accessible as properties.
    2222        add_post_meta( $id, 'test', 'test' );
    23         $this->assertEquals( 'test', get_post_meta( $id, 'test', true ) );
    24         $this->assertEquals( 'test', $post->test );
     23        $this->assertSame( 'test', get_post_meta( $id, 'test', true ) );
     24        $this->assertSame( 'test', $post->test );
    2525
    2626        // Make sure meta does not eclipse true properties.
    2727        add_post_meta( $id, 'post_type', 'dummy' );
    28         $this->assertEquals( 'dummy', get_post_meta( $id, 'post_type', true ) );
    29         $this->assertEquals( 'post', $post->post_type );
     28        $this->assertSame( 'dummy', get_post_meta( $id, 'post_type', true ) );
     29        $this->assertSame( 'post', $post->post_type );
    3030
    3131        // Excercise the output argument.
    3232        $post = get_post( $id, ARRAY_A );
    3333        $this->assertInternalType( 'array', $post );
    34         $this->assertEquals( 'post', $post['post_type'] );
     34        $this->assertSame( 'post', $post['post_type'] );
    3535
    3636        $post = get_post( $id, ARRAY_N );
     
    4242        $post = get_post( $post, ARRAY_A );
    4343        $this->assertInternalType( 'array', $post );
    44         $this->assertEquals( 'post', $post['post_type'] );
    45         $this->assertEquals( $id, $post['ID'] );
     44        $this->assertSame( 'post', $post['post_type'] );
     45        $this->assertSame( $id, $post['ID'] );
    4646
    4747        // Should default to OBJECT when given invalid output argument.
    4848        $post = get_post( $id, 'invalid-output-value' );
    4949        $this->assertInstanceOf( 'WP_Post', $post );
    50         $this->assertEquals( $id, $post->ID );
     50        $this->assertSame( $id, $post->ID );
    5151
    5252        // Make sure stdClass in $GLOBALS['post'] is handled.
     
    5757        $post            = get_post( null );
    5858        $this->assertInstanceOf( 'WP_Post', $post );
    59         $this->assertEquals( $id, $post->ID );
     59        $this->assertSame( $id, $post->ID );
    6060        unset( $GLOBALS['post'] );
    6161
     
    7777            )
    7878        );
    79         $this->assertEquals( $updated, $child_id );
     79        $this->assertSame( $updated, $child_id );
    8080        $updated = wp_update_post(
    8181            array(
     
    8484            )
    8585        );
    86         $this->assertEquals( $updated, $grandchild_id );
    87 
    88         $this->assertEquals( array( $parent_id ), get_post( $child_id )->ancestors );
    89         $this->assertEquals( array( $parent_id ), get_post_ancestors( $child_id ) );
    90         $this->assertEquals( array( $parent_id ), get_post_ancestors( get_post( $child_id ) ) );
    91 
    92         $this->assertEquals( array( $child_id, $parent_id ), get_post( $grandchild_id )->ancestors );
    93         $this->assertEquals( array( $child_id, $parent_id ), get_post_ancestors( $grandchild_id ) );
    94         $this->assertEquals( array( $child_id, $parent_id ), get_post_ancestors( get_post( $grandchild_id ) ) );
    95 
    96         $this->assertEquals( array(), get_post( $parent_id )->ancestors );
    97         $this->assertEquals( array(), get_post_ancestors( $parent_id ) );
    98         $this->assertEquals( array(), get_post_ancestors( get_post( $parent_id ) ) );
     86        $this->assertSame( $updated, $grandchild_id );
     87
     88        $this->assertSame( array( $parent_id ), get_post( $child_id )->ancestors );
     89        $this->assertSame( array( $parent_id ), get_post_ancestors( $child_id ) );
     90        $this->assertSame( array( $parent_id ), get_post_ancestors( get_post( $child_id ) ) );
     91
     92        $this->assertSame( array( $child_id, $parent_id ), get_post( $grandchild_id )->ancestors );
     93        $this->assertSame( array( $child_id, $parent_id ), get_post_ancestors( $grandchild_id ) );
     94        $this->assertSame( array( $child_id, $parent_id ), get_post_ancestors( get_post( $grandchild_id ) ) );
     95
     96        $this->assertSame( array(), get_post( $parent_id )->ancestors );
     97        $this->assertSame( array(), get_post_ancestors( $parent_id ) );
     98        $this->assertSame( array(), get_post_ancestors( get_post( $parent_id ) ) );
    9999    }
    100100
     
    105105        foreach ( array( null, 0, false, '0', '' ) as $post_id ) {
    106106            $this->assertInternalType( 'array', get_post_ancestors( $post_id ) );
    107             $this->assertEquals( array(), get_post_ancestors( $post_id ) );
     107            $this->assertSame( array(), get_post_ancestors( $post_id ) );
    108108        }
    109109    }
     
    114114
    115115        $this->assertInternalType( 'array', $post->post_category );
    116         $this->assertEquals( 1, count( $post->post_category ) );
     116        $this->assertSame( 1, count( $post->post_category ) );
    117117        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
    118118        $term1 = wp_insert_term( 'Foo', 'category' );
     
    120120        $term3 = wp_insert_term( 'Baz', 'category' );
    121121        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'], $term3['term_id'] ) );
    122         $this->assertEquals( 3, count( $post->post_category ) );
    123         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
     122        $this->assertSame( 3, count( $post->post_category ) );
     123        $this->assertSame( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post->post_category );
    124124
    125125        $post = get_post( $post_id, ARRAY_A );
    126         $this->assertEquals( 3, count( $post['post_category'] ) );
    127         $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post['post_category'] );
     126        $this->assertSame( 3, count( $post['post_category'] ) );
     127        $this->assertSame( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ), $post['post_category'] );
    128128    }
    129129
     
    136136        wp_set_post_tags( $post_id, 'Foo, Bar, Baz' );
    137137        $this->assertInternalType( 'array', $post->tags_input );
    138         $this->assertEquals( 3, count( $post->tags_input ) );
    139         $this->assertEquals( array( 'Bar', 'Baz', 'Foo' ), $post->tags_input );
     138        $this->assertSame( 3, count( $post->tags_input ) );
     139        $this->assertSame( array( 'Bar', 'Baz', 'Foo' ), $post->tags_input );
    140140
    141141        $post = get_post( $post_id, ARRAY_A );
    142142        $this->assertInternalType( 'array', $post['tags_input'] );
    143         $this->assertEquals( 3, count( $post['tags_input'] ) );
    144         $this->assertEquals( array( 'Bar', 'Baz', 'Foo' ), $post['tags_input'] );
     143        $this->assertSame( 3, count( $post['tags_input'] ) );
     144        $this->assertSame( array( 'Bar', 'Baz', 'Foo' ), $post['tags_input'] );
    145145    }
    146146
     
    151151        $this->assertInternalType( 'string', $post->page_template );
    152152        $template = get_post_meta( $post->ID, '_wp_page_template', true );
    153         $this->assertEquals( $template, $post->page_template );
     153        $this->assertSame( $template, $post->page_template );
    154154        update_post_meta( $post_id, '_wp_page_template', 'foo.php' );
    155155        $template = get_post_meta( $post->ID, '_wp_page_template', true );
    156         $this->assertEquals( 'foo.php', $template );
    157         $this->assertEquals( $template, $post->page_template );
     156        $this->assertSame( 'foo.php', $template );
     157        $this->assertSame( $template, $post->page_template );
    158158    }
    159159
     
    167167        );
    168168
    169         $this->assertEquals( 'raw', $post->filter );
     169        $this->assertSame( 'raw', $post->filter );
    170170        $this->assertInternalType( 'int', $post->post_parent );
    171171
    172172        $display_post = get_post( $post, OBJECT, 'js' );
    173         $this->assertEquals( 'js', $display_post->filter );
    174         $this->assertEquals( esc_js( "Mary's home" ), $display_post->post_title );
     173        $this->assertSame( 'js', $display_post->filter );
     174        $this->assertSame( esc_js( "Mary's home" ), $display_post->post_title );
    175175
    176176        // Pass a js filtered WP_Post to get_post() with the filter set to raw.
    177177        // The post should be fetched from cache instead of using the passed object.
    178178        $raw_post = get_post( $display_post, OBJECT, 'raw' );
    179         $this->assertEquals( 'raw', $raw_post->filter );
     179        $this->assertSame( 'raw', $raw_post->filter );
    180180        $this->assertNotEquals( esc_js( "Mary's home" ), $raw_post->post_title );
    181181
    182182        $raw_post->filter( 'js' );
    183         $this->assertEquals( 'js', $post->filter );
    184         $this->assertEquals( esc_js( "Mary's home" ), $raw_post->post_title );
     183        $this->assertSame( 'js', $post->filter );
     184        $this->assertSame( esc_js( "Mary's home" ), $raw_post->post_title );
    185185    }
    186186
     
    190190        $post->foo = 'bar';
    191191
    192         $this->assertEquals( 'bar', get_post( $post )->foo );
    193         $this->assertEquals( 'bar', get_post( $post, OBJECT, 'display' )->foo );
     192        $this->assertSame( 'bar', get_post( $post )->foo );
     193        $this->assertSame( 'bar', get_post( $post, OBJECT, 'display' )->foo );
    194194    }
    195195
     
    199199        $post = get_post( $id, ARRAY_A );
    200200
    201         $this->assertEquals( $id, $post['ID'] );
     201        $this->assertSame( $id, $post['ID'] );
    202202        $this->assertInternalType( 'array', $post['ancestors'] );
    203         $this->assertEquals( 'raw', $post['filter'] );
     203        $this->assertSame( 'raw', $post['filter'] );
    204204    }
    205205
Note: See TracChangeset for help on using the changeset viewer.