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

    r47122 r48937  
    5353
    5454        // Check it exists.
    55         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique', true ) );
    56         $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique', false ) );
     55        $this->assertSame( 'value', get_post_meta( self::$post_id, 'unique', true ) );
     56        $this->assertSame( array( 'value' ), get_post_meta( self::$post_id, 'unique', false ) );
    5757
    5858        // Fail to delete the wrong value.
     
    6363
    6464        // Check it is deleted.
    65         $this->assertEquals( '', get_post_meta( self::$post_id, 'unique', true ) );
    66         $this->assertEquals( array(), get_post_meta( self::$post_id, 'unique', false ) );
     65        $this->assertSame( '', get_post_meta( self::$post_id, 'unique', true ) );
     66        $this->assertSame( array(), get_post_meta( self::$post_id, 'unique', false ) );
    6767
    6868    }
     
    7474
    7575        // Check they exist.
    76         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique', true ) );
    77         $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
     76        $this->assertSame( 'value', get_post_meta( self::$post_id, 'nonunique', true ) );
     77        $this->assertSame( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
    7878
    7979        // Fail to delete the wrong value.
     
    8484
    8585        // Check the remainder exists.
    86         $this->assertEquals( 'another value', get_post_meta( self::$post_id, 'nonunique', true ) );
    87         $this->assertEquals( array( 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
     86        $this->assertSame( 'another value', get_post_meta( self::$post_id, 'nonunique', true ) );
     87        $this->assertSame( array( 'another value' ), get_post_meta( self::$post_id, 'nonunique', false ) );
    8888
    8989        // Add a third one.
     
    9999        $actual = get_post_meta( self::$post_id, 'nonunique', false );
    100100        sort( $actual );
    101         $this->assertEquals( $expected, $actual );
     101        $this->assertSame( $expected, $actual );
    102102
    103103        // Delete the lot.
     
    114114
    115115        // Check they exist.
    116         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
    117         $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
    118         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
    119         $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
     116        $this->assertSame( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
     117        $this->assertSame( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     118        $this->assertSame( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     119        $this->assertSame( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    120120
    121121        // Update them.
     
    125125
    126126        // Check they updated.
    127         $this->assertEquals( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
    128         $this->assertEquals( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
    129         $this->assertEquals( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
    130         $this->assertEquals( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
     127        $this->assertSame( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
     128        $this->assertSame( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     129        $this->assertSame( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     130        $this->assertSame( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    131131
    132132    }
     
    138138
    139139        // Check they exist.
    140         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_delete', true ) );
    141         $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
     140        $this->assertSame( 'value', get_post_meta( self::$post_id, 'unique_delete', true ) );
     141        $this->assertSame( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
    142142
    143143        // Delete one of them.
     
    145145
    146146        // Check the other still exists.
    147         $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
     147        $this->assertSame( 'value', get_post_meta( self::$post_id_2, 'unique_delete', true ) );
    148148
    149149    }
     
    155155
    156156        // Check they exist.
    157         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_delete_by_key', true ) );
    158         $this->assertEquals( 'value', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
     157        $this->assertSame( 'value', get_post_meta( self::$post_id, 'unique_delete_by_key', true ) );
     158        $this->assertSame( 'value', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
    159159
    160160        // Delete one of them.
     
    162162
    163163        // Check the other still exists.
    164         $this->assertEquals( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
    165         $this->assertEquals( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
     164        $this->assertSame( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
     165        $this->assertSame( '', get_post_meta( self::$post_id_2, 'unique_delete_by_key', true ) );
    166166    }
    167167
     
    205205
    206206        // Check they exist.
    207         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
    208         $this->assertEquals( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
    209         $this->assertEquals( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
    210         $this->assertEquals( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
     207        $this->assertSame( 'value', get_post_meta( self::$post_id, 'unique_update', true ) );
     208        $this->assertSame( array( 'value' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     209        $this->assertSame( 'value', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     210        $this->assertSame( array( 'value', 'another value' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    211211
    212212        // Update them.
     
    216216
    217217        // Check they updated.
    218         $this->assertEquals( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
    219         $this->assertEquals( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
    220         $this->assertEquals( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
    221         $this->assertEquals( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
     218        $this->assertSame( 'new', get_post_meta( self::$post_id, 'unique_update', true ) );
     219        $this->assertSame( array( 'new' ), get_post_meta( self::$post_id, 'unique_update', false ) );
     220        $this->assertSame( 'new', get_post_meta( self::$post_id, 'nonunique_update', true ) );
     221        $this->assertSame( array( 'new', 'another new' ), get_post_meta( self::$post_id, 'nonunique_update', false ) );
    222222
    223223        // Slashed update.
     
    225225        $this->assertTrue( update_meta( $mid1, 'unique_update', addslashes( $data ) ) );
    226226        $meta = get_metadata_by_mid( 'post', $mid1 );
    227         $this->assertEquals( $data, $meta->meta_value );
     227        $this->assertSame( $data, $meta->meta_value );
    228228    }
    229229
     
    264264        $GLOBALS['wp_meta_keys'] = array();
    265265
    266         $this->assertEquals( 'post', $this->last_register_meta_call['object_type'] );
    267         $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
    268         $this->assertEquals( $args, $this->last_register_meta_call['args'] );
     266        $this->assertSame( 'post', $this->last_register_meta_call['object_type'] );
     267        $this->assertSame( $meta_key, $this->last_register_meta_call['meta_key'] );
     268        $this->assertSame( $args, $this->last_register_meta_call['args'] );
    269269    }
    270270
Note: See TracChangeset for help on using the changeset viewer.