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

    r48604 r48937  
    1010        $x->yZ    = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
    1111        $x->a     = array( 5, 111, 'x' );
    12         $this->assertEquals(
     12        $this->assertSame(
    1313            array(
    1414                '_baba' => 5,
     
    1919        );
    2020        $y = new MockClass;
    21         $this->assertEquals( array(), wp_parse_args( $y ) );
     21        $this->assertSame( array(), wp_parse_args( $y ) );
    2222    }
    2323
     
    2525        // Arrays.
    2626        $a = array();
    27         $this->assertEquals( array(), wp_parse_args( $a ) );
     27        $this->assertSame( array(), wp_parse_args( $a ) );
    2828        $b = array(
    2929            '_baba' => 5,
     
    3131            'a'     => array( 5, 111, 'x' ),
    3232        );
    33         $this->assertEquals(
     33        $this->assertSame(
    3434            array(
    3535                '_baba' => 5,
     
    4747        $x->a     = array( 5, 111, 'x' );
    4848        $d        = array( 'pu' => 'bu' );
    49         $this->assertEquals(
     49        $this->assertSame(
    5050            array(
    5151                'pu'    => 'bu',
     
    5757        );
    5858        $e = array( '_baba' => 6 );
    59         $this->assertEquals(
     59        $this->assertSame(
    6060            array(
    6161                '_baba' => 5,
     
    7070        $b = true;
    7171        wp_parse_str( $b, $s );
    72         $this->assertEquals( $s, wp_parse_args( $b ) );
     72        $this->assertSame( $s, wp_parse_args( $b ) );
    7373        $q = 'x=5&_baba=dudu&';
    7474        wp_parse_str( $q, $ss );
    75         $this->assertEquals( $ss, wp_parse_args( $q ) );
     75        $this->assertSame( $ss, wp_parse_args( $q ) );
    7676    }
    7777
     
    137137     */
    138138    function test_wp_normalize_path( $path, $expected ) {
    139         $this->assertEquals( $expected, wp_normalize_path( $path ) );
     139        $this->assertSame( $expected, wp_normalize_path( $path ) );
    140140    }
    141141
     
    169169
    170170        // Sanity check.
    171         $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
     171        $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
    172172
    173173        // Check number is appended for file already exists.
    174174        $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' );
    175         $this->assertEquals( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' );
     175        $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'Number not appended correctly' );
    176176        $this->assertFileNotExists( $testdir . 'test-image-1.png' );
    177177
    178178        // Check special chars.
    179         $this->assertEquals( 'testtest-image.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );
     179        $this->assertSame( 'testtest-image.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );
    180180
    181181        // Check special chars with potential conflicting name.
    182         $this->assertEquals( 'test-image-1.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' );
     182        $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' );
    183183
    184184        // Check with single quotes in name (somehow).
    185         $this->assertEquals( 'abcdefgh.png', wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' );
     185        $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' );
    186186
    187187        // Check with double quotes in name (somehow).
    188         $this->assertEquals( 'abcdefgh.png', wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' );
     188        $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' );
    189189
    190190        // Test crazy name (useful for regression tests).
    191         $this->assertEquals( '12af34567890@..^_qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' );
     191        $this->assertSame( '12af34567890@..^_qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' );
    192192
    193193        // Test slashes in names.
    194         $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\fg.png' ), 'Slash not removed' );
    195         $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\fg.png' ), 'Double slashed not removed' );
    196         $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' );
     194        $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\fg.png' ), 'Slash not removed' );
     195        $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\fg.png' ), 'Double slashed not removed' );
     196        $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' );
    197197    }
    198198
     
    206206
    207207        // Test collision with "dimension-like" original filename.
    208         $this->assertEquals( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) );
     208        $this->assertSame( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) );
    209209        // Test collision with existing sub-size filename.
    210210        // Existing files: one-blue-pixel-100x100.png, one-blue-pixel-1-100x100.png.
    211         $this->assertEquals( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) );
     211        $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) );
    212212        // Same as above with upper case extension.
    213         $this->assertEquals( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) );
     213        $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) );
    214214
    215215        remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
     
    369369            $_SERVER['REQUEST_URI'] = 'nothing';
    370370
    371             $this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1', $url ) );
    372             $this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
    373             $this->assertEquals(
     371            $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1', $url ) );
     372            $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
     373            $this->assertSame(
    374374                "$url?foo=2",
    375375                add_query_arg(
     
    381381                )
    382382            );
    383             $this->assertEquals(
     383            $this->assertSame(
    384384                "$url?foo=1&bar=2",
    385385                add_query_arg(
     
    394394            $_SERVER['REQUEST_URI'] = $url;
    395395
    396             $this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1' ) );
    397             $this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) );
    398             $this->assertEquals(
     396            $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1' ) );
     397            $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) );
     398            $this->assertSame(
    399399                "$url?foo=2",
    400400                add_query_arg(
     
    405405                )
    406406            );
    407             $this->assertEquals(
     407            $this->assertSame(
    408408                "$url?foo=1&bar=2",
    409409                add_query_arg(
     
    420420            $url                    = str_replace( '#frag', '', $frag_url );
    421421
    422             $this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) );
    423             $this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) );
    424             $this->assertEquals(
     422            $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) );
     423            $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) );
     424            $this->assertSame(
    425425                "$url?foo=2#frag",
    426426                add_query_arg(
     
    432432                )
    433433            );
    434             $this->assertEquals(
     434            $this->assertSame(
    435435                "$url?foo=1&bar=2#frag",
    436436                add_query_arg(
     
    445445            $_SERVER['REQUEST_URI'] = $frag_url;
    446446
    447             $this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) );
    448             $this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) );
    449             $this->assertEquals(
     447            $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) );
     448            $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) );
     449            $this->assertSame(
    450450                "$url?foo=2#frag",
    451451                add_query_arg(
     
    456456                )
    457457            );
    458             $this->assertEquals(
     458            $this->assertSame(
    459459                "$url?foo=1&bar=2#frag",
    460460                add_query_arg(
     
    482482            $_SERVER['REQUEST_URI'] = 'nothing';
    483483
    484             $this->assertEquals( "$url&foo=1", add_query_arg( 'foo', '1', $url ) );
    485             $this->assertEquals( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
    486             $this->assertEquals(
     484            $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1', $url ) );
     485            $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
     486            $this->assertSame(
    487487                "$url&foo=2",
    488488                add_query_arg(
     
    494494                )
    495495            );
    496             $this->assertEquals(
     496            $this->assertSame(
    497497                "$url&foo=1&bar=2",
    498498                add_query_arg(
     
    507507            $_SERVER['REQUEST_URI'] = $url;
    508508
    509             $this->assertEquals( "$url&foo=1", add_query_arg( 'foo', '1' ) );
    510             $this->assertEquals( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) );
    511             $this->assertEquals(
     509            $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1' ) );
     510            $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) );
     511            $this->assertSame(
    512512                "$url&foo=2",
    513513                add_query_arg(
     
    518518                )
    519519            );
    520             $this->assertEquals(
     520            $this->assertSame(
    521521                "$url&foo=1&bar=2",
    522522                add_query_arg(
     
    537537    function test_add_query_arg_numeric_keys() {
    538538        $url = add_query_arg( array( 'foo' => 'bar' ), '1=1' );
    539         $this->assertEquals( '1=1&foo=bar', $url );
     539        $this->assertSame( '1=1&foo=bar', $url );
    540540
    541541        $url = add_query_arg(
     
    546546            '1=1'
    547547        );
    548         $this->assertEquals( '1=2&foo=bar', $url );
     548        $this->assertSame( '1=2&foo=bar', $url );
    549549
    550550        $url = add_query_arg( array( '1' => '2' ), 'foo=bar' );
    551         $this->assertEquals( 'foo=bar&1=2', $url );
     551        $this->assertSame( 'foo=bar&1=2', $url );
    552552    }
    553553
     
    601601        $this->assertInternalType( 'array', $mimes2 );
    602602        $this->assertNotEmpty( $mimes2 );
    603         $this->assertEquals( $mimes2, $mimes );
     603        $this->assertSame( $mimes2, $mimes );
    604604    }
    605605
     
    611611
    612612        update_option( 'blog_charset', 'utf8' );
    613         $this->assertEquals( 'UTF-8', get_option( 'blog_charset' ) );
     613        $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) );
    614614
    615615        update_option( 'blog_charset', 'utf-8' );
    616         $this->assertEquals( 'UTF-8', get_option( 'blog_charset' ) );
     616        $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) );
    617617
    618618        update_option( 'blog_charset', 'UTF8' );
    619         $this->assertEquals( 'UTF-8', get_option( 'blog_charset' ) );
     619        $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) );
    620620
    621621        update_option( 'blog_charset', 'UTF-8' );
    622         $this->assertEquals( 'UTF-8', get_option( 'blog_charset' ) );
     622        $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) );
    623623
    624624        update_option( 'blog_charset', 'ISO-8859-1' );
    625         $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset' ) );
     625        $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) );
    626626
    627627        update_option( 'blog_charset', 'ISO8859-1' );
    628         $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset' ) );
     628        $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) );
    629629
    630630        update_option( 'blog_charset', 'iso8859-1' );
    631         $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset' ) );
     631        $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) );
    632632
    633633        update_option( 'blog_charset', 'iso-8859-1' );
    634         $this->assertEquals( 'ISO-8859-1', get_option( 'blog_charset' ) );
     634        $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) );
    635635
    636636        // Arbitrary strings are passed through.
    637637        update_option( 'blog_charset', 'foobarbaz' );
    638         $this->assertEquals( 'foobarbaz', get_option( 'blog_charset' ) );
     638        $this->assertSame( 'foobarbaz', get_option( 'blog_charset' ) );
    639639
    640640        update_option( 'blog_charset', $orig_blog_charset );
     
    707707        $actual                     = _device_can_upload();
    708708        unset( $_SERVER['HTTP_USER_AGENT'] );
    709         $this->assertEquals( $expected, $actual );
     709        $this->assertSame( $expected, $actual );
    710710    }
    711711
     
    889889        $this->assertInternalType( 'array', $urls );
    890890        $this->assertCount( count( $original_urls ), $urls );
    891         $this->assertEquals( $original_urls, $urls );
     891        $this->assertSame( $original_urls, $urls );
    892892
    893893        $exploded = array_values( array_filter( array_map( 'trim', explode( "\n", $blob ) ) ) );
     
    895895        $decoded = array_map( 'html_entity_decode', $exploded );
    896896
    897         $this->assertEquals( $decoded, $urls );
    898         $this->assertEquals( $original_urls, $decoded );
     897        $this->assertSame( $decoded, $urls );
     898        $this->assertSame( $original_urls, $decoded );
    899899
    900900        $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
     
    910910        $this->assertInternalType( 'array', $urls );
    911911        $this->assertCount( 8, $urls );
    912         $this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
     912        $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls );
    913913
    914914        $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
     
    924924        $this->assertInternalType( 'array', $urls );
    925925        $this->assertCount( 8, $urls );
    926         $this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
     926        $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls );
    927927    }
    928928
     
    931931     */
    932932    function test_wp_json_encode() {
    933         $this->assertEquals( wp_json_encode( 'a' ), '"a"' );
     933        $this->assertSame( wp_json_encode( 'a' ), '"a"' );
    934934    }
    935935
     
    938938     */
    939939    function test_wp_json_encode_utf8() {
    940         $this->assertEquals( wp_json_encode( '这' ), '"\u8fd9"' );
     940        $this->assertSame( wp_json_encode( '这' ), '"\u8fd9"' );
    941941    }
    942942
     
    959959        $utf8  = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
    960960
    961         $this->assertEquals( 'aあb', $utf8 );
    962 
    963         $this->assertEquals( '"a\u3042b"', wp_json_encode( $eucjp ) );
     961        $this->assertSame( 'aあb', $utf8 );
     962
     963        $this->assertSame( '"a\u3042b"', wp_json_encode( $eucjp ) );
    964964
    965965        mb_detect_order( $old_charsets );
     
    984984        $utf8  = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
    985985
    986         $this->assertEquals( 'aあb', $utf8 );
    987 
    988         $this->assertEquals( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) );
     986        $this->assertSame( 'aあb', $utf8 );
     987
     988        $this->assertSame( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) );
    989989
    990990        mb_detect_order( $old_charsets );
     
    995995     */
    996996    function test_wp_json_encode_array() {
    997         $this->assertEquals( wp_json_encode( array( 'a' ) ), '["a"]' );
     997        $this->assertSame( wp_json_encode( array( 'a' ) ), '["a"]' );
    998998    }
    999999
     
    10041004        $object    = new stdClass;
    10051005        $object->a = 'b';
    1006         $this->assertEquals( wp_json_encode( $object ), '{"a":"b"}' );
     1006        $this->assertSame( wp_json_encode( $object ), '{"a":"b"}' );
    10071007    }
    10081008
     
    10291029        $this->assertTrue( is_string( $date_return ), 'The date return must be a string' );
    10301030        $this->assertNotEmpty( $date_return, 'The date return could not be an empty string' );
    1031         $this->assertEquals( $expected, $date_return, 'The date does not match' );
     1031        $this->assertSame( $expected, $date_return, 'The date does not match' );
    10321032        $this->assertEquals( new DateTime( $expected ), new DateTime( $date_return ), 'The date is not the same after the call method' );
    10331033    }
     
    10701070        foreach ( $extensions as $type => $extension_list ) {
    10711071            foreach ( $extension_list as $extension ) {
    1072                 $this->assertEquals( $type, wp_ext2type( $extension ) );
    1073                 $this->assertEquals( $type, wp_ext2type( strtoupper( $extension ) ) );
     1072                $this->assertSame( $type, wp_ext2type( $extension ) );
     1073                $this->assertSame( $type, wp_ext2type( strtoupper( $extension ) ) );
    10741074            }
    10751075        }
     
    10961096
    10971097        $this->assertSame( $ini_limit_before, $ini_limit_after );
    1098         $this->assertSame( false, $raised_limit );
     1098        $this->assertFalse( $raised_limit );
    10991099        $this->assertEquals( WP_MAX_MEMORY_LIMIT, $ini_limit_after );
    11001100    }
     
    11151115
    11161116        $unique_uuids = array_unique( $uuids );
    1117         $this->assertEquals( $uuids, $unique_uuids );
     1117        $this->assertSame( $uuids, $unique_uuids );
    11181118    }
    11191119
     
    11841184            $ids[] = $id;
    11851185        }
    1186         $this->assertEquals( $ids, array_unique( $ids ) );
     1186        $this->assertSame( $ids, array_unique( $ids ) );
    11871187
    11881188        // Test with prefix.
     
    11931193            $ids[] = $id;
    11941194        }
    1195         $this->assertEquals( $ids, array_unique( $ids ) );
     1195        $this->assertSame( $ids, array_unique( $ids ) );
    11961196    }
    11971197
     
    12051205        }
    12061206
    1207         $this->assertEquals( $expected, wp_get_image_mime( $file ) );
     1207        $this->assertSame( $expected, wp_get_image_mime( $file ) );
    12081208    }
    12091209
     
    12171217        }
    12181218
    1219         $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     1219        $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) );
    12201220    }
    12211221
     
    12391239
    12401240        add_filter( 'upload_mimes', array( $this, '_filter_mime_types_svg' ) );
    1241         $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     1241        $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) );
    12421242
    12431243        // Cleanup.
     
    12641264
    12651265        add_filter( 'upload_mimes', array( $this, '_filter_mime_types_woff' ) );
    1266         $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     1266        $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) );
    12671267
    12681268        // Cleanup.
Note: See TracChangeset for help on using the changeset viewer.