WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (17 months 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-autosaves-controller.php

    r46586 r48937  
    141141        $response = rest_get_server()->dispatch( $request );
    142142        $data     = $response->get_data();
    143         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     143        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    144144        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    145145
     
    148148        $response = rest_get_server()->dispatch( $request );
    149149        $data     = $response->get_data();
    150         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     150        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    151151        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    152152    }
     
    158158        $keys     = array_keys( $data['endpoints'][0]['args'] );
    159159        sort( $keys );
    160         $this->assertEquals(
     160        $this->assertSame(
    161161            array(
    162162                'context',
     
    172172        $response = rest_get_server()->dispatch( $request );
    173173        $data     = $response->get_data();
    174         $this->assertEquals( 200, $response->get_status() );
     174        $this->assertSame( 200, $response->get_status() );
    175175        $this->assertCount( 1, $data );
    176176
    177         $this->assertEquals( self::$autosave_post_id, $data[0]['id'] );
     177        $this->assertSame( self::$autosave_post_id, $data[0]['id'] );
    178178
    179179        $this->check_get_autosave_response( $data[0], $this->post_autosave );
     
    208208        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );
    209209        $response = rest_get_server()->dispatch( $request );
    210         $this->assertEquals( 200, $response->get_status() );
     210        $this->assertSame( 200, $response->get_status() );
    211211        $data = $response->get_data();
    212212
     
    278278        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/autosaves/' . self::$autosave_post_id );
    279279        $response = rest_get_server()->dispatch( $request );
    280         $this->assertEquals( 200, $response->get_status() );
     280        $this->assertSame( 200, $response->get_status() );
    281281        $this->check_get_autosave_response( $response, $this->post_autosave );
    282282    }
     
    287287        $data       = $response->get_data();
    288288        $properties = $data['schema']['properties'];
    289         $this->assertEquals( 13, count( $properties ) );
     289        $this->assertSame( 13, count( $properties ) );
    290290        $this->assertArrayHasKey( 'author', $properties );
    291291        $this->assertArrayHasKey( 'content', $properties );
     
    378378        $new_data = $response->get_data();
    379379
    380         $this->assertEquals( $current_post->ID, $new_data['parent'] );
    381         $this->assertEquals( $current_post->post_title, $new_data['title']['raw'] );
    382         $this->assertEquals( $current_post->post_excerpt, $new_data['excerpt']['raw'] );
     380        $this->assertSame( $current_post->ID, $new_data['parent'] );
     381        $this->assertSame( $current_post->post_title, $new_data['title']['raw'] );
     382        $this->assertSame( $current_post->post_excerpt, $new_data['excerpt']['raw'] );
    383383
    384384        // Updated post_content.
     
    386386
    387387        $autosave_post = wp_get_post_autosave( self::$post_id );
    388         $this->assertEquals( $autosave_data['title'], $autosave_post->post_title );
    389         $this->assertEquals( $autosave_data['content'], $autosave_post->post_content );
    390         $this->assertEquals( $autosave_data['excerpt'], $autosave_post->post_excerpt );
     388        $this->assertSame( $autosave_data['title'], $autosave_post->post_title );
     389        $this->assertSame( $autosave_data['content'], $autosave_post->post_content );
     390        $this->assertSame( $autosave_data['excerpt'], $autosave_post->post_excerpt );
    391391    }
    392392
     
    415415        $post     = get_post( $post_id );
    416416
    417         $this->assertEquals( $post_id, $new_data['id'] );
     417        $this->assertSame( $post_id, $new_data['id'] );
    418418        // The draft post should be updated.
    419         $this->assertEquals( $autosave_data['content'], $new_data['content']['raw'] );
    420         $this->assertEquals( $autosave_data['title'], $new_data['title']['raw'] );
    421         $this->assertEquals( $autosave_data['content'], $post->post_content );
    422         $this->assertEquals( $autosave_data['title'], $post->post_title );
     419        $this->assertSame( $autosave_data['content'], $new_data['content']['raw'] );
     420        $this->assertSame( $autosave_data['title'], $new_data['title']['raw'] );
     421        $this->assertSame( $autosave_data['content'], $post->post_content );
     422        $this->assertSame( $autosave_data['title'], $post->post_title );
    423423
    424424        // Not updated.
    425         $this->assertEquals( $post_data['post_excerpt'], $post->post_excerpt );
     425        $this->assertSame( $post_data['post_excerpt'], $post->post_excerpt );
    426426
    427427        wp_delete_post( $post_id );
     
    454454        $current_post = get_post( $post_id );
    455455
    456         $this->assertEquals( $current_post->ID, $new_data['parent'] );
     456        $this->assertSame( $current_post->ID, $new_data['parent'] );
    457457
    458458        // The draft post shouldn't change.
    459         $this->assertEquals( $current_post->post_title, $post_data['post_title'] );
    460         $this->assertEquals( $current_post->post_content, $post_data['post_content'] );
    461         $this->assertEquals( $current_post->post_excerpt, $post_data['post_excerpt'] );
     459        $this->assertSame( $current_post->post_title, $post_data['post_title'] );
     460        $this->assertSame( $current_post->post_content, $post_data['post_content'] );
     461        $this->assertSame( $current_post->post_excerpt, $post_data['post_excerpt'] );
    462462
    463463        $autosave_post = wp_get_post_autosave( $post_id );
    464464
    465465        // No changes.
    466         $this->assertEquals( $current_post->post_title, $autosave_post->post_title );
    467         $this->assertEquals( $current_post->post_excerpt, $autosave_post->post_excerpt );
     466        $this->assertSame( $current_post->post_title, $autosave_post->post_title );
     467        $this->assertSame( $current_post->post_excerpt, $autosave_post->post_excerpt );
    468468
    469469        // Has changes.
    470         $this->assertEquals( $autosave_data['content'], $autosave_post->post_content );
     470        $this->assertSame( $autosave_data['content'], $autosave_post->post_content );
    471471
    472472        wp_delete_post( $post_id );
     
    497497
    498498        $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
    499         $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
     499        $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
    500500
    501501        wp_set_current_user( 1 );
     
    530530
    531531        $rendered_content = apply_filters( 'the_content', $autosave->post_content );
    532         $this->assertEquals( $rendered_content, $response['content']['rendered'] );
    533 
    534         $this->assertEquals( mysql_to_rfc3339( $autosave->post_date ), $response['date'] ); //@codingStandardsIgnoreLine
    535         $this->assertEquals( mysql_to_rfc3339( $autosave->post_date_gmt ), $response['date_gmt'] ); //@codingStandardsIgnoreLine
     532        $this->assertSame( $rendered_content, $response['content']['rendered'] );
     533
     534        $this->assertSame( mysql_to_rfc3339( $autosave->post_date ), $response['date'] ); //@codingStandardsIgnoreLine
     535        $this->assertSame( mysql_to_rfc3339( $autosave->post_date_gmt ), $response['date_gmt'] ); //@codingStandardsIgnoreLine
    536536
    537537        $rendered_guid = apply_filters( 'get_the_guid', $autosave->guid, $autosave->ID );
    538         $this->assertEquals( $rendered_guid, $response['guid']['rendered'] );
    539 
    540         $this->assertEquals( $autosave->ID, $response['id'] );
    541         $this->assertEquals( mysql_to_rfc3339( $autosave->post_modified ), $response['modified'] ); //@codingStandardsIgnoreLine
    542         $this->assertEquals( mysql_to_rfc3339( $autosave->post_modified_gmt ), $response['modified_gmt'] ); //@codingStandardsIgnoreLine
    543         $this->assertEquals( $autosave->post_name, $response['slug'] );
     538        $this->assertSame( $rendered_guid, $response['guid']['rendered'] );
     539
     540        $this->assertSame( $autosave->ID, $response['id'] );
     541        $this->assertSame( mysql_to_rfc3339( $autosave->post_modified ), $response['modified'] ); //@codingStandardsIgnoreLine
     542        $this->assertSame( mysql_to_rfc3339( $autosave->post_modified_gmt ), $response['modified_gmt'] ); //@codingStandardsIgnoreLine
     543        $this->assertSame( $autosave->post_name, $response['slug'] );
    544544
    545545        $rendered_title = get_the_title( $autosave->ID );
    546         $this->assertEquals( $rendered_title, $response['title']['rendered'] );
     546        $this->assertSame( $rendered_title, $response['title']['rendered'] );
    547547
    548548        $parent            = get_post( $autosave->post_parent );
     
    550550        $parent_object     = get_post_type_object( $parent->post_type );
    551551        $parent_base       = ! empty( $parent_object->rest_base ) ? $parent_object->rest_base : $parent_object->name;
    552         $this->assertEquals( rest_url( '/wp/v2/' . $parent_base . '/' . $autosave->post_parent ), $links['parent'][0]['href'] );
     552        $this->assertSame( rest_url( '/wp/v2/' . $parent_base . '/' . $autosave->post_parent ), $links['parent'][0]['href'] );
    553553    }
    554554
     
    561561        $parent_post_id = wp_is_post_revision( $post->ID );
    562562
    563         $this->assertEquals( $post->ID, self::$autosave_post_id );
    564         $this->assertEquals( $parent_post_id, self::$post_id );
     563        $this->assertSame( $post->ID, self::$autosave_post_id );
     564        $this->assertSame( $parent_post_id, self::$post_id );
    565565    }
    566566
     
    581581        $data     = $response->get_data();
    582582
    583         $this->assertEquals( self::$child_draft_page_id, $data['id'] );
    584         $this->assertEquals( self::$parent_page_id, $data['parent'] );
     583        $this->assertSame( self::$child_draft_page_id, $data['id'] );
     584        $this->assertSame( self::$parent_page_id, $data['parent'] );
    585585    }
    586586
Note: See TracChangeset for help on using the changeset viewer.