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

    r48317 r48937  
    149149
    150150        foreach ( $inputs as $key => $input ) {
    151             $this->assertEquals( $expected[ $key ], $wpdb->esc_like( $input ) );
     151            $this->assertSame( $expected[ $key ], $wpdb->esc_like( $input ) );
    152152        }
    153153    }
     
    168168    function test_like_query( $data, $like, $result ) {
    169169        global $wpdb;
    170         return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( 'SELECT %s LIKE %s', $data, $wpdb->esc_like( $like ) ) ) );
     170        return $this->assertSame( $result, $wpdb->get_var( $wpdb->prepare( 'SELECT %s LIKE %s', $data, $wpdb->esc_like( $like ) ) ) );
    171171    }
    172172
     
    254254
    255255        $this->assertNotEquals( $col_meta, $new_meta );
    256         $this->assertEquals( $col_meta, $wpdb->col_meta );
     256        $this->assertSame( $col_meta, $wpdb->col_meta );
    257257    }
    258258
     
    282282
    283283        $sql = $wpdb->remove_placeholder_escape( $sql );
    284         $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
     284        $this->assertSame( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
    285285    }
    286286
     
    368368        // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    369369        $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = $id", $id );
    370         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0", $prepared );
     370        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0", $prepared );
    371371    }
    372372
     
    375375
    376376        $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, 'admin' );
    377         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
     377        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
    378378    }
    379379
     
    386386        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    387387        $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( 'admin' ) );
    388         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
     388        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
    389389
    390390        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    391391        $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), 'admin' );
    392         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
     392        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
    393393    }
    394394
     
    397397
    398398        $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, 'admin' ) );
    399         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
     399        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
    400400    }
    401401
     
    408408        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    409409        $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( 'admin' ) ) );
    410         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
     410        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
    411411
    412412        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    413413        $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), 'admin' ) );
    414         $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
     414        $this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
    415415    }
    416416
     
    491491        $calls  = explode( ', ', $str );
    492492        $called = join( '->', array( __CLASS__, __FUNCTION__ ) );
    493         $this->assertEquals( $called, end( $calls ) );
     493        $this->assertSame( $called, end( $calls ) );
    494494    }
    495495
     
    502502        $this->assertTrue( $wpdb->has_cap( 'GROUP_CONCAT' ) );
    503503        $this->assertTrue( $wpdb->has_cap( 'SUBQUERIES' ) );
    504         $this->assertEquals(
     504        $this->assertSame(
    505505            version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
    506506            $wpdb->has_cap( 'set_charset' )
    507507        );
    508         $this->assertEquals(
     508        $this->assertSame(
    509509            version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
    510510            $wpdb->has_cap( 'SET_CHARSET' )
     
    549549        $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
    550550
    551         $this->assertEquals( array( 'ID' ), $wpdb->get_col_info() );
    552         $this->assertEquals( array( $wpdb->users ), $wpdb->get_col_info( 'table' ) );
    553         $this->assertEquals( $wpdb->users, $wpdb->get_col_info( 'table', 0 ) );
     551        $this->assertSame( array( 'ID' ), $wpdb->get_col_info() );
     552        $this->assertSame( array( $wpdb->users ), $wpdb->get_col_info( 'table' ) );
     553        $this->assertSame( $wpdb->users, $wpdb->get_col_info( 'table', 0 ) );
    554554    }
    555555
     
    557557        global $wpdb;
    558558        $rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
    559         $this->assertEquals( 1, $rows );
     559        $this->assertSame( 1, $rows );
    560560        $this->assertNotEmpty( $wpdb->insert_id );
    561561        $d_rows = $wpdb->delete( $wpdb->users, array( 'ID' => $wpdb->insert_id ) );
    562         $this->assertEquals( 1, $d_rows );
     562        $this->assertSame( 1, $d_rows );
    563563    }
    564564
     
    566566        global $wpdb;
    567567        $rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
    568         $this->assertEquals( 1, $rows );
     568        $this->assertSame( 1, $rows );
    569569        $this->assertNotEmpty( $wpdb->insert_id );
    570570
    571571        $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $wpdb->insert_id ) );
    572572        $this->assertInternalType( 'object', $row );
    573         $this->assertEquals( 'Walter Sobchak', $row->display_name );
     573        $this->assertSame( 'Walter Sobchak', $row->display_name );
    574574    }
    575575
     
    668668        global $wpdb;
    669669        $rows1 = $wpdb->insert( $wpdb->users, array( 'display_name' => 'Walter Sobchak' ) );
    670         $this->assertEquals( 1, $rows1 );
     670        $this->assertSame( 1, $rows1 );
    671671        $this->assertNotEmpty( $wpdb->insert_id );
    672672        $last = $wpdb->insert_id;
     
    679679            )
    680680        );
    681         $this->assertEquals( 2, $rows2 );
     681        $this->assertSame( 2, $rows2 );
    682682        $this->assertNotEmpty( $wpdb->insert_id );
    683683
    684         $this->assertEquals( $last, $wpdb->insert_id );
     684        $this->assertSame( $last, $wpdb->insert_id );
    685685
    686686        $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) );
    687         $this->assertEquals( 'Walter Replace Sobchak', $row->display_name );
     687        $this->assertSame( 'Walter Replace Sobchak', $row->display_name );
    688688    }
    689689
     
    700700        $expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE ";
    701701        $this->assertNotEmpty( $wpdb->last_error );
    702         $this->assertEquals( $expected1, $wpdb->last_query );
     702        $this->assertSame( $expected1, $wpdb->last_query );
    703703
    704704        $wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' ) );
     
    706706        $expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'";
    707707        $this->assertEmpty( $wpdb->last_error );
    708         $this->assertEquals( $expected2, $wpdb->last_query );
     708        $this->assertSame( $expected2, $wpdb->last_query );
    709709        $wpdb->suppress_errors( $suppress );
    710710    }
     
    876876     */
    877877    function test_get_table_from_query( $query, $table ) {
    878         $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) );
     878        $this->assertSame( $table, self::$_wpdb->get_table_from_query( $query ) );
    879879    }
    880880
     
    918918     */
    919919    function test_get_escaped_table_from_show_query( $query, $table ) {
    920         $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) );
     920        $this->assertSame( $table, self::$_wpdb->get_table_from_query( $query ) );
    921921    }
    922922
     
    10851085        remove_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10 );
    10861086
    1087         $this->assertEquals( $charset, 'fake_charset' );
     1087        $this->assertSame( $charset, 'fake_charset' );
    10881088    }
    10891089    function filter_pre_get_table_charset( $charset, $table ) {
     
    10991099        remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10 );
    11001100
    1101         $this->assertEquals( $charset, 'fake_col_charset' );
     1101        $this->assertSame( $charset, 'fake_col_charset' );
    11021102    }
    11031103    function filter_pre_get_col_charset( $charset, $table, $column ) {
     
    13671367        // phpcs:ignore WordPress.DB.PreparedSQL
    13681368        $sql = $wpdb->prepare( $sql, ...$values );
    1369         $this->assertEquals( $expected, $sql );
     1369        $this->assertSame( $expected, $sql );
    13701370    }
    13711371
     
    13861386        // phpcs:ignore WordPress.DB.PreparedSQL
    13871387        $sql = $wpdb->prepare( $sql, $values );
    1388         $this->assertEquals( $expected, $sql );
     1388        $this->assertSame( $expected, $sql );
    13891389    }
    13901390
     
    15811581        $actual = $wpdb->prepare( $sql, $values );
    15821582
    1583         $this->assertEquals( $expected, $actual );
     1583        $this->assertSame( $expected, $actual );
    15841584    }
    15851585
     
    16691669
    16701670        $this->assertNotContains( '%s', $sql );
    1671         $this->assertEquals( $value, $actual );
     1671        $this->assertSame( $value, $actual );
    16721672    }
    16731673
     
    16791679        $sql = $wpdb->prepare( " $sql %s ", 'foo' );
    16801680
    1681         $this->assertEquals( "  'foo' {$wpdb->placeholder_escape()}1\$c  'foo' ", $sql );
     1681        $this->assertSame( "  'foo' {$wpdb->placeholder_escape()}1\$c  'foo' ", $sql );
    16821682    }
    16831683
Note: See TracChangeset for help on using the changeset viewer.