Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (3 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/rest-post-types-controller.php

    r47122 r48937  
    2323        $response = rest_get_server()->dispatch( $request );
    2424        $data     = $response->get_data();
    25         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     25        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    2626        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    2727        // Single.
     
    2929        $response = rest_get_server()->dispatch( $request );
    3030        $data     = $response->get_data();
    31         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     31        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    3232        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    3333    }
     
    3939        $data       = $response->get_data();
    4040        $post_types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
    41         $this->assertEquals( count( $post_types ), count( $data ) );
    42         $this->assertEquals( $post_types['post']->name, $data['post']['slug'] );
     41        $this->assertSame( count( $post_types ), count( $data ) );
     42        $this->assertSame( $post_types['post']->name, $data['post']['slug'] );
    4343        $this->check_post_type_obj( 'view', $post_types['post'], $data['post'], $data['post']['_links'] );
    44         $this->assertEquals( $post_types['page']->name, $data['page']['slug'] );
     44        $this->assertSame( $post_types['page']->name, $data['page']['slug'] );
    4545        $this->check_post_type_obj( 'view', $post_types['page'], $data['page'], $data['page']['_links'] );
    4646        $this->assertFalse( isset( $data['revision'] ) );
     
    6060        $this->check_post_type_object_response( 'view', $response );
    6161        $data = $response->get_data();
    62         $this->assertEquals( array( 'category', 'post_tag' ), $data['taxonomies'] );
     62        $this->assertSame( array( 'category', 'post_tag' ), $data['taxonomies'] );
    6363    }
    6464
     
    6868        $this->check_post_type_object_response( 'view', $response, 'page' );
    6969        $data = $response->get_data();
    70         $this->assertEquals( array(), $data['taxonomies'] );
     70        $this->assertSame( array(), $data['taxonomies'] );
    7171    }
    7272
     
    9898        $request  = new WP_REST_Request( 'POST', '/wp/v2/types' );
    9999        $response = rest_get_server()->dispatch( $request );
    100         $this->assertEquals( 404, $response->get_status() );
     100        $this->assertSame( 404, $response->get_status() );
    101101    }
    102102
     
    105105        $request  = new WP_REST_Request( 'POST', '/wp/v2/types/post' );
    106106        $response = rest_get_server()->dispatch( $request );
    107         $this->assertEquals( 404, $response->get_status() );
     107        $this->assertSame( 404, $response->get_status() );
    108108    }
    109109
     
    112112        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/types/post' );
    113113        $response = rest_get_server()->dispatch( $request );
    114         $this->assertEquals( 404, $response->get_status() );
     114        $this->assertSame( 404, $response->get_status() );
    115115    }
    116116
     
    131131        $request->set_param( '_fields', 'id,name' );
    132132        $response = $endpoint->prepare_item_for_response( $obj, $request );
    133         $this->assertEquals(
     133        $this->assertSame(
    134134            array(
    135135                // 'id' doesn't exist in this context.
     
    145145        $data       = $response->get_data();
    146146        $properties = $data['schema']['properties'];
    147         $this->assertEquals( 10, count( $properties ) );
     147        $this->assertSame( 10, count( $properties ) );
    148148        $this->assertArrayHasKey( 'capabilities', $properties );
    149149        $this->assertArrayHasKey( 'description', $properties );
     
    183183
    184184        $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
    185         $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
     185        $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
    186186
    187187        $request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
     
    199199
    200200    protected function check_post_type_obj( $context, $post_type_obj, $data, $links ) {
    201         $this->assertEquals( $post_type_obj->label, $data['name'] );
    202         $this->assertEquals( $post_type_obj->name, $data['slug'] );
    203         $this->assertEquals( $post_type_obj->description, $data['description'] );
    204         $this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] );
    205         $this->assertEquals( $post_type_obj->rest_base, $data['rest_base'] );
     201        $this->assertSame( $post_type_obj->label, $data['name'] );
     202        $this->assertSame( $post_type_obj->name, $data['slug'] );
     203        $this->assertSame( $post_type_obj->description, $data['description'] );
     204        $this->assertSame( $post_type_obj->hierarchical, $data['hierarchical'] );
     205        $this->assertSame( $post_type_obj->rest_base, $data['rest_base'] );
    206206
    207207        $links = test_rest_expand_compact_links( $links );
    208         $this->assertEquals( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
     208        $this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
    209209        $this->assertArrayHasKey( 'https://api.w.org/items', $links );
    210210        if ( 'edit' === $context ) {
    211             $this->assertEquals( $post_type_obj->cap, $data['capabilities'] );
    212             $this->assertEquals( $post_type_obj->labels, $data['labels'] );
     211            $this->assertSame( $post_type_obj->cap, $data['capabilities'] );
     212            $this->assertSame( $post_type_obj->labels, $data['labels'] );
    213213            if ( in_array( $post_type_obj->name, array( 'post', 'page' ), true ) ) {
    214214                $viewable = true;
     
    216216                $viewable = is_post_type_viewable( $post_type_obj );
    217217            }
    218             $this->assertEquals( $viewable, $data['viewable'] );
    219             $this->assertEquals( get_all_post_type_supports( $post_type_obj->name ), $data['supports'] );
     218            $this->assertSame( $viewable, $data['viewable'] );
     219            $this->assertSame( get_all_post_type_supports( $post_type_obj->name ), $data['supports'] );
    220220        } else {
    221221            $this->assertFalse( isset( $data['capabilities'] ) );
     
    227227
    228228    protected function check_post_type_object_response( $context, $response, $post_type = 'post' ) {
    229         $this->assertEquals( 200, $response->get_status() );
     229        $this->assertSame( 200, $response->get_status() );
    230230        $data = $response->get_data();
    231231        $obj  = get_post_type_object( $post_type );
Note: See TracChangeset for help on using the changeset viewer.