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

    r47122 r48937  
    2020    function test_sanitize_meta() {
    2121        $meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
    22         $this->assertEquals( 'unsanitized', $meta );
     22        $this->assertSame( 'unsanitized', $meta );
    2323
    2424        register_meta( 'post', 'some_meta', array( $this, '_meta_sanitize_cb' ) );
    2525        $meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
    26         $this->assertEquals( 'sanitized', $meta );
     26        $this->assertSame( 'sanitized', $meta );
    2727    }
    2828
     
    4949        $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value' ) );
    5050        $meta = get_metadata_by_mid( 'user', $this->meta_id );
    51         $this->assertEquals( 'meta_new_value', $meta->meta_value );
     51        $this->assertSame( 'meta_new_value', $meta->meta_value );
    5252
    5353        // Update the meta value.
    5454        $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value', 'meta_new_key' ) );
    5555        $meta = get_metadata_by_mid( 'user', $this->meta_id );
    56         $this->assertEquals( 'meta_new_key', $meta->meta_key );
     56        $this->assertSame( 'meta_new_key', $meta->meta_key );
    5757
    5858        // Update the key and value.
    5959        $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', 'meta_key' ) );
    6060        $meta = get_metadata_by_mid( 'user', $this->meta_id );
    61         $this->assertEquals( 'meta_key', $meta->meta_key );
    62         $this->assertEquals( 'meta_value', $meta->meta_value );
     61        $this->assertSame( 'meta_key', $meta->meta_key );
     62        $this->assertSame( 'meta_value', $meta->meta_value );
    6363
    6464        // Update the value that has to be serialized.
    6565        $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, array( 'first', 'second' ) ) );
    6666        $meta = get_metadata_by_mid( 'user', $this->meta_id );
    67         $this->assertEquals( array( 'first', 'second' ), $meta->meta_value );
     67        $this->assertSame( array( 'first', 'second' ), $meta->meta_value );
    6868
    6969        // Let's try some invalid meta data.
     
    138138        );
    139139
    140         $this->assertEquals( 1, count( $u ) );
     140        $this->assertSame( 1, count( $u ) );
    141141
    142142        // User found is not locally defined author (it's the admin).
     
    144144
    145145        // Test EXISTS and NOT EXISTS together, no users should be found.
    146         $this->assertEquals(
     146        $this->assertSame(
    147147            0,
    148148            count(
     
    164164        );
    165165
    166         $this->assertEquals(
     166        $this->assertSame(
    167167            2,
    168168            count(
     
    182182        delete_metadata( 'user', $this->author->ID, 'meta_key' );
    183183
    184         $this->assertEquals(
     184        $this->assertSame(
    185185            2,
    186186            count(
     
    209209        $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
    210210        $this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value ) );
    211         $this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
     211        $this->assertSame( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
    212212        $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
    213213        $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
    214214        $this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value ) );
    215         $this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
     215        $this->assertSame( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
    216216        $this->assertTrue( update_metadata( 'user', $this->author->ID, $key, 'blah' ) );
    217         $this->assertEquals( 'blah', get_metadata( 'user', $this->author->ID, $key, true ) );
     217        $this->assertSame( 'blah', get_metadata( 'user', $this->author->ID, $key, true ) );
    218218        $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
    219219        $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
     
    222222        // Test overslashing.
    223223        $this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value2 ) );
    224         $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
     224        $this->assertSame( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
    225225        $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
    226226        $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
    227227        $this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value2 ) );
    228         $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
     228        $this->assertSame( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
    229229    }
    230230
     
    253253        );
    254254
    255         $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
    256         $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
     255        $this->assertSame( array( $post_id2, $post_id1 ), $posts->posts );
     256        $this->assertSame( 2, substr_count( $posts->request, 'CAST(' ) );
    257257
    258258        // Make sure the newer meta_query syntax behaves in a consistent way.
     
    274274        );
    275275
    276         $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
    277         $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
     276        $this->assertSame( array( $post_id2, $post_id1 ), $posts->posts );
     277        $this->assertSame( 2, substr_count( $posts->request, 'CAST(' ) );
    278278
    279279        // The legacy `meta_key` value should take precedence.
     
    298298        );
    299299
    300         $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
    301         $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
     300        $this->assertSame( array( $post_id2, $post_id1 ), $posts->posts );
     301        $this->assertSame( 2, substr_count( $posts->request, 'CAST(' ) );
    302302    }
    303303
     
    311311        foreach ( range( 1, 10 ) as $i ) {
    312312            $meta = get_post_meta( $post_id, 'color' );
    313             $this->assertEquals( $meta, $colors );
     313            $this->assertSame( $meta, $colors );
    314314
    315315            if ( 0 === $i % 2 ) {
     
    400400        $found = get_metadata( 'user', $this->author->ID );
    401401
    402         $this->assertEquals( array( $value ), $found['foo'] );
     402        $this->assertSame( array( $value ), $found['foo'] );
    403403    }
    404404
Note: See TracChangeset for help on using the changeset viewer.