From 625449e8696c6692698352b4ea606f634db9775d Mon Sep 17 00:00:00 2001 From: Paul Kang Date: Fri, 13 Jun 2025 10:48:39 -0700 Subject: [PATCH 1/2] Add comprehensive unit tests for User Response class - Test class existence and instantiation - Test inheritance from ApiResponse - Test documented properties (id and name) - Test edge cases with missing properties, null values, and special characters - Test inherited methods like get_id() and ArrayAccess interface - Test additional undocumented properties access These tests improve code coverage for the previously untested User Response class. --- tests/Unit/Api/User/ResponseTest.php | 259 +++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 tests/Unit/Api/User/ResponseTest.php diff --git a/tests/Unit/Api/User/ResponseTest.php b/tests/Unit/Api/User/ResponseTest.php new file mode 100644 index 000000000..4d713f4ec --- /dev/null +++ b/tests/Unit/Api/User/ResponseTest.php @@ -0,0 +1,259 @@ +assertTrue( class_exists( Response::class ) ); + } + + /** + * Test that Response extends ApiResponse. + */ + public function test_extends_api_response() { + $response = new Response( '{}' ); + $this->assertInstanceOf( ApiResponse::class, $response ); + } + + /** + * Test instantiation with user data. + */ + public function test_instantiation_with_user_data() { + $data = json_encode( [ 'id' => '123456789', 'name' => 'John Doe' ] ); + $response = new Response( $data ); + + $this->assertInstanceOf( Response::class, $response ); + $this->assertEquals( $data, $response->to_string() ); + } + + /** + * Test accessing id property. + */ + public function test_id_property_access() { + $user_id = '987654321'; + $data = json_encode( [ 'id' => $user_id ] ); + $response = new Response( $data ); + + $this->assertEquals( $user_id, $response->id ); + } + + /** + * Test accessing name property. + */ + public function test_name_property_access() { + $user_name = 'Jane Smith'; + $data = json_encode( [ 'name' => $user_name ] ); + $response = new Response( $data ); + + $this->assertEquals( $user_name, $response->name ); + } + + /** + * Test accessing both id and name properties. + */ + public function test_id_and_name_properties() { + $user_id = '1234567890'; + $user_name = 'Test User'; + $data = json_encode( [ 'id' => $user_id, 'name' => $user_name ] ); + $response = new Response( $data ); + + $this->assertEquals( $user_id, $response->id ); + $this->assertEquals( $user_name, $response->name ); + } + + /** + * Test with missing id property. + */ + public function test_missing_id_property() { + $data = json_encode( [ 'name' => 'User Without ID' ] ); + $response = new Response( $data ); + + $this->assertNull( $response->id ); + $this->assertEquals( 'User Without ID', $response->name ); + } + + /** + * Test with missing name property. + */ + public function test_missing_name_property() { + $data = json_encode( [ 'id' => '999' ] ); + $response = new Response( $data ); + + $this->assertEquals( '999', $response->id ); + $this->assertNull( $response->name ); + } + + /** + * Test with empty object. + */ + public function test_empty_object() { + $response = new Response( '{}' ); + + $this->assertNull( $response->id ); + $this->assertNull( $response->name ); + } + + /** + * Test with additional properties. + */ + public function test_additional_properties() { + $data = json_encode( [ + 'id' => '123', + 'name' => 'Test User', + 'email' => 'test@example.com', + 'picture' => 'https://example.com/pic.jpg' + ] ); + $response = new Response( $data ); + + // Documented properties should work + $this->assertEquals( '123', $response->id ); + $this->assertEquals( 'Test User', $response->name ); + + // Additional properties should also be accessible via magic getter + $this->assertEquals( 'test@example.com', $response->email ); + $this->assertEquals( 'https://example.com/pic.jpg', $response->picture ); + } + + /** + * Test with special characters in name. + */ + public function test_special_characters_in_name() { + $special_name = "O'Brien & Co. \"Quotes\" 'Apostrophes'"; + $data = json_encode( [ 'id' => '456', 'name' => $special_name ] ); + $response = new Response( $data ); + + $this->assertEquals( $special_name, $response->name ); + } + + /** + * Test with Unicode characters in name. + */ + public function test_unicode_characters_in_name() { + $unicode_name = '李明 (Li Ming) 🌟 émojis'; + $data = json_encode( [ 'id' => '789', 'name' => $unicode_name ] ); + $response = new Response( $data ); + + $this->assertEquals( $unicode_name, $response->name ); + } + + /** + * Test with numeric id as integer. + */ + public function test_numeric_id_as_integer() { + $data = json_encode( [ 'id' => 12345, 'name' => 'Numeric ID User' ] ); + $response = new Response( $data ); + + // Should be accessible even if stored as integer + $this->assertEquals( 12345, $response->id ); + } + + /** + * Test with very long id. + */ + public function test_very_long_id() { + $long_id = '1234567890123456789012345678901234567890'; + $data = json_encode( [ 'id' => $long_id, 'name' => 'Long ID User' ] ); + $response = new Response( $data ); + + $this->assertEquals( $long_id, $response->id ); + } + + /** + * Test with null values. + */ + public function test_null_values() { + $data = json_encode( [ 'id' => null, 'name' => null ] ); + $response = new Response( $data ); + + $this->assertNull( $response->id ); + $this->assertNull( $response->name ); + } + + /** + * Test ArrayAccess interface. + */ + public function test_array_access_interface() { + $data = json_encode( [ 'id' => '111', 'name' => 'Array Access User' ] ); + $response = new Response( $data ); + + // Test array access + $this->assertEquals( '111', $response['id'] ); + $this->assertEquals( 'Array Access User', $response['name'] ); + + // Test isset + $this->assertTrue( isset( $response['id'] ) ); + $this->assertTrue( isset( $response['name'] ) ); + $this->assertFalse( isset( $response['nonexistent'] ) ); + } + + /** + * Test inherited get_id method. + */ + public function test_get_id_method() { + $user_id = '555666777'; + $data = json_encode( [ 'id' => $user_id ] ); + $response = new Response( $data ); + + $this->assertEquals( $user_id, $response->get_id() ); + } + + /** + * Test get_id method with missing id. + */ + public function test_get_id_method_missing_id() { + $data = json_encode( [ 'name' => 'No ID User' ] ); + $response = new Response( $data ); + + $this->assertNull( $response->get_id() ); + } + + /** + * Test that the class has no additional public methods. + */ + public function test_no_additional_public_methods() { + $reflection = new \ReflectionClass( Response::class ); + $public_methods = $reflection->getMethods( \ReflectionMethod::IS_PUBLIC ); + + // Filter out inherited methods + $own_methods = array_filter( $public_methods, function( $method ) { + return $method->getDeclaringClass()->getName() === Response::class; + } ); + + // Should have no methods of its own (empty class extending ApiResponse) + $this->assertCount( 0, $own_methods ); + } + + /** + * Test response with nested user data. + */ + public function test_nested_user_data() { + $data = json_encode( [ + 'id' => '999', + 'name' => 'Nested User', + 'location' => [ + 'city' => 'San Francisco', + 'country' => 'USA' + ] + ] ); + $response = new Response( $data ); + + $this->assertEquals( '999', $response->id ); + $this->assertEquals( 'Nested User', $response->name ); + $this->assertIsArray( $response->location ); + $this->assertEquals( 'San Francisco', $response->location['city'] ); + } +} \ No newline at end of file From 963f2501690cef064727384fa87d9df4a5825444 Mon Sep 17 00:00:00 2001 From: Paul Kang Date: Mon, 16 Jun 2025 10:00:38 -0700 Subject: [PATCH 2/2] Change filename --- tests/Unit/Api/User/{ResponseTest.php => UserResponseTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Unit/Api/User/{ResponseTest.php => UserResponseTest.php} (98%) diff --git a/tests/Unit/Api/User/ResponseTest.php b/tests/Unit/Api/User/UserResponseTest.php similarity index 98% rename from tests/Unit/Api/User/ResponseTest.php rename to tests/Unit/Api/User/UserResponseTest.php index 4d713f4ec..c8a6ab0cf 100644 --- a/tests/Unit/Api/User/ResponseTest.php +++ b/tests/Unit/Api/User/UserResponseTest.php @@ -12,7 +12,7 @@ * * @since 3.5.2 */ -class ResponseTest extends AbstractWPUnitTestWithOptionIsolationAndSafeFiltering { +class UserResponseTest extends AbstractWPUnitTestWithOptionIsolationAndSafeFiltering { /** * Test that the class exists and can be instantiated.