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-request.php

    r48642 r48937  
    2424        $this->request->set_header( 'Content-Type', $value );
    2525
    26         $this->assertEquals( $value, $this->request->get_header( 'Content-Type' ) );
     26        $this->assertSame( $value, $this->request->get_header( 'Content-Type' ) );
    2727    }
    2828
     
    3434    public function test_remove_header() {
    3535        $this->request->add_header( 'Test-Header', 'value' );
    36         $this->assertEquals( 'value', $this->request->get_header( 'Test-Header' ) );
     36        $this->assertSame( 'value', $this->request->get_header( 'Test-Header' ) );
    3737
    3838        $this->request->remove_header( 'Test-Header' );
     
    4646        $this->request->add_header( 'Accept', $value2 );
    4747
    48         $this->assertEquals( $value1 . ',' . $value2, $this->request->get_header( 'Accept' ) );
    49         $this->assertEquals( array( $value1, $value2 ), $this->request->get_header_as_array( 'Accept' ) );
     48        $this->assertSame( $value1 . ',' . $value2, $this->request->get_header( 'Accept' ) );
     49        $this->assertSame( array( $value1, $value2 ), $this->request->get_header_as_array( 'Accept' ) );
    5050    }
    5151
     
    6767     */
    6868    public function test_header_canonicalization( $original, $expected ) {
    69         $this->assertEquals( $expected, $this->request->canonicalize_header_name( $original ) );
     69        $this->assertSame( $expected, $this->request->canonicalize_header_name( $original ) );
    7070    }
    7171
     
    9797        $parsed = $this->request->get_content_type();
    9898
    99         $this->assertEquals( $value, $parsed['value'] );
    100         $this->assertEquals( $type, $parsed['type'] );
    101         $this->assertEquals( $subtype, $parsed['subtype'] );
    102         $this->assertEquals( $parameters, $parsed['parameters'] );
     99        $this->assertSame( $value, $parsed['value'] );
     100        $this->assertSame( $type, $parsed['type'] );
     101        $this->assertSame( $subtype, $parsed['subtype'] );
     102        $this->assertSame( $parameters, $parsed['parameters'] );
    103103    }
    104104
     
    145145
    146146        // Check that query takes precedence.
    147         $this->assertEquals( 'query', $this->request->get_param( 'source' ) );
     147        $this->assertSame( 'query', $this->request->get_param( 'source' ) );
    148148
    149149        // Check that the correct arguments are parsed (and that falling through
     
    166166
    167167        // Check that POST takes precedence.
    168         $this->assertEquals( 'body', $this->request->get_param( 'source' ) );
     168        $this->assertSame( 'body', $this->request->get_param( 'source' ) );
    169169
    170170        // Check that the correct arguments are parsed (and that falling through
     
    187187
    188188        // Check that JSON takes precedence.
    189         $this->assertEquals( 'json', $this->request->get_param( 'source' ) );
     189        $this->assertSame( 'json', $this->request->get_param( 'source' ) );
    190190
    191191        // Check that the correct arguments are parsed (and that falling through
     
    209209
    210210        // Check that JSON is ignored.
    211         $this->assertEquals( 'body', $this->request->get_param( 'source' ) );
     211        $this->assertSame( 'body', $this->request->get_param( 'source' ) );
    212212
    213213        // Check that the correct arguments are parsed (and that falling through
     
    253253        $this->request->set_body( http_build_query( $data ) );
    254254        foreach ( $data as $key => $expected_value ) {
    255             $this->assertEquals( $expected_value, $this->request->get_param( $key ) );
     255            $this->assertSame( $expected_value, $this->request->get_param( $key ) );
    256256        }
    257257    }
     
    275275
    276276        foreach ( $data as $key => $expected_value ) {
    277             $this->assertEquals( $expected_value, $this->request->get_param( $key ) );
     277            $this->assertSame( $expected_value, $this->request->get_param( $key ) );
    278278        }
    279279    }
     
    297297
    298298        foreach ( $data as $key => $expected_value ) {
    299             $this->assertEquals( $expected_value, $this->request->get_param( $key ) );
     299            $this->assertSame( $expected_value, $this->request->get_param( $key ) );
    300300        }
    301301    }
     
    308308        $expected = array(
    309309            'source'             => 'body',
     310            'has_default_params' => true,
    310311            'has_url_params'     => true,
    311312            'has_query_params'   => true,
    312313            'has_body_params'    => true,
    313             'has_default_params' => true,
    314         );
    315         $this->assertEquals( $expected, $this->request->get_params() );
     314        );
     315        $this->assertSame( $expected, $this->request->get_params() );
    316316    }
    317317
     
    327327            '2' => 'goodbye',
    328328        );
    329         $this->assertEquals( $expected, $this->request->get_params() );
     329        $this->assertSame( $expected, $this->request->get_params() );
    330330    }
    331331
     
    353353        $this->request->sanitize_params();
    354354
    355         $this->assertEquals( 123, $this->request->get_param( 'someinteger' ) );
    356         $this->assertEquals( 0, $this->request->get_param( 'somestring' ) );
     355        $this->assertSame( 123, $this->request->get_param( 'someinteger' ) );
     356        $this->assertSame( 0, $this->request->get_param( 'somestring' ) );
    357357    }
    358358
     
    379379        $valid = $this->request->sanitize_params();
    380380        $this->assertWPError( $valid );
    381         $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
     381        $this->assertSame( 'rest_invalid_param', $valid->get_error_code() );
    382382    }
    383383
     
    440440
    441441        $this->assertWPError( $valid );
    442         $this->assertEquals( 'rest_missing_callback_param', $valid->get_error_code() );
     442        $this->assertSame( 'rest_missing_callback_param', $valid->get_error_code() );
    443443    }
    444444
     
    460460
    461461        $this->assertWPError( $valid );
    462         $this->assertEquals( 'rest_missing_callback_param', $valid->get_error_code() );
     462        $this->assertSame( 'rest_missing_callback_param', $valid->get_error_code() );
    463463
    464464        $data = $valid->get_error_data( 'rest_missing_callback_param' );
     
    488488
    489489        $this->assertWPError( $valid );
    490         $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
     490        $this->assertSame( 'rest_invalid_param', $valid->get_error_code() );
    491491    }
    492492
     
    497497        $valid = $this->request->has_valid_params();
    498498        $this->assertWPError( $valid );
    499         $this->assertEquals( 'rest_invalid_json', $valid->get_error_code() );
     499        $this->assertSame( 'rest_invalid_json', $valid->get_error_code() );
    500500        $data = $valid->get_error_data();
    501         $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
     501        $this->assertSame( JSON_ERROR_SYNTAX, $data['json_error_code'] );
    502502    }
    503503
     
    535535
    536536        $this->assertWPError( $valid );
    537         $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
     537        $this->assertSame( 'rest_invalid_param', $valid->get_error_code() );
    538538
    539539        $data = $valid->get_error_data( 'rest_invalid_param' );
     
    568568        $error_data = $valid->get_error_data();
    569569
    570         $this->assertEquals( array( 'someinteger', 'someotherparams' ), array_keys( $error_data['params'] ) );
    571         $this->assertEquals( 'This is not valid!', $error_data['params']['someotherparams'] );
     570        $this->assertSame( array( 'someinteger', 'someotherparams' ), array_keys( $error_data['params'] ) );
     571        $this->assertSame( 'This is not valid!', $error_data['params']['someotherparams'] );
    572572    }
    573573
     
    595595        update_option( 'permalink_structure', $permalink_structure );
    596596        $url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) );
    597         $this->assertEquals( $original_url, $url );
     597        $this->assertSame( $original_url, $url );
    598598        $request = WP_REST_Request::from_url( $url );
    599599        $this->assertInstanceOf( 'WP_REST_Request', $request );
    600         $this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
     600        $this->assertSame( '/wp/v2/posts/1', $request->get_route() );
    601601        $this->assertEqualSets(
    602602            array(
     
    624624        $request = new WP_REST_Request();
    625625        $request->set_param( 'param', 'value' );
    626         $this->assertEquals( 'value', $request->get_param( 'param' ) );
     626        $this->assertSame( 'value', $request->get_param( 'param' ) );
    627627    }
    628628
     
    638638            )
    639639        );
    640         $this->assertEquals( 'value', $request->get_param( 'param' ) );
    641         $this->assertEquals(
     640        $this->assertSame( 'value', $request->get_param( 'param' ) );
     641        $this->assertSame(
    642642            array( 'param' => 'value' ),
    643643            $request->get_json_params()
     
    645645
    646646        $request->set_param( 'param', 'new_value' );
    647         $this->assertEquals( 'new_value', $request->get_param( 'param' ) );
    648         $this->assertEquals(
     647        $this->assertSame( 'new_value', $request->get_param( 'param' ) );
     648        $this->assertSame(
    649649            array( 'param' => 'new_value' ),
    650650            $request->get_json_params()
     
    673673        $request->set_param( 'param', 'new_value' );
    674674
    675         $this->assertEquals( 'new_value', $request->get_param( 'param' ) );
    676         $this->assertEquals( array(), $request->get_body_params() );
    677         $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() );
    678         $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() );
     675        $this->assertSame( 'new_value', $request->get_param( 'param' ) );
     676        $this->assertSame( array(), $request->get_body_params() );
     677        $this->assertSame( array( 'param' => 'new_value' ), $request->get_json_params() );
     678        $this->assertSame( array( 'param' => 'new_value' ), $request->get_query_params() );
    679679    }
    680680
     
    705705        $request->set_param( 'param_query', 'new_value' );
    706706
    707         $this->assertEquals( 'new_value', $request->get_param( 'param_query' ) );
    708         $this->assertEquals( array(), $request->get_body_params() );
    709         $this->assertEquals( array( 'param_body' => 'value_body' ), $request->get_json_params() );
    710         $this->assertEquals( array( 'param_query' => 'new_value' ), $request->get_query_params() );
     707        $this->assertSame( 'new_value', $request->get_param( 'param_query' ) );
     708        $this->assertSame( array(), $request->get_body_params() );
     709        $this->assertSame( array( 'param_body' => 'value_body' ), $request->get_json_params() );
     710        $this->assertSame( array( 'param_query' => 'new_value' ), $request->get_query_params() );
    711711        // Verify the default wasn't overwritten.
    712         $this->assertEquals( $original_defaults, $request->get_default_params() );
     712        $this->assertSame( $original_defaults, $request->get_default_params() );
    713713    }
    714714
     
    734734        $request->set_param( 'param', null );
    735735
    736         $this->assertEquals( null, $request->get_param( 'param' ) );
    737         $this->assertEquals( array(), $request->get_body_params() );
    738         $this->assertEquals( array( 'param' => null ), $request->get_json_params() );
    739         $this->assertEquals( array( 'param' => null ), $request->get_query_params() );
     736        $this->assertNull( $request->get_param( 'param' ) );
     737        $this->assertSame( array(), $request->get_body_params() );
     738        $this->assertSame( array( 'param' => null ), $request->get_json_params() );
     739        $this->assertSame( array( 'param' => null ), $request->get_query_params() );
    740740    }
    741741
     
    761761        $request->set_param( 'param', 'new_value' );
    762762
    763         $this->assertEquals( 'new_value', $request->get_param( 'param' ) );
    764         $this->assertEquals( array(), $request->get_body_params() );
    765         $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() );
    766         $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() );
     763        $this->assertSame( 'new_value', $request->get_param( 'param' ) );
     764        $this->assertSame( array(), $request->get_body_params() );
     765        $this->assertSame( array( 'param' => 'new_value' ), $request->get_json_params() );
     766        $this->assertSame( array( 'param' => 'new_value' ), $request->get_query_params() );
    767767    }
    768768
     
    778778
    779779        $this->assertTrue( $request->has_param( 'param' ) );
    780         $this->assertEquals( 'value', $request->get_param( 'param' ) );
     780        $this->assertSame( 'value', $request->get_param( 'param' ) );
    781781    }
    782782}
Note: See TracChangeset for help on using the changeset viewer.