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

    r47122 r48937  
    8989
    9090        $id = self::factory()->post->create();
    91         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     91        $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
    9292
    9393        $id = self::factory()->post->create( array( 'post_type' => 'page' ) );
    94         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     94        $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
    9595    }
    9696
     
    9898        $post_id   = self::factory()->post->create();
    9999        $permalink = get_permalink( $post_id );
    100         $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
     100        $this->assertSame( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
    101101
    102102        $post_id   = self::factory()->post->create( array( 'post_type' => 'page' ) );
    103103        $permalink = get_permalink( $post_id );
    104         $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
     104        $this->assertSame( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
    105105    }
    106106
     
    116116        $page_url_to_id = url_to_postid( set_url_scheme( $page_permalink, 'http' ) );
    117117
    118         $this->assertEquals( $post_id, $post_url_to_id );
    119         $this->assertEquals( $page_id, $page_url_to_id );
     118        $this->assertSame( $post_id, $post_url_to_id );
     119        $this->assertSame( $page_id, $page_url_to_id );
    120120    }
    121121
     
    149149
    150150        // Test that the url_to_postid() call matched.
    151         $this->assertEquals( $post_id, $url_to_postid );
     151        $this->assertSame( $post_id, $url_to_postid );
    152152    }
    153153
     
    178178
    179179        $id = self::factory()->post->create( array( 'post_type' => $post_type ) );
    180         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     180        $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
    181181
    182182        _unregister_post_type( $post_type );
     
    199199        );
    200200
    201         $this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) );
    202         $this->assertEquals( $child_id, url_to_postid( get_permalink( $child_id ) ) );
     201        $this->assertSame( $parent_id, url_to_postid( get_permalink( $parent_id ) ) );
     202        $this->assertSame( $child_id, url_to_postid( get_permalink( $child_id ) ) );
    203203    }
    204204
     
    240240        );
    241241
    242         $this->assertEquals( home_url( 'parent/child1/grandchild/' ), get_permalink( $grandchild_id_1 ) );
    243         $this->assertEquals( home_url( 'parent/child2/grandchild/' ), get_permalink( $grandchild_id_2 ) );
    244         $this->assertEquals( $grandchild_id_1, url_to_postid( get_permalink( $grandchild_id_1 ) ) );
    245         $this->assertEquals( $grandchild_id_2, url_to_postid( get_permalink( $grandchild_id_2 ) ) );
     242        $this->assertSame( home_url( 'parent/child1/grandchild/' ), get_permalink( $grandchild_id_1 ) );
     243        $this->assertSame( home_url( 'parent/child2/grandchild/' ), get_permalink( $grandchild_id_2 ) );
     244        $this->assertSame( $grandchild_id_1, url_to_postid( get_permalink( $grandchild_id_1 ) ) );
     245        $this->assertSame( $grandchild_id_2, url_to_postid( get_permalink( $grandchild_id_2 ) ) );
    246246    }
    247247
     
    257257            )
    258258        );
    259         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    260         $this->assertEquals( $id, url_to_postid( site_url( '/example/examp' ) ) );
    261         $this->assertEquals( $id, url_to_postid( '/example/examp/' ) );
    262         $this->assertEquals( $id, url_to_postid( '/example/examp' ) );
    263 
    264         $this->assertEquals( 0, url_to_postid( site_url( '/example/ex' ) ) );
    265         $this->assertEquals( 0, url_to_postid( '/example/ex' ) );
    266         $this->assertEquals( 0, url_to_postid( '/example/ex/' ) );
    267         $this->assertEquals( 0, url_to_postid( '/example-page/example/' ) );
    268         $this->assertEquals( 0, url_to_postid( '/example-page/ex/' ) );
     259        $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
     260        $this->assertSame( $id, url_to_postid( site_url( '/example/examp' ) ) );
     261        $this->assertSame( $id, url_to_postid( '/example/examp/' ) );
     262        $this->assertSame( $id, url_to_postid( '/example/examp' ) );
     263
     264        $this->assertSame( 0, url_to_postid( site_url( '/example/ex' ) ) );
     265        $this->assertSame( 0, url_to_postid( '/example/ex' ) );
     266        $this->assertSame( 0, url_to_postid( '/example/ex/' ) );
     267        $this->assertSame( 0, url_to_postid( '/example-page/example/' ) );
     268        $this->assertSame( 0, url_to_postid( '/example-page/ex/' ) );
    269269    }
    270270
     
    277277
    278278        $this->go_to( $home_url );
    279         $this->assertEquals( array(), $GLOBALS['wp']->query_vars );
     279        $this->assertSame( array(), $GLOBALS['wp']->query_vars );
    280280
    281281        $this->go_to( $home_url . 'page' );
    282         $this->assertEquals(
     282        $this->assertSame(
    283283            array(
    284284                'page'     => '',
     
    298298
    299299        $this->go_to( $home_url );
    300         $this->assertEquals( array(), $GLOBALS['wp']->query_vars );
     300        $this->assertSame( array(), $GLOBALS['wp']->query_vars );
    301301
    302302        $this->go_to( $home_url . 'page' );
    303         $this->assertEquals(
     303        $this->assertSame(
    304304            array(
    305305                'page'     => '',
     
    317317            $GLOBALS['wp']->query_vars
    318318        );
    319         $this->assertEquals(
     319        $this->assertSame(
    320320            array(
    321321                'page'     => '',
     
    338338        _unregister_post_type( 'foo' );
    339339
    340         $this->assertEquals( array(), $GLOBALS['wp']->query_vars );
     340        $this->assertSame( array(), $GLOBALS['wp']->query_vars );
    341341    }
    342342
     
    352352        );
    353353
    354         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    355         $this->assertEquals( $id, url_to_postid( site_url( '/example/example/' ) ) );
    356         $this->assertEquals( $id, url_to_postid( '/example/example/' ) );
    357         $this->assertEquals( $id, url_to_postid( '/example/example' ) );
     354        $this->assertSame( $id, url_to_postid( get_permalink( $id ) ) );
     355        $this->assertSame( $id, url_to_postid( site_url( '/example/example/' ) ) );
     356        $this->assertSame( $id, url_to_postid( '/example/example/' ) );
     357        $this->assertSame( $id, url_to_postid( '/example/example' ) );
    358358    }
    359359
     
    374374        // This url should NOT return a post ID.
    375375        $badurl = site_url( '/example-collision' );
    376         $this->assertEquals( 0, url_to_postid( $badurl ) );
     376        $this->assertSame( 0, url_to_postid( $badurl ) );
    377377    }
    378378
     
    395395        // This url should NOT return a post ID.
    396396        $badurl = network_home_url( '/example-collision' );
    397         $this->assertEquals( 0, url_to_postid( $badurl ) );
     397        $this->assertSame( 0, url_to_postid( $badurl ) );
    398398
    399399        restore_current_blog();
     
    414414        $post_id = self::factory()->post->create( array( 'post_title' => get_post( $page_id )->post_title ) );
    415415
    416         $this->assertEquals( $post_id, url_to_postid( get_permalink( $post_id ) ) );
     416        $this->assertSame( $post_id, url_to_postid( get_permalink( $post_id ) ) );
    417417    }
    418418
Note: See TracChangeset for help on using the changeset viewer.