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

    r47198 r48937  
    3737     */
    3838    function test_mb_strlen( $string, $expected_character_length ) {
    39         $this->assertEquals( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
     39        $this->assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
    4040    }
    4141
     
    4545    function test_mb_strlen_via_regex( $string, $expected_character_length ) {
    4646        _wp_can_use_pcre_u( false );
    47         $this->assertEquals( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
     47        $this->assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
    4848        _wp_can_use_pcre_u( 'reset' );
    4949    }
     
    5353     */
    5454    function test_8bit_mb_strlen( $string, $expected_character_length, $expected_byte_length ) {
    55         $this->assertEquals( $expected_byte_length, _mb_strlen( $string, '8bit' ) );
     55        $this->assertSame( $expected_byte_length, _mb_strlen( $string, '8bit' ) );
    5656    }
    5757
     
    6060     */
    6161    function test_mb_substr( $string, $start, $length, $expected_character_substring ) {
    62         $this->assertEquals( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
     62        $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
    6363    }
    6464
     
    6868    function test_mb_substr_via_regex( $string, $start, $length, $expected_character_substring ) {
    6969        _wp_can_use_pcre_u( false );
    70         $this->assertEquals( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
     70        $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
    7171        _wp_can_use_pcre_u( 'reset' );
    7272    }
     
    7676     */
    7777    function test_8bit_mb_substr( $string, $start, $length, $expected_character_substring, $expected_byte_substring ) {
    78         $this->assertEquals( $expected_byte_substring, _mb_substr( $string, $start, $length, '8bit' ) );
     78        $this->assertSame( $expected_byte_substring, _mb_substr( $string, $start, $length, '8bit' ) );
    7979    }
    8080
     
    8484        $string_mb    = base64_decode( '5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=' );
    8585
    86         $this->assertEquals( 'DEF', _mb_substr( $string_ascii, 3 ) );
    87         $this->assertEquals( 'DEF', _mb_substr( $string_ascii, 3, 5, 'ISO-8859-1' ) );
     86        $this->assertSame( 'DEF', _mb_substr( $string_ascii, 3 ) );
     87        $this->assertSame( 'DEF', _mb_substr( $string_ascii, 3, 5, 'ISO-8859-1' ) );
    8888
    8989        // Specific latin-1 as that is the default the core PHP test operates under.
    90         $this->assertEquals( 'peacrOiqng==', base64_encode( _mb_substr( $string_mb, 2, 7, 'latin-1' ) ) );
    91         $this->assertEquals( '6Kqe44OG44Kt44K544OI44Gn44GZ', base64_encode( _mb_substr( $string_mb, 2, 7, 'utf-8' ) ) );
     90        $this->assertSame( 'peacrOiqng==', base64_encode( _mb_substr( $string_mb, 2, 7, 'latin-1' ) ) );
     91        $this->assertSame( '6Kqe44OG44Kt44K544OI44Gn44GZ', base64_encode( _mb_substr( $string_mb, 2, 7, 'utf-8' ) ) );
    9292
    9393        /* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt */
     
    159159        $iterator = 0;
    160160        foreach ( $inputs as $input ) {
    161             $this->assertEquals( $outputs[ $iterator ], _mb_substr( $input, $start, $length ) );
     161            $this->assertSame( $outputs[ $iterator ], _mb_substr( $input, $start, $length ) );
    162162            $iterator++;
    163163        }
     
    166166
    167167    function test_hash_hmac_simple() {
    168         $this->assertEquals( '140d1cb79fa12e2a31f32d35ad0a2723', _hash_hmac( 'md5', 'simple', 'key' ) );
    169         $this->assertEquals( '993003b95758e0ac2eba451a4c5877eb1bb7b92a', _hash_hmac( 'sha1', 'simple', 'key' ) );
     168        $this->assertSame( '140d1cb79fa12e2a31f32d35ad0a2723', _hash_hmac( 'md5', 'simple', 'key' ) );
     169        $this->assertSame( '993003b95758e0ac2eba451a4c5877eb1bb7b92a', _hash_hmac( 'sha1', 'simple', 'key' ) );
    170170    }
    171171
    172172    function test_hash_hmac_padding() {
    173         $this->assertEquals( '3c1399103807cf12ec38228614416a8c', _hash_hmac( 'md5', 'simple', '65 character key 65 character key 65 character key 65 character k' ) );
    174         $this->assertEquals( '4428826d20003e309d6c2a6515891370daf184ea', _hash_hmac( 'sha1', 'simple', '65 character key 65 character key 65 character key 65 character k' ) );
     173        $this->assertSame( '3c1399103807cf12ec38228614416a8c', _hash_hmac( 'md5', 'simple', '65 character key 65 character key 65 character key 65 character k' ) );
     174        $this->assertSame( '4428826d20003e309d6c2a6515891370daf184ea', _hash_hmac( 'sha1', 'simple', '65 character key 65 character key 65 character key 65 character k' ) );
    175175    }
    176176
    177177    function test_hash_hmac_output() {
    178         $this->assertEquals( array( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ), unpack( 'H32', _hash_hmac( 'md5', 'simple', 'key', true ) ) );
    179         $this->assertEquals( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
     178        $this->assertSame( array( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ), unpack( 'H32', _hash_hmac( 'md5', 'simple', 'key', true ) ) );
     179        $this->assertSame( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
    180180    }
    181181
     
    187187        $json = new Services_JSON();
    188188        // Super basic test to verify Services_JSON is intact and working.
    189         $this->assertEquals( '["foo"]', $json->encodeUnsafe( array( 'foo' ) ) );
    190         $this->assertEquals( array( 'foo' ), $json->decode( '["foo"]' ) );
     189        $this->assertSame( '["foo"]', $json->encodeUnsafe( array( 'foo' ) ) );
     190        $this->assertSame( array( 'foo' ), $json->decode( '["foo"]' ) );
    191191    }
    192192
Note: See TracChangeset for help on using the changeset viewer.