WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/09/2020 03:43:14 PM (13 months ago)
Author:
SergeyBiryukov
Message:

Tests: Use assertSame() in some newly introduced 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.

Follow-up to [48937], [48939], [48940], [48944].

See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r49257 r49547  
    15511551        $this->assertCount( 1, $events );
    15521552        $this->assertWPError( $events[0]['args'][0] );
    1553         $this->assertEquals( 'rest_invalid_handler', $events[0]['args'][0]->get_error_code() );
     1553        $this->assertSame( 'rest_invalid_handler', $events[0]['args'][0]->get_error_code() );
    15541554    }
    15551555
     
    16151615        $this->assertCount( 1, $events );
    16161616        $this->assertWPError( $events[0]['args'][0] );
    1617         $this->assertEquals( 'rest_invalid_param', $events[0]['args'][0]->get_error_code() );
     1617        $this->assertSame( 'rest_invalid_param', $events[0]['args'][0]->get_error_code() );
    16181618    }
    16191619
     
    16541654        $response = rest_do_request( $request );
    16551655
    1656         $this->assertEquals( 207, $response->get_status() );
     1656        $this->assertSame( 207, $response->get_status() );
    16571657
    16581658        if ( $allowed ) {
    1659             $this->assertEquals( 'data', $response->get_data()['responses'][0]['body'] );
     1659            $this->assertSame( 'data', $response->get_data()['responses'][0]['body'] );
    16601660        } else {
    1661             $this->assertEquals( 'rest_batch_not_allowed', $response->get_data()['responses'][0]['body']['code'] );
     1661            $this->assertSame( 'rest_batch_not_allowed', $response->get_data()['responses'][0]['body']['code'] );
    16621662        }
    16631663    }
     
    17241724        $data     = $response->get_data();
    17251725
    1726         $this->assertEquals( 207, $response->get_status() );
     1726        $this->assertSame( 207, $response->get_status() );
    17271727        $this->assertArrayHasKey( 'failed', $data );
    1728         $this->assertEquals( 'validation', $data['failed'] );
     1728        $this->assertSame( 'validation', $data['failed'] );
    17291729        $this->assertCount( 2, $data['responses'] );
    17301730        $this->assertNull( $data['responses'][0] );
    1731         $this->assertEquals( 400, $data['responses'][1]['status'] );
     1731        $this->assertSame( 400, $data['responses'][1]['status'] );
    17321732        $this->assertFalse( get_option( 'test_project' ) );
    17331733    }
     
    17801780        $data     = $response->get_data();
    17811781
    1782         $this->assertEquals( 207, $response->get_status() );
     1782        $this->assertSame( 207, $response->get_status() );
    17831783        $this->assertArrayNotHasKey( 'failed', $data );
    17841784        $this->assertCount( 2, $data['responses'] );
    1785         $this->assertEquals( 'gutenberg', $data['responses'][0]['body'] );
    1786         $this->assertEquals( 'WordPress', $data['responses'][1]['body'] );
     1785        $this->assertSame( 'gutenberg', $data['responses'][0]['body'] );
     1786        $this->assertSame( 'WordPress', $data['responses'][1]['body'] );
    17871787    }
    17881788
     
    17971797                'methods'             => array( 'POST', 'DELETE' ),
    17981798                'callback'            => function ( WP_REST_Request $request ) {
    1799                     $this->assertEquals( 'DELETE', $request->get_method() );
    1800                     $this->assertEquals( '/test-ns/v1/test/5', $request->get_route() );
    1801                     $this->assertEquals( array( 'id' => '5' ), $request->get_url_params() );
    1802                     $this->assertEquals( array( 'query' => 'param' ), $request->get_query_params() );
    1803                     $this->assertEquals( array( 'project' => 'gutenberg' ), $request->get_body_params() );
    1804                     $this->assertEquals( array( 'my_header' => array( 'my-value' ) ), $request->get_headers() );
     1799                    $this->assertSame( 'DELETE', $request->get_method() );
     1800                    $this->assertSame( '/test-ns/v1/test/5', $request->get_route() );
     1801                    $this->assertSame( array( 'id' => '5' ), $request->get_url_params() );
     1802                    $this->assertSame( array( 'query' => 'param' ), $request->get_query_params() );
     1803                    $this->assertSame( array( 'project' => 'gutenberg' ), $request->get_body_params() );
     1804                    $this->assertSame( array( 'my_header' => array( 'my-value' ) ), $request->get_headers() );
    18051805
    18061806                    return new WP_REST_Response( 'test' );
     
    18311831        $response = rest_get_server()->dispatch( $request );
    18321832
    1833         $this->assertEquals( 207, $response->get_status() );
    1834         $this->assertEquals( 'test', $response->get_data()['responses'][0]['body'] );
     1833        $this->assertSame( 207, $response->get_status() );
     1834        $this->assertSame( 'test', $response->get_data()['responses'][0]['body'] );
    18351835    }
    18361836
     
    18841884        $data     = $response->get_data();
    18851885
    1886         $this->assertEquals( 207, $response->get_status() );
     1886        $this->assertSame( 207, $response->get_status() );
    18871887        $this->assertArrayNotHasKey( 'failed', $data );
    18881888        $this->assertCount( 2, $data['responses'] );
    1889         $this->assertEquals( 'gutenberg', $data['responses'][0]['body'] );
    1890         $this->assertEquals( 400, $data['responses'][1]['status'] );
    1891         $this->assertEquals( 'gutenberg', get_option( 'test_project' ) );
     1889        $this->assertSame( 'gutenberg', $data['responses'][0]['body'] );
     1890        $this->assertSame( 400, $data['responses'][1]['status'] );
     1891        $this->assertSame( 'gutenberg', get_option( 'test_project' ) );
    18921892    }
    18931893
     
    19291929
    19301930        $response = rest_get_server()->dispatch( $request );
    1931         $this->assertEquals( 400, $response->get_status() );
     1931        $this->assertSame( 400, $response->get_status() );
    19321932    }
    19331933
Note: See TracChangeset for help on using the changeset viewer.