From 5626a3cb48f014a39fd27cf213230bf36edcc23e 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 20:28:30 +0000 Subject: [PATCH] I've improved the code coverage for the `JsonRpcMessage` class. I added specific test cases to ensure full coverage of branches within the `fromJson` and `jsonSerialize` methods. The following branches were targeted: fromJson: - `!array_key_exists('id', $data)`: I ensured that responses or errors missing an 'id' field are handled. I added `testFromJsonResponseMissingId` and `testFromJsonErrorResponseMissingId`. - `!is_array($data['error'])`: I ensured that error messages with a non-array 'error' field are handled. I added `testFromJsonErrorFieldNotAnArray`. jsonSerialize: - `$this->id !== null` (for requests/notifications): I ensured that the 'id' field is correctly serialized for messages that are requests (i.e., have an ID and are not errors/results). I added `testJsonSerializeRequestWithId` for explicit coverage, complementing existing indirect coverage. All new and existing tests pass, and linters are clean. --- tests/Message/JsonRpcMessageTest.php | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/Message/JsonRpcMessageTest.php b/tests/Message/JsonRpcMessageTest.php index 744acc8..98a3d0c 100644 --- a/tests/Message/JsonRpcMessageTest.php +++ b/tests/Message/JsonRpcMessageTest.php @@ -359,4 +359,50 @@ public function testFromJsonWithErrorField(): void $this->assertNull($message->params); $this->assertNull($message->result); } + + public function testFromJsonResponseMissingId(): void + { + // This JSON represents a result response but is missing the 'id' field. + $json = '{"jsonrpc": "2.0", "result": {"foo": "bar"}}'; + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST); + $this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)'); + JsonRpcMessage::fromJson($json); + } + + public function testFromJsonErrorResponseMissingId(): void + { + // This JSON represents an error response but is missing the 'id' field. + $json = '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}'; + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST); + $this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)'); + JsonRpcMessage::fromJson($json); + } + + public function testFromJsonErrorFieldNotAnArray(): void + { + // This JSON represents an error response, but the 'error' field is a string, not an object/array. + $json = '{"jsonrpc": "2.0", "error": "This should be an error object", "id": "err-789"}'; + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST); + $this->expectExceptionMessage('Invalid error object in JSON-RPC response'); + JsonRpcMessage::fromJson($json); + } + + public function testJsonSerializeRequestWithId(): void + { + $message = new JsonRpcMessage('test.request', ['foo' => 'bar'], 'req-789'); + // Ensure it's treated as a request by jsonSerialize, not result/error + $message->result = null; + $message->error = null; + + $serialized = $message->jsonSerialize(); + + $this->assertArrayHasKey('id', $serialized); + $this->assertEquals('req-789', $serialized['id']); + $this->assertEquals('test.request', $serialized['method']); + $this->assertEquals(['foo' => 'bar'], $serialized['params']); + $this->assertEquals('2.0', $serialized['jsonrpc']); + } }