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

    r47554 r48937  
    1212    public function test_get_avatar_url_gravatar_url() {
    1313        $url = get_avatar_url( 1 );
    14         $this->assertEquals( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
     14        $this->assertSame( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
    1515    }
    1616
     
    2020    public function test_get_avatar_url_size() {
    2121        $url = get_avatar_url( 1 );
    22         $this->assertEquals( preg_match( '|\?.*s=96|', $url ), 1 );
     22        $this->assertSame( preg_match( '|\?.*s=96|', $url ), 1 );
    2323
    2424        $args = array( 'size' => 100 );
    2525        $url  = get_avatar_url( 1, $args );
    26         $this->assertEquals( preg_match( '|\?.*s=100|', $url ), 1 );
     26        $this->assertSame( preg_match( '|\?.*s=100|', $url ), 1 );
    2727    }
    2828
     
    3232    public function test_get_avatar_url_default() {
    3333        $url = get_avatar_url( 1 );
    34         $this->assertEquals( preg_match( '|\?.*d=mm|', $url ), 1 );
     34        $this->assertSame( preg_match( '|\?.*d=mm|', $url ), 1 );
    3535
    3636        $args = array( 'default' => 'wavatar' );
    3737        $url  = get_avatar_url( 1, $args );
    38         $this->assertEquals( preg_match( '|\?.*d=wavatar|', $url ), 1 );
    39 
    40         $this->assertEquals( preg_match( '|\?.*f=y|', $url ), 0 );
     38        $this->assertSame( preg_match( '|\?.*d=wavatar|', $url ), 1 );
     39
     40        $this->assertSame( preg_match( '|\?.*f=y|', $url ), 0 );
    4141        $args = array( 'force_default' => true );
    4242        $url  = get_avatar_url( 1, $args );
    43         $this->assertEquals( preg_match( '|\?.*f=y|', $url ), 1 );
     43        $this->assertSame( preg_match( '|\?.*f=y|', $url ), 1 );
    4444    }
    4545
     
    4949    public function test_get_avatar_url_rating() {
    5050        $url = get_avatar_url( 1 );
    51         $this->assertEquals( preg_match( '|\?.*r=g|', $url ), 1 );
     51        $this->assertSame( preg_match( '|\?.*r=g|', $url ), 1 );
    5252
    5353        $args = array( 'rating' => 'M' );
    5454        $url  = get_avatar_url( 1, $args );
    55         $this->assertEquals( preg_match( '|\?.*r=m|', $url ), 1 );
     55        $this->assertSame( preg_match( '|\?.*r=m|', $url ), 1 );
    5656    }
    5757
     
    6161    public function test_get_avatar_url_scheme() {
    6262        $url = get_avatar_url( 1 );
    63         $this->assertEquals( preg_match( '|^http://|', $url ), 1 );
     63        $this->assertSame( preg_match( '|^http://|', $url ), 1 );
    6464
    6565        $args = array( 'scheme' => 'https' );
    6666        $url  = get_avatar_url( 1, $args );
    67         $this->assertEquals( preg_match( '|^https://|', $url ), 1 );
     67        $this->assertSame( preg_match( '|^https://|', $url ), 1 );
    6868
    6969        $args = array( 'scheme' => 'lolcat' );
    7070        $url  = get_avatar_url( 1, $args );
    71         $this->assertEquals( preg_match( '|^lolcat://|', $url ), 0 );
     71        $this->assertSame( preg_match( '|^lolcat://|', $url ), 0 );
    7272    }
    7373
     
    7979
    8080        $url2 = get_avatar_url( WP_TESTS_EMAIL );
    81         $this->assertEquals( $url, $url2 );
     81        $this->assertSame( $url, $url2 );
    8282
    8383        $url2 = get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
    84         $this->assertEquals( $url, $url2 );
     84        $this->assertSame( $url, $url2 );
    8585
    8686        $user = get_user_by( 'id', 1 );
    8787        $url2 = get_avatar_url( $user );
    88         $this->assertEquals( $url, $url2 );
     88        $this->assertSame( $url, $url2 );
    8989
    9090        $post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
    9191        $post    = get_post( $post_id );
    9292        $url2    = get_avatar_url( $post );
    93         $this->assertEquals( $url, $url2 );
     93        $this->assertSame( $url, $url2 );
    9494
    9595        $comment_id = self::factory()->comment->create(
     
    101101        $comment    = get_comment( $comment_id );
    102102        $url2       = get_avatar_url( $comment );
    103         $this->assertEquals( $url, $url2 );
     103        $this->assertSame( $url, $url2 );
    104104    }
    105105
     
    115115        remove_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10 );
    116116
    117         $this->assertEquals( $url, $this->fake_url );
     117        $this->assertSame( $url, $this->fake_url );
    118118    }
    119119    public function pre_get_avatar_url_filter( $args ) {
     
    132132        remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 );
    133133
    134         $this->assertEquals( $url, $this->fake_url );
     134        $this->assertSame( $url, $this->fake_url );
    135135    }
    136136    public function get_avatar_url_filter( $url ) {
     
    161161        remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 );
    162162
    163         $this->assertEquals( $url, $url2 );
     163        $this->assertSame( $url, $url2 );
    164164    }
    165165    public function get_avatar_comment_types_filter( $comment_types ) {
     
    170170    public function test_get_avatar() {
    171171        $img = get_avatar( 1 );
    172         $this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 );
     172        $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 );
    173173    }
    174174
     
    176176        $size = '100';
    177177        $img  = get_avatar( 1, $size );
    178         $this->assertEquals( preg_match( "|^<img .*height='$size'.*width='$size'|", $img ), 1 );
     178        $this->assertSame( preg_match( "|^<img .*height='$size'.*width='$size'|", $img ), 1 );
    179179    }
    180180
     
    182182        $alt = 'Mr Hyde';
    183183        $img = get_avatar( 1, 96, '', $alt );
    184         $this->assertEquals( preg_match( "|^<img alt='$alt'|", $img ), 1 );
     184        $this->assertSame( preg_match( "|^<img alt='$alt'|", $img ), 1 );
    185185    }
    186186
     
    188188        $class = 'first';
    189189        $img   = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
    190         $this->assertEquals( preg_match( "|^<img .*class='[^']*{$class}[^']*'|", $img ), 1 );
     190        $this->assertSame( preg_match( "|^<img .*class='[^']*{$class}[^']*'|", $img ), 1 );
    191191    }
    192192
    193193    public function test_get_avatar_default_class() {
    194194        $img = get_avatar( 1, 96, '', '', array( 'force_default' => true ) );
    195         $this->assertEquals( preg_match( "|^<img .*class='[^']*avatar-default[^']*'|", $img ), 1 );
     195        $this->assertSame( preg_match( "|^<img .*class='[^']*avatar-default[^']*'|", $img ), 1 );
    196196    }
    197197
     
    219219        remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 );
    220220
    221         $this->assertEquals( $img, $this->fake_img );
     221        $this->assertSame( $img, $this->fake_img );
    222222    }
    223223    public function pre_get_avatar_filter( $img ) {
     
    235235        remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 );
    236236
    237         $this->assertEquals( $img, $this->fake_url );
     237        $this->assertSame( $img, $this->fake_url );
    238238    }
    239239    public function get_avatar_filter( $img ) {
Note: See TracChangeset for help on using the changeset viewer.