From b2b29c66aadcd26a958dee9716a5aab39aa13e20 Mon Sep 17 00:00:00 2001 From: Shreyas Ikhar Date: Tue, 5 May 2026 11:27:15 +0530 Subject: [PATCH] Replace assertEquals with assertSame for stricter checks --- tests/phpunit/tests/admin/includesPost.php | 2 +- .../rest-api/rest-attachments-controller.php | 2 +- .../rest-global-styles-controller.php | 2 +- .../wpRestAbilitiesV1CategoriesController.php | 24 +++++----- .../wpRestAbilitiesV1ListController.php | 32 +++++++------- .../wpRestAbilitiesV1RunController.php | 44 +++++++++---------- tests/phpunit/tests/theme-previews.php | 8 ++-- 7 files changed, 57 insertions(+), 57 deletions(-) diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index d9d39d8da727d..a10c6d830fd01 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -253,7 +253,7 @@ public function test_bulk_edit_posts_stomping() { // Check that the first post's values don't stomp the second post. $this->assertSame( 'draft', $post->post_status ); - $this->assertEquals( self::$author_ids[1], $post->post_author ); + $this->assertSame( self::$author_ids[1], (int) $post->post_author ); $this->assertSame( 'closed', $post->comment_status ); $this->assertSame( 'closed', $post->ping_status ); } diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index c8746931ed30a..9fed2e5ac0515 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -3028,7 +3028,7 @@ public function test_edit_image_updates_attachment_fields() { $response = rest_do_request( $request ); // The edit endpoint creates a new attachment, so we expect a 201 status. - $this->assertEquals( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status() ); $data = $response->get_data(); $new_attachment_id = $data['id']; diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php index 08ed7f65f818b..068ff716c385f 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -788,7 +788,7 @@ public function test_global_styles_route_accepts_integer_id() { $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertIsInt( $data['id'] ); diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php index 43525263ac5ba..f5a394ebfa619 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php @@ -145,7 +145,7 @@ public function test_get_items(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertIsArray( $data ); @@ -168,7 +168,7 @@ public function test_get_item(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories/test-data-retrieval' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'test-data-retrieval', $data['slug'] ); @@ -186,7 +186,7 @@ public function test_get_item_with_meta(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories/test-communication' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'test-communication', $data['slug'] ); @@ -208,7 +208,7 @@ public function test_get_item_with_selected_fields(): void { $response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request ); remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 2, $data, 'Response should only contain the requested fields.' ); @@ -227,7 +227,7 @@ public function test_get_item_not_found(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories/non-existent' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_category_not_found', $data['code'] ); @@ -244,7 +244,7 @@ public function test_get_items_permission_denied(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); + $this->assertSame( 401, $response->get_status() ); } /** @@ -258,7 +258,7 @@ public function test_get_item_permission_denied(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories/test-data-retrieval' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); + $this->assertSame( 401, $response->get_status() ); } /** @@ -271,15 +271,15 @@ public function test_pagination_headers(): void { $request->set_param( 'per_page', 10 ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $headers = $response->get_headers(); $this->assertArrayHasKey( 'X-WP-Total', $headers ); $this->assertArrayHasKey( 'X-WP-TotalPages', $headers ); $total_categories = count( wp_get_ability_categories() ); - $this->assertEquals( $total_categories, (int) $headers['X-WP-Total'] ); - $this->assertEquals( ceil( $total_categories / 10 ), (int) $headers['X-WP-TotalPages'] ); + $this->assertSame( $total_categories, (int) $headers['X-WP-Total'] ); + $this->assertSame( (int) ceil( $total_categories / 10 ), (int) $headers['X-WP-TotalPages'] ); } /** @@ -352,7 +352,7 @@ public function test_collection_params(): void { $request->set_param( 'page', 2 ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 5, $data ); @@ -450,7 +450,7 @@ public function test_get_schema(): void { public function test_ability_category_slug_with_valid_format(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/categories/test-data-retrieval' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); } /** diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php index d73a2c64177fc..68f46862e98ea 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php @@ -280,7 +280,7 @@ public function test_get_items(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertIsArray( $data ); @@ -303,7 +303,7 @@ public function test_get_item(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/calculator' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 7, $data, 'Response should contain all fields.' ); @@ -330,7 +330,7 @@ public function test_get_item_with_selected_fields(): void { $response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request ); remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 2, $data, 'Response should only contain the requested fields.' ); @@ -351,7 +351,7 @@ public function test_get_item_with_embed_context(): void { $response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request ); remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 3, $data, 'Response should only contain the fields for embed context.' ); @@ -371,7 +371,7 @@ public function test_get_item_not_found(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/non/existent' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_not_found', $data['code'] ); @@ -386,7 +386,7 @@ public function test_get_item_not_show_in_rest(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/not-show-in-rest' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_not_found', $data['code'] ); @@ -404,7 +404,7 @@ public function test_get_items_permission_denied(): void { $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 401, $response->get_status() ); + $this->assertSame( 401, $response->get_status() ); } /** @@ -417,15 +417,15 @@ public function test_pagination_headers(): void { $request->set_param( 'per_page', 10 ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $headers = $response->get_headers(); $this->assertArrayHasKey( 'X-WP-Total', $headers ); $this->assertArrayHasKey( 'X-WP-TotalPages', $headers ); $total_abilities = count( wp_get_abilities() ) - 1; // Exclude the one that doesn't show in REST. - $this->assertEquals( $total_abilities, (int) $headers['X-WP-Total'] ); - $this->assertEquals( ceil( $total_abilities / 10 ), (int) $headers['X-WP-TotalPages'] ); + $this->assertSame( $total_abilities, (int) $headers['X-WP-Total'] ); + $this->assertSame( (int) ceil( $total_abilities / 10 ), (int) $headers['X-WP-TotalPages'] ); } /** @@ -507,7 +507,7 @@ public function test_collection_params(): void { $request->set_param( 'page', 2 ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertCount( 5, $data ); @@ -629,7 +629,7 @@ public function test_ability_name_with_valid_special_characters(): void { wp_unregister_ability( 'test-hyphen/ability' ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); } /** @@ -663,7 +663,7 @@ public function test_ability_name_with_invalid_special_characters( string $name $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/' . $name ); $response = $this->server->dispatch( $request ); // Should return 404 as the regex pattern won't match - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); } /** @@ -681,7 +681,7 @@ public function test_extremely_long_ability_names(): void { $response = $this->server->dispatch( $request ); // Should return 404 as ability doesn't exist - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); } /** @@ -738,7 +738,7 @@ public function test_filter_by_category(): void { $request->set_param( 'category', 'math' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertIsArray( $data ); @@ -770,7 +770,7 @@ public function test_filter_by_nonexistent_category(): void { $request->set_param( 'category', 'nonexistent' ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertIsArray( $data ); diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php index c8a1c802091c5..2425e65587a6a 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php @@ -428,7 +428,7 @@ public function test_execute_regular_ability_post(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $this->assertEquals( 8, $response->get_data() ); } @@ -449,7 +449,7 @@ public function test_execute_readonly_ability_get(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( self::$user_id, $data['id'] ); } @@ -471,7 +471,7 @@ public function test_execute_destructive_ability_delete(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $this->assertSame( 'User successfully deleted!', $response->get_data() ); } @@ -615,7 +615,7 @@ public function test_contextual_permission_check(): void { ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 403, $response->get_status() ); + $this->assertSame( 403, $response->get_status() ); $request->set_body( wp_json_encode( @@ -629,7 +629,7 @@ public function test_contextual_permission_check(): void { ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $this->assertSame( 'Success: test data', $response->get_data() ); } @@ -644,7 +644,7 @@ public function test_do_not_show_in_rest(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_not_found', $data['code'] ); $this->assertSame( 'Ability not found.', $data['message'] ); @@ -661,7 +661,7 @@ public function test_null_return_handling(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNull( $data ); } @@ -677,7 +677,7 @@ public function test_wp_error_return_handling(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 500, $response->get_status() ); + $this->assertSame( 500, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'test_error', $data['code'] ); $this->assertSame( 'This is a test error', $data['message'] ); @@ -696,7 +696,7 @@ public function test_execute_non_existent_ability(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 404, $response->get_status() ); + $this->assertSame( 404, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_not_found', $data['code'] ); } @@ -734,7 +734,7 @@ public function test_invalid_json_in_post_body(): void { $response = $this->server->dispatch( $request ); // When JSON is invalid, WordPress returns 400 Bad Request - $this->assertEquals( 400, $response->get_status() ); + $this->assertSame( 400, $response->get_status() ); } /** @@ -758,7 +758,7 @@ public function test_get_request_with_nested_input_array(): void { ); $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'nested', $data['level1']['level2']['value'] ); @@ -780,7 +780,7 @@ public function test_get_request_with_non_array_input(): void { $response = $this->server->dispatch( $request ); // When input is not an array, WordPress returns 400 Bad Request - $this->assertEquals( 400, $response->get_status() ); + $this->assertSame( 400, $response->get_status() ); } /** @@ -801,7 +801,7 @@ public function test_post_request_with_non_array_input(): void { $response = $this->server->dispatch( $request ); // When input is not an array, WordPress returns 400 Bad Request - $this->assertEquals( 400, $response->get_status() ); + $this->assertSame( 400, $response->get_status() ); } /** @@ -928,14 +928,14 @@ public function test_ability_without_annotations_defaults_to_post_method(): void // Should require POST (default behavior). $get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/no-annotations/run' ); $get_response = $this->server->dispatch( $get_request ); - $this->assertEquals( 405, $get_response->get_status() ); + $this->assertSame( 405, $get_response->get_status() ); // Should work with POST. $post_request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/no-annotations/run' ); $post_request->set_header( 'Content-Type', 'application/json' ); $post_response = $this->server->dispatch( $post_request ); - $this->assertEquals( 200, $post_response->get_status() ); + $this->assertSame( 200, $post_response->get_status() ); } /** @@ -966,7 +966,7 @@ public function test_empty_input_handling_get_method(): void { // Tests GET with no input parameter. $get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/read-only-empty/run' ); $get_response = $this->server->dispatch( $get_request ); - $this->assertEquals( 200, $get_response->get_status() ); + $this->assertSame( 200, $get_response->get_status() ); $this->assertTrue( $get_response->get_data()['input_was_empty'] ); } @@ -1002,7 +1002,7 @@ public function test_empty_input_handling_get_method_with_normalized_input(): vo // Tests GET with no input parameter. $get_request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/test/read-only-empty-array/run' ); $get_response = $this->server->dispatch( $get_request ); - $this->assertEquals( 200, $get_response->get_status() ); + $this->assertSame( 200, $get_response->get_status() ); $this->assertTrue( $get_response->get_data() ); } @@ -1034,7 +1034,7 @@ public function test_empty_input_handling_post_method(): void { $post_request->set_body( '{}' ); // Empty JSON object $post_response = $this->server->dispatch( $post_request ); - $this->assertEquals( 200, $post_response->get_status() ); + $this->assertSame( 200, $post_response->get_status() ); $this->assertTrue( $post_response->get_data()['input_was_empty'] ); } @@ -1073,7 +1073,7 @@ public function test_malformed_json_post_body( string $json ): void { $response = $this->server->dispatch( $request ); // Malformed JSON should result in 400 Bad Request - $this->assertEquals( 400, $response->get_status() ); + $this->assertSame( 400, $response->get_status() ); } /** @@ -1120,7 +1120,7 @@ public function test_php_type_strings_in_input(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertEquals( $inputs, $data['echo'] ); } @@ -1167,7 +1167,7 @@ public function test_mixed_encoding_in_input(): void { $response = $this->server->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); // Input should be preserved exactly @@ -1236,6 +1236,6 @@ public function test_options_method_handling(): void { $request = new WP_REST_Request( 'OPTIONS', '/wp-abilities/v1/abilities/test/calculator/run' ); $response = $this->server->dispatch( $request ); // OPTIONS requests return 200 with allowed methods - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); } } diff --git a/tests/phpunit/tests/theme-previews.php b/tests/phpunit/tests/theme-previews.php index 22c9a8bbd2eb6..ab730b4275243 100644 --- a/tests/phpunit/tests/theme-previews.php +++ b/tests/phpunit/tests/theme-previews.php @@ -19,9 +19,9 @@ public function test_initialize_theme_preview_hooks() { $_GET['wp_theme_preview'] = 'twentytwentythree'; do_action( 'plugins_loaded' ); // Ensure `plugins_loaded` triggers `wp_initialize_theme_preview_hooks`. - $this->assertEquals( has_filter( 'stylesheet', 'wp_get_theme_preview_path' ), 10 ); - $this->assertEquals( has_filter( 'template', 'wp_get_theme_preview_path' ), 10 ); - $this->assertEquals( has_action( 'init', 'wp_attach_theme_preview_middleware' ), 10 ); - $this->assertEquals( has_action( 'admin_head', 'wp_block_theme_activate_nonce' ), 10 ); + $this->assertSame( 10, has_filter( 'stylesheet', 'wp_get_theme_preview_path' ) ); + $this->assertSame( 10, has_filter( 'template', 'wp_get_theme_preview_path' ) ); + $this->assertSame( 10, has_action( 'init', 'wp_attach_theme_preview_middleware' ) ); + $this->assertSame( 10, has_action( 'admin_head', 'wp_block_theme_activate_nonce' ) ); } }