Skip to content

Commit

Permalink
Tests: Use assertSame() in WP_REST_Navigation_Fallback_Controller
Browse files Browse the repository at this point in the history
… 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 [56052].

See #59655.

git-svn-id: https://develop.svn.wordpress.org/trunk@57656 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Feb 19, 2024
1 parent 8ee7651 commit b05838f
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public function test_should_not_return_menus_for_users_without_permissions() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 403, $response->get_status(), 'Response should indicate user does not have permission.' );
$this->assertSame( 403, $response->get_status(), 'Response should indicate user does not have permission.' );

$this->assertEquals( 'rest_cannot_create', $data['code'], 'Response should indicate user cannot create.' );
$this->assertSame( 'rest_cannot_create', $data['code'], 'Response should indicate user cannot create.' );

$this->assertEquals( 'Sorry, you are not allowed to create Navigation Menus as this user.', $data['message'], 'Response should indicate failed request status.' );
$this->assertSame( 'Sorry, you are not allowed to create Navigation Menus as this user.', $data['message'], 'Response should indicate failed request status.' );
}

/**
Expand All @@ -80,13 +80,13 @@ public function test_get_item() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status(), 'Status should indicate successful request.' );
$this->assertSame( 200, $response->get_status(), 'Status should indicate successful request.' );

$this->assertIsArray( $data, 'Response should be of correct type.' );

$this->assertArrayHasKey( 'id', $data, 'Response should contain expected fields.' );

$this->assertEquals( 'wp_navigation', get_post_type( $data['id'] ), '"id" field should represent a post of type "wp_navigation"' );
$this->assertSame( 'wp_navigation', get_post_type( $data['id'] ), '"id" field should represent a post of type "wp_navigation"' );

// Check that only a single Navigation fallback was created.
$navs_in_db = $this->get_navigations_in_database();
Expand All @@ -105,16 +105,16 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status(), 'Status should indicate successful request.' );
$this->assertSame( 200, $response->get_status(), 'Status should indicate successful request.' );

$this->assertArrayHasKey( 'schema', $data, '"schema" key should exist in response.' );

$schema = $data['schema'];

$this->assertEquals( 'object', $schema['type'], 'The schema type should match the expected type.' );
$this->assertSame( 'object', $schema['type'], 'The schema type should match the expected type.' );

$this->assertArrayHasKey( 'id', $schema['properties'], 'Schema should have an "id" property.' );
$this->assertEquals( 'integer', $schema['properties']['id']['type'], 'Schema "id" property should be an integer.' );
$this->assertSame( 'integer', $schema['properties']['id']['type'], 'Schema "id" property should be an integer.' );
$this->assertTrue( $schema['properties']['id']['readonly'], 'Schema "id" property should be readonly.' );
}

Expand Down

0 comments on commit b05838f

Please sign in to comment.