From 2cfac4648294b4ebdf90cc6b2188af3239aaa9ef Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 4 Jun 2025 19:54:16 +0000 Subject: [PATCH] Add tests for fromJson response handling Added two new test cases to JsonRpcMessageTest.php: - testFromJsonWithResultField: Ensures fromJson correctly parses a JSON-RPC response containing a 'result' field. - testFromJsonWithErrorField: Ensures fromJson correctly parses a JSON-RPC response containing an 'error' field. These tests specifically cover the conditional branch in JsonRpcMessage::fromJson that handles messages with either 'result' or 'error' keys, improving code coverage for response message parsing. --- tests/Message/JsonRpcMessageTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Message/JsonRpcMessageTest.php b/tests/Message/JsonRpcMessageTest.php index 13213a5..744acc8 100644 --- a/tests/Message/JsonRpcMessageTest.php +++ b/tests/Message/JsonRpcMessageTest.php @@ -335,4 +335,28 @@ public function testToResponseArrayForNotificationThrowsException(): void $this->expectExceptionMessage('Message is not a response or error, cannot convert to response array.'); $message->toResponseArray(); } + + public function testFromJsonWithResultField(): void + { + $json = '{"jsonrpc":"2.0","result":{"data":"success"},"id":"res-123"}'; + $message = JsonRpcMessage::fromJson($json); + + $this->assertSame('res-123', $message->id); + $this->assertSame(['data' => 'success'], $message->result); + $this->assertSame('', $message->method); + $this->assertNull($message->params); + $this->assertNull($message->error); + } + + public function testFromJsonWithErrorField(): void + { + $json = '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":"err-456"}'; + $message = JsonRpcMessage::fromJson($json); + + $this->assertSame('err-456', $message->id); + $this->assertSame(['code' => -32600, 'message' => 'Invalid Request'], $message->error); + $this->assertSame('', $message->method); + $this->assertNull($message->params); + $this->assertNull($message->result); + } }