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-revisions-controller.php

    r47547 r48937  
    111111        $response = rest_get_server()->dispatch( $request );
    112112        $data     = $response->get_data();
    113         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     113        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    114114        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    115115        // Single.
     
    117117        $response = rest_get_server()->dispatch( $request );
    118118        $data     = $response->get_data();
    119         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     119        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
    120120        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    121121    }
     
    126126        $response = rest_get_server()->dispatch( $request );
    127127        $data     = $response->get_data();
    128         $this->assertEquals( 200, $response->get_status() );
     128        $this->assertSame( 200, $response->get_status() );
    129129        $this->assertCount( $this->total_revisions, $data );
    130130
    131131        // Reverse chronology.
    132         $this->assertEquals( $this->revision_id3, $data[0]['id'] );
     132        $this->assertSame( $this->revision_id3, $data[0]['id'] );
    133133        $this->check_get_revision_response( $data[0], $this->revision_3 );
    134134
    135         $this->assertEquals( $this->revision_id2, $data[1]['id'] );
     135        $this->assertSame( $this->revision_id2, $data[1]['id'] );
    136136        $this->check_get_revision_response( $data[1], $this->revision_2 );
    137137
    138         $this->assertEquals( $this->revision_id1, $data[2]['id'] );
     138        $this->assertSame( $this->revision_id1, $data[2]['id'] );
    139139        $this->check_get_revision_response( $data[2], $this->revision_1 );
    140140    }
     
    169169        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    170170        $response = rest_get_server()->dispatch( $request );
    171         $this->assertEquals( 200, $response->get_status() );
     171        $this->assertSame( 200, $response->get_status() );
    172172        $this->check_get_revision_response( $response, $this->revision_1 );
    173173        $fields = array(
     
    263263        $request->set_param( 'force', true );
    264264        $response = rest_get_server()->dispatch( $request );
    265         $this->assertEquals( 200, $response->get_status() );
     265        $this->assertSame( 200, $response->get_status() );
    266266        $this->assertNull( get_post( $this->revision_id1 ) );
    267267    }
     
    309309        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    310310        $response = rest_get_server()->dispatch( $request );
    311         $this->assertEquals( 200, $response->get_status() );
     311        $this->assertSame( 200, $response->get_status() );
    312312        $this->check_get_revision_response( $response, $this->revision_1 );
    313313    }
     
    321321        $revision = get_post( $this->revision_id1 );
    322322        $response = $endpoint->prepare_item_for_response( $revision, $request );
    323         $this->assertEquals(
     323        $this->assertSame(
    324324            array(
    325325                'id',
     
    335335        $data       = $response->get_data();
    336336        $properties = $data['schema']['properties'];
    337         $this->assertEquals( 12, count( $properties ) );
     337        $this->assertSame( 12, count( $properties ) );
    338338        $this->assertArrayHasKey( 'author', $properties );
    339339        $this->assertArrayHasKey( 'content', $properties );
     
    387387
    388388        $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
    389         $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
     389        $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
    390390
    391391        wp_set_current_user( 1 );
     
    420420
    421421        $rendered_content = apply_filters( 'the_content', $revision->post_content );
    422         $this->assertEquals( $rendered_content, $response['content']['rendered'] );
    423 
    424         $this->assertEquals( mysql_to_rfc3339( $revision->post_date ), $response['date'] );
    425         $this->assertEquals( mysql_to_rfc3339( $revision->post_date_gmt ), $response['date_gmt'] );
     422        $this->assertSame( $rendered_content, $response['content']['rendered'] );
     423
     424        $this->assertSame( mysql_to_rfc3339( $revision->post_date ), $response['date'] );
     425        $this->assertSame( mysql_to_rfc3339( $revision->post_date_gmt ), $response['date_gmt'] );
    426426
    427427        $rendered_excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $revision->post_excerpt, $revision ) );
    428         $this->assertEquals( $rendered_excerpt, $response['excerpt']['rendered'] );
     428        $this->assertSame( $rendered_excerpt, $response['excerpt']['rendered'] );
    429429
    430430        $rendered_guid = apply_filters( 'get_the_guid', $revision->guid, $revision->ID );
    431         $this->assertEquals( $rendered_guid, $response['guid']['rendered'] );
    432 
    433         $this->assertEquals( $revision->ID, $response['id'] );
    434         $this->assertEquals( mysql_to_rfc3339( $revision->post_modified ), $response['modified'] );
    435         $this->assertEquals( mysql_to_rfc3339( $revision->post_modified_gmt ), $response['modified_gmt'] );
    436         $this->assertEquals( $revision->post_name, $response['slug'] );
     431        $this->assertSame( $rendered_guid, $response['guid']['rendered'] );
     432
     433        $this->assertSame( $revision->ID, $response['id'] );
     434        $this->assertSame( mysql_to_rfc3339( $revision->post_modified ), $response['modified'] );
     435        $this->assertSame( mysql_to_rfc3339( $revision->post_modified_gmt ), $response['modified_gmt'] );
     436        $this->assertSame( $revision->post_name, $response['slug'] );
    437437
    438438        $rendered_title = get_the_title( $revision->ID );
    439         $this->assertEquals( $rendered_title, $response['title']['rendered'] );
     439        $this->assertSame( $rendered_title, $response['title']['rendered'] );
    440440
    441441        $parent            = get_post( $revision->post_parent );
     
    443443        $parent_object     = get_post_type_object( $parent->post_type );
    444444        $parent_base       = ! empty( $parent_object->rest_base ) ? $parent_object->rest_base : $parent_object->name;
    445         $this->assertEquals( rest_url( '/wp/v2/' . $parent_base . '/' . $revision->post_parent ), $links['parent'][0]['href'] );
     445        $this->assertSame( rest_url( '/wp/v2/' . $parent_base . '/' . $revision->post_parent ), $links['parent'][0]['href'] );
    446446    }
    447447
     
    454454        $parent_post_id = wp_is_post_revision( $post->ID );
    455455
    456         $this->assertEquals( $post->ID, $this->revision_id1 );
    457         $this->assertEquals( $parent_post_id, self::$post_id );
     456        $this->assertSame( $post->ID, $this->revision_id1 );
     457        $this->assertSame( $parent_post_id, self::$post_id );
    458458    }
    459459
Note: See TracChangeset for help on using the changeset viewer.