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

    r48273 r48937  
    3838        $response = rest_get_server()->dispatch( $request );
    3939        $data     = $response->get_data();
    40         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     40        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    4141        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    4242        // Single.
     
    4444        $response = rest_get_server()->dispatch( $request );
    4545        $data     = $response->get_data();
    46         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     46        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    4747        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    4848    }
     
    5353        $data       = $response->get_data();
    5454        $taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) );
    55         $this->assertEquals( count( $taxonomies ), count( $data ) );
    56         $this->assertEquals( 'Categories', $data['category']['name'] );
    57         $this->assertEquals( 'category', $data['category']['slug'] );
    58         $this->assertEquals( true, $data['category']['hierarchical'] );
    59         $this->assertEquals( 'Tags', $data['post_tag']['name'] );
    60         $this->assertEquals( 'post_tag', $data['post_tag']['slug'] );
    61         $this->assertEquals( false, $data['post_tag']['hierarchical'] );
    62         $this->assertEquals( 'tags', $data['post_tag']['rest_base'] );
     55        $this->assertSame( count( $taxonomies ), count( $data ) );
     56        $this->assertSame( 'Categories', $data['category']['name'] );
     57        $this->assertSame( 'category', $data['category']['slug'] );
     58        $this->assertTrue( $data['category']['hierarchical'] );
     59        $this->assertSame( 'Tags', $data['post_tag']['name'] );
     60        $this->assertSame( 'post_tag', $data['post_tag']['slug'] );
     61        $this->assertFalse( $data['post_tag']['hierarchical'] );
     62        $this->assertSame( 'tags', $data['post_tag']['rest_base'] );
    6363    }
    6464
     
    7070        $data       = $response->get_data();
    7171        $taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) );
    72         $this->assertEquals( count( $taxonomies ), count( $data ) );
    73         $this->assertEquals( 'Categories', $data['category']['name'] );
    74         $this->assertEquals( 'category', $data['category']['slug'] );
    75         $this->assertEquals( true, $data['category']['hierarchical'] );
    76         $this->assertEquals( 'Tags', $data['post_tag']['name'] );
    77         $this->assertEquals( 'post_tag', $data['post_tag']['slug'] );
    78         $this->assertEquals( false, $data['post_tag']['hierarchical'] );
    79         $this->assertEquals( 'tags', $data['post_tag']['rest_base'] );
     72        $this->assertSame( count( $taxonomies ), count( $data ) );
     73        $this->assertSame( 'Categories', $data['category']['name'] );
     74        $this->assertSame( 'category', $data['category']['slug'] );
     75        $this->assertTrue( $data['category']['hierarchical'] );
     76        $this->assertSame( 'Tags', $data['post_tag']['name'] );
     77        $this->assertSame( 'post_tag', $data['post_tag']['slug'] );
     78        $this->assertFalse( $data['post_tag']['hierarchical'] );
     79        $this->assertSame( 'tags', $data['post_tag']['rest_base'] );
    8080    }
    8181
     
    9999        $request->set_param( 'type', 'wingding' );
    100100        $response = rest_get_server()->dispatch( $request );
    101         $this->assertEquals( 200, $response->get_status() );
     101        $this->assertSame( 200, $response->get_status() );
    102102        $data = $response->get_data();
    103         $this->assertEquals( '{}', json_encode( $data ) );
     103        $this->assertSame( '{}', json_encode( $data ) );
    104104    }
    105105
     
    154154        $request  = new WP_REST_Request( 'POST', '/wp/v2/taxonomies' );
    155155        $response = rest_get_server()->dispatch( $request );
    156         $this->assertEquals( 404, $response->get_status() );
     156        $this->assertSame( 404, $response->get_status() );
    157157    }
    158158
     
    161161        $request  = new WP_REST_Request( 'POST', '/wp/v2/taxonomies/category' );
    162162        $response = rest_get_server()->dispatch( $request );
    163         $this->assertEquals( 404, $response->get_status() );
     163        $this->assertSame( 404, $response->get_status() );
    164164    }
    165165
     
    168168        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/taxonomies/category' );
    169169        $response = rest_get_server()->dispatch( $request );
    170         $this->assertEquals( 404, $response->get_status() );
     170        $this->assertSame( 404, $response->get_status() );
    171171    }
    172172
     
    187187        $request->set_param( '_fields', 'id,name' );
    188188        $response = $endpoint->prepare_item_for_response( $tax, $request );
    189         $this->assertEquals(
     189        $this->assertSame(
    190190            array(
    191191                // 'id' doesn't exist in this context.
     
    209209        $types = $response->get_data()['types'];
    210210        $this->assertArrayHasKey( 0, $types );
    211         $this->assertEquals( 'post', $types[0] );
     211        $this->assertSame( 'post', $types[0] );
    212212        $this->assertArrayHasKey( 1, $types );
    213         $this->assertEquals( 'attachment', $types[1] );
    214         $this->assertEquals( 2, count( $types ) );
     213        $this->assertSame( 'attachment', $types[1] );
     214        $this->assertSame( 2, count( $types ) );
    215215    }
    216216
     
    220220        $data       = $response->get_data();
    221221        $properties = $data['schema']['properties'];
    222         $this->assertEquals( 10, count( $properties ) );
     222        $this->assertSame( 10, count( $properties ) );
    223223        $this->assertArrayHasKey( 'capabilities', $properties );
    224224        $this->assertArrayHasKey( 'description', $properties );
     
    252252
    253253    protected function check_taxonomy_object( $context, $tax_obj, $data, $links ) {
    254         $this->assertEquals( $tax_obj->label, $data['name'] );
    255         $this->assertEquals( $tax_obj->name, $data['slug'] );
    256         $this->assertEquals( $tax_obj->description, $data['description'] );
    257         $this->assertEquals( $tax_obj->hierarchical, $data['hierarchical'] );
    258         $this->assertEquals( $tax_obj->rest_base, $data['rest_base'] );
    259         $this->assertEquals( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
     254        $this->assertSame( $tax_obj->label, $data['name'] );
     255        $this->assertSame( $tax_obj->name, $data['slug'] );
     256        $this->assertSame( $tax_obj->description, $data['description'] );
     257        $this->assertSame( $tax_obj->hierarchical, $data['hierarchical'] );
     258        $this->assertSame( $tax_obj->rest_base, $data['rest_base'] );
     259        $this->assertSame( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
    260260        $this->assertArrayHasKey( 'https://api.w.org/items', $links );
    261261        if ( 'edit' === $context ) {
    262             $this->assertEquals( $tax_obj->cap, $data['capabilities'] );
    263             $this->assertEquals( $tax_obj->labels, $data['labels'] );
    264             $this->assertEquals( $tax_obj->show_tagcloud, $data['show_cloud'] );
    265 
    266             $this->assertEquals( $tax_obj->public, $data['visibility']['public'] );
    267             $this->assertEquals( $tax_obj->publicly_queryable, $data['visibility']['publicly_queryable'] );
    268             $this->assertEquals( $tax_obj->show_admin_column, $data['visibility']['show_admin_column'] );
    269             $this->assertEquals( $tax_obj->show_in_nav_menus, $data['visibility']['show_in_nav_menus'] );
    270             $this->assertEquals( $tax_obj->show_in_quick_edit, $data['visibility']['show_in_quick_edit'] );
    271             $this->assertEquals( $tax_obj->show_ui, $data['visibility']['show_ui'] );
     262            $this->assertSame( $tax_obj->cap, $data['capabilities'] );
     263            $this->assertSame( $tax_obj->labels, $data['labels'] );
     264            $this->assertSame( $tax_obj->show_tagcloud, $data['show_cloud'] );
     265
     266            $this->assertSame( $tax_obj->public, $data['visibility']['public'] );
     267            $this->assertSame( $tax_obj->publicly_queryable, $data['visibility']['publicly_queryable'] );
     268            $this->assertSame( $tax_obj->show_admin_column, $data['visibility']['show_admin_column'] );
     269            $this->assertSame( $tax_obj->show_in_nav_menus, $data['visibility']['show_in_nav_menus'] );
     270            $this->assertSame( $tax_obj->show_in_quick_edit, $data['visibility']['show_in_quick_edit'] );
     271            $this->assertSame( $tax_obj->show_ui, $data['visibility']['show_ui'] );
    272272        } else {
    273273            $this->assertFalse( isset( $data['capabilities'] ) );
     
    279279
    280280    protected function check_taxonomy_object_response( $context, $response ) {
    281         $this->assertEquals( 200, $response->get_status() );
     281        $this->assertSame( 200, $response->get_status() );
    282282        $data     = $response->get_data();
    283283        $category = get_taxonomy( 'category' );
     
    286286
    287287    protected function check_taxonomies_for_type_response( $type, $response ) {
    288         $this->assertEquals( 200, $response->get_status() );
     288        $this->assertSame( 200, $response->get_status() );
    289289        $data       = $response->get_data();
    290290        $taxonomies = $this->get_public_taxonomies( get_object_taxonomies( $type, 'objects' ) );
    291         $this->assertEquals( count( $taxonomies ), count( $data ) );
     291        $this->assertSame( count( $taxonomies ), count( $data ) );
    292292    }
    293293
Note: See TracChangeset for help on using the changeset viewer.