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

    r48840 r48937  
    5252        // Ensure that authors were imported correctly.
    5353        $user_count = count_users();
    54         $this->assertEquals( 3, $user_count['total_users'] );
     54        $this->assertSame( 3, $user_count['total_users'] );
    5555        $admin = get_user_by( 'login', 'admin' );
    56         $this->assertEquals( 'admin', $admin->user_login );
    57         $this->assertEquals( 'local@host.null', $admin->user_email );
     56        $this->assertSame( 'admin', $admin->user_login );
     57        $this->assertSame( 'local@host.null', $admin->user_email );
    5858        $editor = get_user_by( 'login', 'editor' );
    59         $this->assertEquals( 'editor', $editor->user_login );
    60         $this->assertEquals( 'editor@example.org', $editor->user_email );
    61         $this->assertEquals( 'FirstName', $editor->user_firstname );
    62         $this->assertEquals( 'LastName', $editor->user_lastname );
     59        $this->assertSame( 'editor', $editor->user_login );
     60        $this->assertSame( 'editor@example.org', $editor->user_email );
     61        $this->assertSame( 'FirstName', $editor->user_firstname );
     62        $this->assertSame( 'LastName', $editor->user_lastname );
    6363        $author = get_user_by( 'login', 'author' );
    64         $this->assertEquals( 'author', $author->user_login );
    65         $this->assertEquals( 'author@example.org', $author->user_email );
     64        $this->assertSame( 'author', $author->user_login );
     65        $this->assertSame( 'author@example.org', $author->user_email );
    6666
    6767        // Check that terms were imported correctly.
     
    6969        $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) );
    7070        $foo = get_term_by( 'slug', 'foo', 'category' );
    71         $this->assertEquals( 0, $foo->parent );
     71        $this->assertSame( 0, $foo->parent );
    7272        $bar     = get_term_by( 'slug', 'bar', 'category' );
    7373        $foo_bar = get_term_by( 'slug', 'foo-bar', 'category' );
    74         $this->assertEquals( $bar->term_id, $foo_bar->parent );
     74        $this->assertSame( $bar->term_id, $foo_bar->parent );
    7575
    7676        // Check that posts/pages were imported correctly.
     
    9292            )
    9393        );
    94         $this->assertEquals( 11, count( $posts ) );
     94        $this->assertSame( 11, count( $posts ) );
    9595
    9696        $post = $posts[0];
    97         $this->assertEquals( 'Many Categories', $post->post_title );
    98         $this->assertEquals( 'many-categories', $post->post_name );
    99         $this->assertEquals( $admin->ID, $post->post_author );
    100         $this->assertEquals( 'post', $post->post_type );
    101         $this->assertEquals( 'publish', $post->post_status );
    102         $this->assertEquals( 0, $post->post_parent );
    103         $cats = wp_get_post_categories( $post->ID );
    104         $this->assertEquals( 27, count( $cats ) );
     97        $this->assertSame( 'Many Categories', $post->post_title );
     98        $this->assertSame( 'many-categories', $post->post_name );
     99        $this->assertEquals( $admin->ID, $post->post_author );
     100        $this->assertSame( 'post', $post->post_type );
     101        $this->assertSame( 'publish', $post->post_status );
     102        $this->assertSame( 0, $post->post_parent );
     103        $cats = wp_get_post_categories( $post->ID );
     104        $this->assertSame( 27, count( $cats ) );
    105105
    106106        $post = $posts[1];
    107         $this->assertEquals( 'Non-standard post format', $post->post_title );
    108         $this->assertEquals( 'non-standard-post-format', $post->post_name );
    109         $this->assertEquals( $admin->ID, $post->post_author );
    110         $this->assertEquals( 'post', $post->post_type );
    111         $this->assertEquals( 'publish', $post->post_status );
    112         $this->assertEquals( 0, $post->post_parent );
    113         $cats = wp_get_post_categories( $post->ID );
    114         $this->assertEquals( 1, count( $cats ) );
     107        $this->assertSame( 'Non-standard post format', $post->post_title );
     108        $this->assertSame( 'non-standard-post-format', $post->post_name );
     109        $this->assertEquals( $admin->ID, $post->post_author );
     110        $this->assertSame( 'post', $post->post_type );
     111        $this->assertSame( 'publish', $post->post_status );
     112        $this->assertSame( 0, $post->post_parent );
     113        $cats = wp_get_post_categories( $post->ID );
     114        $this->assertSame( 1, count( $cats ) );
    115115        $this->assertTrue( has_post_format( 'aside', $post->ID ) );
    116116
    117117        $post = $posts[2];
    118         $this->assertEquals( 'Top-level Foo', $post->post_title );
    119         $this->assertEquals( 'top-level-foo', $post->post_name );
    120         $this->assertEquals( $admin->ID, $post->post_author );
    121         $this->assertEquals( 'post', $post->post_type );
    122         $this->assertEquals( 'publish', $post->post_status );
    123         $this->assertEquals( 0, $post->post_parent );
     118        $this->assertSame( 'Top-level Foo', $post->post_title );
     119        $this->assertSame( 'top-level-foo', $post->post_name );
     120        $this->assertEquals( $admin->ID, $post->post_author );
     121        $this->assertSame( 'post', $post->post_type );
     122        $this->assertSame( 'publish', $post->post_status );
     123        $this->assertSame( 0, $post->post_parent );
    124124        $cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );
    125         $this->assertEquals( 1, count( $cats ) );
    126         $this->assertEquals( 'foo', $cats[0]->slug );
     125        $this->assertSame( 1, count( $cats ) );
     126        $this->assertSame( 'foo', $cats[0]->slug );
    127127
    128128        $post = $posts[3];
    129         $this->assertEquals( 'Foo-child', $post->post_title );
    130         $this->assertEquals( 'foo-child', $post->post_name );
     129        $this->assertSame( 'Foo-child', $post->post_title );
     130        $this->assertSame( 'foo-child', $post->post_name );
    131131        $this->assertEquals( $editor->ID, $post->post_author );
    132         $this->assertEquals( 'post', $post->post_type );
    133         $this->assertEquals( 'publish', $post->post_status );
    134         $this->assertEquals( 0, $post->post_parent );
     132        $this->assertSame( 'post', $post->post_type );
     133        $this->assertSame( 'publish', $post->post_status );
     134        $this->assertSame( 0, $post->post_parent );
    135135        $cats = wp_get_post_categories( $post->ID, array( 'fields' => 'all' ) );
    136         $this->assertEquals( 1, count( $cats ) );
    137         $this->assertEquals( 'foo-bar', $cats[0]->slug );
     136        $this->assertSame( 1, count( $cats ) );
     137        $this->assertSame( 'foo-bar', $cats[0]->slug );
    138138
    139139        $post = $posts[4];
    140         $this->assertEquals( 'Private Post', $post->post_title );
    141         $this->assertEquals( 'private-post', $post->post_name );
    142         $this->assertEquals( $admin->ID, $post->post_author );
    143         $this->assertEquals( 'post', $post->post_type );
    144         $this->assertEquals( 'private', $post->post_status );
    145         $this->assertEquals( 0, $post->post_parent );
    146         $cats = wp_get_post_categories( $post->ID );
    147         $this->assertEquals( 1, count( $cats ) );
     140        $this->assertSame( 'Private Post', $post->post_title );
     141        $this->assertSame( 'private-post', $post->post_name );
     142        $this->assertEquals( $admin->ID, $post->post_author );
     143        $this->assertSame( 'post', $post->post_type );
     144        $this->assertSame( 'private', $post->post_status );
     145        $this->assertSame( 0, $post->post_parent );
     146        $cats = wp_get_post_categories( $post->ID );
     147        $this->assertSame( 1, count( $cats ) );
    148148        $tags = wp_get_post_tags( $post->ID );
    149         $this->assertEquals( 3, count( $tags ) );
    150         $this->assertEquals( 'tag1', $tags[0]->slug );
    151         $this->assertEquals( 'tag2', $tags[1]->slug );
    152         $this->assertEquals( 'tag3', $tags[2]->slug );
     149        $this->assertSame( 3, count( $tags ) );
     150        $this->assertSame( 'tag1', $tags[0]->slug );
     151        $this->assertSame( 'tag2', $tags[1]->slug );
     152        $this->assertSame( 'tag3', $tags[2]->slug );
    153153
    154154        $post = $posts[5];
    155         $this->assertEquals( '1-col page', $post->post_title );
    156         $this->assertEquals( '1-col-page', $post->post_name );
    157         $this->assertEquals( $admin->ID, $post->post_author );
    158         $this->assertEquals( 'page', $post->post_type );
    159         $this->assertEquals( 'publish', $post->post_status );
    160         $this->assertEquals( 0, $post->post_parent );
    161         $this->assertEquals( 'onecolumn-page.php', get_post_meta( $post->ID, '_wp_page_template', true ) );
     155        $this->assertSame( '1-col page', $post->post_title );
     156        $this->assertSame( '1-col-page', $post->post_name );
     157        $this->assertEquals( $admin->ID, $post->post_author );
     158        $this->assertSame( 'page', $post->post_type );
     159        $this->assertSame( 'publish', $post->post_status );
     160        $this->assertSame( 0, $post->post_parent );
     161        $this->assertSame( 'onecolumn-page.php', get_post_meta( $post->ID, '_wp_page_template', true ) );
    162162
    163163        $post = $posts[6];
    164         $this->assertEquals( 'Draft Page', $post->post_title );
    165         $this->assertEquals( '', $post->post_name );
    166         $this->assertEquals( $admin->ID, $post->post_author );
    167         $this->assertEquals( 'page', $post->post_type );
    168         $this->assertEquals( 'draft', $post->post_status );
    169         $this->assertEquals( 0, $post->post_parent );
    170         $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
     164        $this->assertSame( 'Draft Page', $post->post_title );
     165        $this->assertSame( '', $post->post_name );
     166        $this->assertEquals( $admin->ID, $post->post_author );
     167        $this->assertSame( 'page', $post->post_type );
     168        $this->assertSame( 'draft', $post->post_status );
     169        $this->assertSame( 0, $post->post_parent );
     170        $this->assertSame( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
    171171
    172172        $post = $posts[7];
    173         $this->assertEquals( 'Parent Page', $post->post_title );
    174         $this->assertEquals( 'parent-page', $post->post_name );
    175         $this->assertEquals( $admin->ID, $post->post_author );
    176         $this->assertEquals( 'page', $post->post_type );
    177         $this->assertEquals( 'publish', $post->post_status );
    178         $this->assertEquals( 0, $post->post_parent );
    179         $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
     173        $this->assertSame( 'Parent Page', $post->post_title );
     174        $this->assertSame( 'parent-page', $post->post_name );
     175        $this->assertEquals( $admin->ID, $post->post_author );
     176        $this->assertSame( 'page', $post->post_type );
     177        $this->assertSame( 'publish', $post->post_status );
     178        $this->assertSame( 0, $post->post_parent );
     179        $this->assertSame( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
    180180
    181181        $post = $posts[8];
    182         $this->assertEquals( 'Child Page', $post->post_title );
    183         $this->assertEquals( 'child-page', $post->post_name );
    184         $this->assertEquals( $admin->ID, $post->post_author );
    185         $this->assertEquals( 'page', $post->post_type );
    186         $this->assertEquals( 'publish', $post->post_status );
    187         $this->assertEquals( $posts[7]->ID, $post->post_parent );
    188         $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
     182        $this->assertSame( 'Child Page', $post->post_title );
     183        $this->assertSame( 'child-page', $post->post_name );
     184        $this->assertEquals( $admin->ID, $post->post_author );
     185        $this->assertSame( 'page', $post->post_type );
     186        $this->assertSame( 'publish', $post->post_status );
     187        $this->assertSame( $posts[7]->ID, $post->post_parent );
     188        $this->assertSame( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
    189189
    190190        $post = $posts[9];
    191         $this->assertEquals( 'Sample Page', $post->post_title );
    192         $this->assertEquals( 'sample-page', $post->post_name );
    193         $this->assertEquals( $admin->ID, $post->post_author );
    194         $this->assertEquals( 'page', $post->post_type );
    195         $this->assertEquals( 'publish', $post->post_status );
    196         $this->assertEquals( 0, $post->post_parent );
    197         $this->assertEquals( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
     191        $this->assertSame( 'Sample Page', $post->post_title );
     192        $this->assertSame( 'sample-page', $post->post_name );
     193        $this->assertEquals( $admin->ID, $post->post_author );
     194        $this->assertSame( 'page', $post->post_type );
     195        $this->assertSame( 'publish', $post->post_status );
     196        $this->assertSame( 0, $post->post_parent );
     197        $this->assertSame( 'default', get_post_meta( $post->ID, '_wp_page_template', true ) );
    198198
    199199        $post = $posts[10];
    200         $this->assertEquals( 'Hello world!', $post->post_title );
    201         $this->assertEquals( 'hello-world', $post->post_name );
     200        $this->assertSame( 'Hello world!', $post->post_title );
     201        $this->assertSame( 'hello-world', $post->post_name );
    202202        $this->assertEquals( $author->ID, $post->post_author );
    203         $this->assertEquals( 'post', $post->post_type );
    204         $this->assertEquals( 'publish', $post->post_status );
    205         $this->assertEquals( 0, $post->post_parent );
    206         $cats = wp_get_post_categories( $post->ID );
    207         $this->assertEquals( 1, count( $cats ) );
     203        $this->assertSame( 'post', $post->post_type );
     204        $this->assertSame( 'publish', $post->post_status );
     205        $this->assertSame( 0, $post->post_parent );
     206        $cats = wp_get_post_categories( $post->ID );
     207        $this->assertSame( 1, count( $cats ) );
    208208    }
    209209
     
    218218
    219219        $user_count = count_users();
    220         $this->assertEquals( 3, $user_count['total_users'] );
     220        $this->assertSame( 3, $user_count['total_users'] );
    221221        $admin = get_user_by( 'login', 'admin' );
    222         $this->assertEquals( 'admin', $admin->user_login );
    223         $this->assertEquals( 'local@host.null', $admin->user_email );
     222        $this->assertSame( 'admin', $admin->user_login );
     223        $this->assertSame( 'local@host.null', $admin->user_email );
    224224        $editor = get_user_by( 'login', 'editor' );
    225         $this->assertEquals( 'editor', $editor->user_login );
    226         $this->assertEquals( 'editor@example.org', $editor->user_email );
    227         $this->assertEquals( 'FirstName', $editor->user_firstname );
    228         $this->assertEquals( 'LastName', $editor->user_lastname );
     225        $this->assertSame( 'editor', $editor->user_login );
     226        $this->assertSame( 'editor@example.org', $editor->user_email );
     227        $this->assertSame( 'FirstName', $editor->user_firstname );
     228        $this->assertSame( 'LastName', $editor->user_lastname );
    229229        $author = get_user_by( 'login', 'author' );
    230         $this->assertEquals( 'author', $author->user_login );
    231         $this->assertEquals( 'author@example.org', $author->user_email );
     230        $this->assertSame( 'author', $author->user_login );
     231        $this->assertSame( 'author@example.org', $author->user_email );
    232232
    233233        $this->assertEquals( 30, wp_count_terms( array( 'taxonomy' => 'category' ) ) );
    234234        $this->assertEquals( 3, wp_count_terms( array( 'taxonomy' => 'post_tag' ) ) );
    235235        $foo = get_term_by( 'slug', 'foo', 'category' );
    236         $this->assertEquals( 0, $foo->parent );
     236        $this->assertSame( 0, $foo->parent );
    237237        $bar     = get_term_by( 'slug', 'bar', 'category' );
    238238        $foo_bar = get_term_by( 'slug', 'foo-bar', 'category' );
    239         $this->assertEquals( $bar->term_id, $foo_bar->parent );
     239        $this->assertSame( $bar->term_id, $foo_bar->parent );
    240240
    241241        $post_count = wp_count_posts( 'post' );
     
    259259            'def1' => array( 'def1' ),
    260260        );
    261         $this->assertEquals(
     261        $this->assertSame(
    262262            array(
    263263                'ABC1' => array( 'ABC1' ),
Note: See TracChangeset for help on using the changeset viewer.