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

    r48526 r48937  
    5050        // The envelope should still be a response, but with defaults.
    5151        $this->assertInstanceOf( 'WP_REST_Response', $envelope_response );
    52         $this->assertEquals( 200, $envelope_response->get_status() );
     52        $this->assertSame( 200, $envelope_response->get_status() );
    5353        $this->assertEmpty( $envelope_response->get_headers() );
    5454        $this->assertEmpty( $envelope_response->get_links() );
     
    5656        $enveloped = $envelope_response->get_data();
    5757
    58         $this->assertEquals( $data, $enveloped['body'] );
    59         $this->assertEquals( $status, $enveloped['status'] );
    60         $this->assertEquals( $headers, $enveloped['headers'] );
     58        $this->assertSame( $data, $enveloped['body'] );
     59        $this->assertSame( $status, $enveloped['status'] );
     60        $this->assertSame( $headers, $enveloped['headers'] );
    6161    }
    6262
     
    8181        $response = rest_get_server()->dispatch( $request );
    8282
    83         $this->assertEquals( 'bar', $request['foo'] );
     83        $this->assertSame( 'bar', $request['foo'] );
    8484    }
    8585
     
    126126        $response = rest_get_server()->dispatch( $request );
    127127        $this->assertInstanceOf( 'WP_REST_Response', $response );
    128         $this->assertEquals( 200, $response->get_status() );
     128        $this->assertSame( 200, $response->get_status() );
    129129        $this->assertArrayNotHasKey( 'foo', (array) $request );
    130130    }
     
    147147        $request = new WP_REST_Request( 'GET', '/no-zero/test' );
    148148        rest_get_server()->dispatch( $request );
    149         $this->assertEquals( array( 'foo' => 'bar' ), $request->get_params() );
     149        $this->assertSame( array( 'foo' => 'bar' ), $request->get_params() );
    150150    }
    151151
     
    162162        $request  = new WP_REST_Request( 'HEAD', '/head-request/test' );
    163163        $response = rest_get_server()->dispatch( $request );
    164         $this->assertEquals( 200, $response->get_status() );
     164        $this->assertSame( 200, $response->get_status() );
    165165    }
    166166
     
    190190        $request  = new WP_REST_Request( 'HEAD', '/head-request/test' );
    191191        $response = rest_get_server()->dispatch( $request );
    192         $this->assertEquals( 200, $response->get_status() );
     192        $this->assertSame( 200, $response->get_status() );
    193193    }
    194194
     
    212212        $request = new WP_REST_Request( 'GET', '/test/some-value' );
    213213        rest_get_server()->dispatch( $request );
    214         $this->assertEquals( array( 'data' => 'some-value' ), $request->get_params() );
     214        $this->assertSame( array( 'data' => 'some-value' ), $request->get_params() );
    215215    }
    216216
     
    234234        $result  = rest_get_server()->dispatch( $request );
    235235
    236         $this->assertEquals( 403, $result->get_status() );
     236        $this->assertSame( 403, $result->get_status() );
    237237    }
    238238
     
    261261        $result = rest_get_server()->dispatch( $request );
    262262
    263         $this->assertEquals( 200, $result->get_status() );
     263        $this->assertSame( 200, $result->get_status() );
    264264    }
    265265
     
    289289
    290290        $sent_headers = $result->get_headers();
    291         $this->assertEquals( $sent_headers['Allow'], 'GET' );
     291        $this->assertSame( $sent_headers['Allow'], 'GET' );
    292292    }
    293293
     
    329329
    330330        $sent_headers = $result->get_headers();
    331         $this->assertEquals( $sent_headers['Allow'], 'GET, POST' );
     331        $this->assertSame( $sent_headers['Allow'], 'GET, POST' );
    332332    }
    333333
     
    365365        $result = apply_filters( 'rest_post_dispatch', $result, rest_get_server(), $request );
    366366
    367         $this->assertEquals( $result->get_status(), 403 );
     367        $this->assertSame( $result->get_status(), 403 );
    368368
    369369        $sent_headers = $result->get_headers();
    370         $this->assertEquals( $sent_headers['Allow'], 'POST' );
     370        $this->assertSame( $sent_headers['Allow'], 'POST' );
    371371    }
    372372
     
    396396        $headers = $result->get_headers();
    397397
    398         $this->assertEquals( 'GET', $headers['Allow'] );
     398        $this->assertSame( 'GET', $headers['Allow'] );
    399399    }
    400400
     
    412412
    413413        // Make sure we default to a 500 error.
    414         $this->assertEquals( 500, $response->get_status() );
     414        $this->assertSame( 500, $response->get_status() );
    415415
    416416        $data = $response->get_data();
    417417
    418         $this->assertEquals( $code, $data['code'] );
    419         $this->assertEquals( $message, $data['message'] );
     418        $this->assertSame( $code, $data['code'] );
     419        $this->assertSame( $message, $data['message'] );
    420420    }
    421421
     
    428428        $this->assertInstanceOf( 'WP_REST_Response', $response );
    429429
    430         $this->assertEquals( 400, $response->get_status() );
     430        $this->assertSame( 400, $response->get_status() );
    431431
    432432        $data = $response->get_data();
    433433
    434         $this->assertEquals( $code, $data['code'] );
    435         $this->assertEquals( $message, $data['message'] );
     434        $this->assertSame( $code, $data['code'] );
     435        $this->assertSame( $message, $data['message'] );
    436436    }
    437437
     
    447447        $this->assertInstanceOf( 'WP_REST_Response', $response );
    448448
    449         $this->assertEquals( 400, $response->get_status() );
     449        $this->assertSame( 400, $response->get_status() );
    450450
    451451        $error = $response->as_error();
    452452        $this->assertInstanceOf( 'WP_Error', $error );
    453         $this->assertEquals( $code, $error->get_error_code() );
    454         $this->assertEquals( $message, $error->get_error_message() );
    455         $this->assertEquals( $message2, $error->errors[ $code2 ][0] );
    456         $this->assertEquals( array( 'status' => 403 ), $error->error_data[ $code2 ] );
     453        $this->assertSame( $code, $error->get_error_code() );
     454        $this->assertSame( $message, $error->get_error_message() );
     455        $this->assertSame( $message2, $error->errors[ $code2 ][0] );
     456        $this->assertSame( array( 'status' => 403 ), $error->error_data[ $code2 ] );
    457457    }
    458458
     
    465465        $response = rest_get_server()->json_error( 'wp-api-test-error', 'Message text' );
    466466
    467         $this->assertEquals( $expected, $response );
     467        $this->assertSame( $expected, $response );
    468468    }
    469469
     
    485485        $response = $stub->json_error( 'wp-api-test-error', 'Message text', 400 );
    486486
    487         $this->assertEquals( $expected, $response );
     487        $this->assertSame( $expected, $response );
    488488    }
    489489
     
    499499            'href' => 'http://example.com/',
    500500        );
    501         $this->assertEquals( $self, $data['_links']['self'][0] );
     501        $this->assertSame( $self, $data['_links']['self'][0] );
    502502
    503503        $alternate = array(
     504            'type' => 'application/xml',
    504505            'href' => 'http://example.org/',
    505             'type' => 'application/xml',
    506         );
    507         $this->assertEquals( $alternate, $data['_links']['alternate'][0] );
     506        );
     507        $this->assertSame( $alternate, $data['_links']['alternate'][0] );
    508508    }
    509509
     
    538538
    539539        // Ensure the context is set to embed when requesting.
    540         $this->assertEquals( 'embed', $alternate[1]['parameters']['context'] );
     540        $this->assertSame( 'embed', $alternate[1]['parameters']['context'] );
    541541    }
    542542
     
    653653        $data = $data['_embedded']['alternate'][0];
    654654
    655         $this->assertEquals( 'yes', $data['parameters']['parsed_params'] );
     655        $this->assertSame( 'yes', $data['parameters']['parsed_params'] );
    656656    }
    657657
     
    685685
    686686        $up_data = $up[0];
    687         $this->assertEquals( 'wp-api-test-error', $up_data['code'] );
    688         $this->assertEquals( 'Test message', $up_data['message'] );
    689         $this->assertEquals( 403, $up_data['data']['status'] );
     687        $this->assertSame( 'wp-api-test-error', $up_data['code'] );
     688        $this->assertSame( 'Test message', $up_data['message'] );
     689        $this->assertSame( 403, $up_data['data']['status'] );
    690690    }
    691691
     
    713713        $this->assertArrayHasKey( 'post', $data['_embedded'] );
    714714        $this->assertCount( 1, $data['_embedded']['post'] );
    715         $this->assertEquals( 'My Awesome Title', $data['_embedded']['post'][0]['title']['rendered'] );
     715        $this->assertSame( 'My Awesome Title', $data['_embedded']['post'][0]['title']['rendered'] );
    716716    }
    717717
     
    803803        $this->assertArrayNotHasKey( '_links', $data );
    804804        $this->assertArrayNotHasKey( '_embedded', $data );
    805         $this->assertEquals( 'data', $data['untouched'] );
     805        $this->assertSame( 'data', $data['untouched'] );
    806806    }
    807807
     
    834834
    835835        $alternate = array(
     836            'type' => 'application/xml',
    836837            'href' => 'http://example.org/',
    837             'type' => 'application/xml',
    838         );
    839         $this->assertEquals( $alternate, $data['_links']['alternate'][0] );
     838        );
     839        $this->assertSame( $alternate, $data['_links']['alternate'][0] );
    840840    }
    841841
     
    855855            'href' => 'http://example.com/',
    856856        );
    857         $this->assertEquals( $self_not_filtered, $data['_links']['self'][0] );
     857        $this->assertSame( $self_not_filtered, $data['_links']['self'][0] );
    858858    }
    859859
     
    955955        $this->assertArrayHasKey( '/test/example/some-route', $data['routes'] );
    956956        $route = $data['routes']['/test/example/some-route'];
    957         $this->assertEquals( 'test/example', $route['namespace'] );
     957        $this->assertSame( 'test/example', $route['namespace'] );
    958958        $this->assertArrayHasKey( 'methods', $route );
    959959        $this->assertContains( 'GET', $route['methods'] );
     
    995995
    996996        // Check top-level.
    997         $this->assertEquals( 'test/example', $data['namespace'] );
     997        $this->assertSame( 'test/example', $data['namespace'] );
    998998        $this->assertArrayHasKey( 'routes', $data );
    999999
     
    10531053        );
    10541054
    1055         $this->assertEquals( $expected, $actual['_links'] );
     1055        $this->assertSame( $expected, $actual['_links'] );
    10561056    }
    10571057
     
    10621062        $headers = rest_get_server()->sent_headers;
    10631063
    1064         $this->assertEquals( 'noindex', $headers['X-Robots-Tag'] );
     1064        $this->assertSame( 'noindex', $headers['X-Robots-Tag'] );
    10651065    }
    10661066
     
    10871087        $headers = rest_get_server()->sent_headers;
    10881088
    1089         $this->assertEquals( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] );
     1089        $this->assertSame( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] );
    10901090    }
    10911091
     
    11041104
    11051105            $this->assertTrue( isset( $headers[ $header ] ), sprintf( 'Header %s is not present in the response.', $header ) );
    1106             $this->assertEquals( $value, $headers[ $header ] );
     1106            $this->assertSame( $value, $headers[ $header ] );
    11071107        }
    11081108
     
    11411141        $result     = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
    11421142        $url_params = rest_get_server()->last_request->get_url_params();
    1143         $this->assertEquals( 'data\\with\\slashes', $url_params['data'] );
     1143        $this->assertSame( 'data\\with\\slashes', $url_params['data'] );
    11441144    }
    11451145
     
    11691169        $result       = rest_get_server()->serve_request( '/test' );
    11701170        $query_params = rest_get_server()->last_request->get_query_params();
    1171         $this->assertEquals( 'data\\with\\slashes', $query_params['data'] );
     1171        $this->assertSame( 'data\\with\\slashes', $query_params['data'] );
    11721172    }
    11731173
     
    11981198
    11991199        $body_params = rest_get_server()->last_request->get_body_params();
    1200         $this->assertEquals( 'data\\with\\slashes', $body_params['data'] );
     1200        $this->assertSame( 'data\\with\\slashes', $body_params['data'] );
    12011201    }
    12021202
     
    12261226        $result      = rest_get_server()->serve_request( '/test' );
    12271227        $json_params = rest_get_server()->last_request->get_json_params();
    1228         $this->assertEquals( 'data\\with\\slashes', $json_params['data'] );
     1228        $this->assertSame( 'data\\with\\slashes', $json_params['data'] );
    12291229    }
    12301230
     
    12541254        $result      = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
    12551255        $file_params = rest_get_server()->last_request->get_file_params();
    1256         $this->assertEquals( 'data\\with\\slashes', $file_params['data']['name'] );
     1256        $this->assertSame( 'data\\with\\slashes', $file_params['data']['name'] );
    12571257    }
    12581258
     
    12771277
    12781278        $result = rest_get_server()->serve_request( '/test/data\\with\\slashes' );
    1279         $this->assertEquals( 'data\\with\\slashes', rest_get_server()->last_request->get_header( 'x_my_header' ) );
     1279        $this->assertSame( 'data\\with\\slashes', rest_get_server()->last_request->get_header( 'x_my_header' ) );
    12801280    }
    12811281
     
    13651365        $response = rest_get_server()->dispatch( $request );
    13661366
    1367         $this->assertEquals( 200, $response->get_status() );
     1367        $this->assertSame( 200, $response->get_status() );
    13681368    }
    13691369
     
    13871387
    13881388        $this->assertNull( $result );
    1389         $this->assertEquals( '', rest_get_server()->sent_body );
     1389        $this->assertSame( '', rest_get_server()->sent_body );
    13901390    }
    13911391
     
    14091409
    14101410        $this->assertNull( $result );
    1411         $this->assertEquals( '', rest_get_server()->sent_body );
     1411        $this->assertSame( '', rest_get_server()->sent_body );
    14121412    }
    14131413
     
    15111511        $response = rest_get_server()->dispatch( $request );
    15121512
    1513         $this->assertEquals( 204, $response->get_status(), '/test-ns/v1/test' );
     1513        $this->assertSame( 204, $response->get_status(), '/test-ns/v1/test' );
    15141514    }
    15151515
Note: See TracChangeset for help on using the changeset viewer.