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/rest-api.php

    r48881 r48937  
    4343     */
    4444    function test_init_action_added() {
    45         $this->assertEquals( 10, has_action( 'init', 'rest_api_init' ) );
     45        $this->assertSame( 10, has_action( 'init', 'rest_api_init' ) );
    4646    }
    4747
     
    4949        $taxonomy = get_taxonomy( 'category' );
    5050        $this->assertTrue( $taxonomy->show_in_rest );
    51         $this->assertEquals( 'categories', $taxonomy->rest_base );
    52         $this->assertEquals( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
     51        $this->assertSame( 'categories', $taxonomy->rest_base );
     52        $this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
    5353
    5454        $taxonomy = get_taxonomy( 'post_tag' );
    5555        $this->assertTrue( $taxonomy->show_in_rest );
    56         $this->assertEquals( 'tags', $taxonomy->rest_base );
    57         $this->assertEquals( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
     56        $this->assertSame( 'tags', $taxonomy->rest_base );
     57        $this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
    5858    }
    5959
     
    6161        $post_type = get_post_type_object( 'post' );
    6262        $this->assertTrue( $post_type->show_in_rest );
    63         $this->assertEquals( 'posts', $post_type->rest_base );
    64         $this->assertEquals( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
     63        $this->assertSame( 'posts', $post_type->rest_base );
     64        $this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
    6565
    6666        $post_type = get_post_type_object( 'page' );
    6767        $this->assertTrue( $post_type->show_in_rest );
    68         $this->assertEquals( 'pages', $post_type->rest_base );
    69         $this->assertEquals( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
     68        $this->assertSame( 'pages', $post_type->rest_base );
     69        $this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
    7070
    7171        $post_type = get_post_type_object( 'attachment' );
    7272        $this->assertTrue( $post_type->show_in_rest );
    73         $this->assertEquals( 'media', $post_type->rest_base );
    74         $this->assertEquals( 'WP_REST_Attachments_Controller', $post_type->rest_controller_class );
     73        $this->assertSame( 'media', $post_type->rest_base );
     74        $this->assertSame( 'WP_REST_Attachments_Controller', $post_type->rest_controller_class );
    7575    }
    7676
     
    9999        $this->assertArrayNotHasKey( 'callback', $endpoint );
    100100        $this->assertArrayHasKey( 'namespace', $endpoint );
    101         $this->assertEquals( 'test-ns', $endpoint['namespace'] );
     101        $this->assertSame( 'test-ns', $endpoint['namespace'] );
    102102
    103103        // Grab the filtered data.
     
    142142        $this->assertArrayNotHasKey( 'callback', $endpoint );
    143143        $this->assertArrayHasKey( 'namespace', $endpoint );
    144         $this->assertEquals( 'test-ns', $endpoint['namespace'] );
     144        $this->assertSame( 'test-ns', $endpoint['namespace'] );
    145145
    146146        $filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
     
    282282        $routes = $GLOBALS['wp_rest_server']->get_routes();
    283283
    284         $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
     284        $this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
    285285    }
    286286
     
    301301        $routes = $GLOBALS['wp_rest_server']->get_routes();
    302302
    303         $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
     303        $this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
    304304    }
    305305
     
    320320        $routes = $GLOBALS['wp_rest_server']->get_routes();
    321321
    322         $this->assertEquals(
     322        $this->assertSame(
    323323            $routes['/test-ns/test'][0]['methods'],
    324324            array(
     
    345345        $routes = $GLOBALS['wp_rest_server']->get_routes();
    346346
    347         $this->assertEquals(
     347        $this->assertSame(
    348348            $routes['/test-ns/test'][0]['methods'],
    349349            array(
     
    371371        $this->assertArrayHasKey( 'Allow', $headers );
    372372
    373         $this->assertEquals( 'GET, POST', $headers['Allow'] );
     373        $this->assertSame( 'GET, POST', $headers['Allow'] );
    374374    }
    375375
     
    403403
    404404        $response = rest_filter_response_fields( $response, null, $request );
    405         $this->assertEquals( array( 'a' => true ), $response->get_data() );
     405        $this->assertSame( array( 'a' => true ), $response->get_data() );
    406406    }
    407407
     
    423423
    424424        $response = rest_filter_response_fields( $response, null, $request );
    425         $this->assertEquals( array( 'b' => 1 ), $response->get_data() );
     425        $this->assertSame( array( 'b' => 1 ), $response->get_data() );
    426426    }
    427427
     
    446446
    447447        $response = rest_filter_response_fields( $response, null, $request );
    448         $this->assertEquals(
     448        $this->assertSame(
    449449            array(
    450450                'b' => 1,
     
    478478
    479479        $response = rest_filter_response_fields( $response, null, $request );
    480         $this->assertEquals(
     480        $this->assertSame(
    481481            array(
    482482                'b' => 1,
     
    517517
    518518        $response = rest_filter_response_fields( $response, null, $request );
    519         $this->assertEquals(
     519        $this->assertSame(
    520520            array(
    521521                array(
     
    563563
    564564        $response = rest_filter_response_fields( $response, null, $request );
    565         $this->assertEquals(
     565        $this->assertSame(
    566566            array(
    567567                'b' => array(
     
    604604
    605605        $response = rest_filter_response_fields( $response, null, $request );
    606         $this->assertEquals(
     606        $this->assertSame(
    607607            array(
    608608                'field' => array(
     
    640640
    641641        $response = rest_filter_response_fields( $response, null, $request );
    642         $this->assertEquals(
     642        $this->assertSame(
    643643            array(
    644644                'meta' => array(
     
    672672
    673673        $response = rest_filter_response_fields( $response, null, $request );
    674         $this->assertEquals(
     674        $this->assertSame(
    675675            array(
    676676                'meta' => array(
     
    704704
    705705        $response = rest_filter_response_fields( $response, null, $request );
    706         $this->assertEquals(
     706        $this->assertSame(
    707707            array(
    708708                'meta' => array(
     
    746746        // In pretty permalinks case, we expect a path of wp-json/ with no query.
    747747        $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    748         $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
     748        $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
    749749
    750750        // In index permalinks case, we expect a path of index.php/wp-json/ with no query.
    751751        $this->set_permalink_structure( '/index.php/%year%/%monthnum%/%day%/%postname%/' );
    752         $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
     752        $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
    753753
    754754        // In non-pretty case, we get a query string to invoke the rest router.
    755755        $this->set_permalink_structure( '' );
    756         $this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php?rest_route=/', get_rest_url() );
     756        $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php?rest_route=/', get_rest_url() );
    757757    }
    758758
     
    808808        get_rest_url();
    809809        $args = $filter->get_args();
    810         $this->assertEquals( '/', $args[0][1] );
     810        $this->assertSame( '/', $args[0][1] );
    811811        $filter->reset();
    812812
     
    814814        get_rest_url( null, 'wp/media/' );
    815815        $args = $filter->get_args();
    816         $this->assertEquals( '/wp/media/', $args[0][1] );
     816        $this->assertSame( '/wp/media/', $args[0][1] );
    817817        $filter->reset();
    818818
     
    820820        get_rest_url( null, '/wp/media/' );
    821821        $args = $filter->get_args();
    822         $this->assertEquals( '/wp/media/', $args[0][1] );
     822        $this->assertSame( '/wp/media/', $args[0][1] );
    823823
    824824        unset( $filter );
     
    847847     */
    848848    public function test_jsonp_callback_check( $callback, $valid ) {
    849         $this->assertEquals( $valid, wp_check_jsonp_callback( $callback ) );
     849        $this->assertSame( $valid, wp_check_jsonp_callback( $callback ) );
    850850    }
    851851
     
    911911     */
    912912    public function test_rest_parse_date_force_utc( $string, $value ) {
    913         $this->assertEquals( $value, rest_parse_date( $string, true ) );
     913        $this->assertSame( $value, rest_parse_date( $string, true ) );
    914914    }
    915915
     
    933933
    934934        $routes = $GLOBALS['wp_rest_server']->get_routes();
    935         $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
     935        $this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
    936936    }
    937937
     
    963963        $request = rest_ensure_request( '/wp/v2/posts' );
    964964        $this->assertInstanceOf( 'WP_REST_Request', $request );
    965         $this->assertEquals( '/wp/v2/posts', $request->get_route() );
    966         $this->assertEquals( 'GET', $request->get_method() );
     965        $this->assertSame( '/wp/v2/posts', $request->get_route() );
     966        $this->assertSame( 'GET', $request->get_method() );
    967967    }
    968968
     
    971971     */
    972972    public function test_rest_parse_embed_param( $expected, $embed ) {
    973         $this->assertEquals( $expected, rest_parse_embed_param( $embed ) );
     973        $this->assertSame( $expected, rest_parse_embed_param( $embed ) );
    974974    }
    975975
     
    10001000     */
    10011001    public function test_rest_filter_response_by_context( $schema, $data, $expected ) {
    1002         $this->assertEquals( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
     1002        $this->assertSame( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
    10031003    }
    10041004
     
    15531553     */
    15541554    public function test_rest_get_route_for_post_non_post() {
    1555         $this->assertEquals( '', rest_get_route_for_post( 'garbage' ) );
     1555        $this->assertSame( '', rest_get_route_for_post( 'garbage' ) );
    15561556    }
    15571557
     
    15641564        unregister_post_type( 'invalid' );
    15651565
    1566         $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1566        $this->assertSame( '', rest_get_route_for_post( $post ) );
    15671567    }
    15681568
     
    15721572    public function test_rest_get_route_for_post_non_rest() {
    15731573        $post = self::factory()->post->create_and_get( array( 'post_type' => 'custom_css' ) );
    1574         $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1574        $this->assertSame( '', rest_get_route_for_post( $post ) );
    15751575    }
    15761576
     
    15801580    public function test_rest_get_route_for_post_custom_controller() {
    15811581        $post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
    1582         $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1582        $this->assertSame( '', rest_get_route_for_post( $post ) );
    15831583    }
    15841584
     
    15881588    public function test_rest_get_route_for_post() {
    15891589        $post = self::factory()->post->create_and_get();
    1590         $this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post ) );
     1590        $this->assertSame( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post ) );
    15911591    }
    15921592
     
    15961596    public function test_rest_get_route_for_media() {
    15971597        $post = self::factory()->attachment->create_and_get();
    1598         $this->assertEquals( '/wp/v2/media/' . $post->ID, rest_get_route_for_post( $post ) );
     1598        $this->assertSame( '/wp/v2/media/' . $post->ID, rest_get_route_for_post( $post ) );
    15991599    }
    16001600
     
    16041604    public function test_rest_get_route_for_post_id() {
    16051605        $post = self::factory()->post->create_and_get();
    1606         $this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post->ID ) );
     1606        $this->assertSame( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post->ID ) );
    16071607    }
    16081608
     
    16111611     */
    16121612    public function test_rest_get_route_for_term_non_term() {
    1613         $this->assertEquals( '', rest_get_route_for_term( 'garbage' ) );
     1613        $this->assertSame( '', rest_get_route_for_term( 'garbage' ) );
    16141614    }
    16151615
     
    16221622        unregister_taxonomy( 'invalid' );
    16231623
    1624         $this->assertEquals( '', rest_get_route_for_term( $term ) );
     1624        $this->assertSame( '', rest_get_route_for_term( $term ) );
    16251625    }
    16261626
     
    16301630    public function test_rest_get_route_for_term_non_rest() {
    16311631        $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'post_format' ) );
    1632         $this->assertEquals( '', rest_get_route_for_term( $term ) );
     1632        $this->assertSame( '', rest_get_route_for_term( $term ) );
    16331633    }
    16341634
     
    16381638    public function test_rest_get_route_for_term() {
    16391639        $term = self::factory()->term->create_and_get();
    1640         $this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term ) );
     1640        $this->assertSame( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term ) );
    16411641    }
    16421642
     
    16461646    public function test_rest_get_route_for_category() {
    16471647        $term = self::factory()->category->create_and_get();
    1648         $this->assertEquals( '/wp/v2/categories/' . $term->term_id, rest_get_route_for_term( $term ) );
     1648        $this->assertSame( '/wp/v2/categories/' . $term->term_id, rest_get_route_for_term( $term ) );
    16491649    }
    16501650
     
    16541654    public function test_rest_get_route_for_term_id() {
    16551655        $term = self::factory()->term->create_and_get();
    1656         $this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term->term_id ) );
     1656        $this->assertSame( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term->term_id ) );
    16571657    }
    16581658
     
    17301730    public function test_rest_sanitize_object( $expected, $value ) {
    17311731        $sanitized = rest_sanitize_object( $value );
    1732         $this->assertEquals( $expected, $sanitized );
     1732        $this->assertSame( $expected, $sanitized );
    17331733    }
    17341734
     
    18591859    public function test_rest_sanitize_array( $expected, $value ) {
    18601860        $sanitized = rest_sanitize_array( $value );
    1861         $this->assertEquals( $expected, $sanitized );
     1861        $this->assertSame( $expected, $sanitized );
    18621862    }
    18631863
     
    19921992     */
    19931993    public function test_get_best_type_for_value( $expected, $value, $types ) {
    1994         $this->assertEquals( $expected, rest_get_best_type_for_value( $value, $types ) );
     1994        $this->assertSame( $expected, rest_get_best_type_for_value( $value, $types ) );
    19951995    }
    19961996
Note: See TracChangeset for help on using the changeset viewer.