Skip to content

Commit 267f619

Browse files
google-labs-jules[bot]james2037
authored andcommitted
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.
1 parent 8d01ed2 commit 267f619

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/Message/JsonRpcMessageTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,50 @@ public function testFromJsonWithErrorField(): void
359359
$this->assertNull($message->params);
360360
$this->assertNull($message->result);
361361
}
362+
363+
public function testFromJsonResponseMissingId(): void
364+
{
365+
// This JSON represents a result response but is missing the 'id' field.
366+
$json = '{"jsonrpc": "2.0", "result": {"foo": "bar"}}';
367+
$this->expectException(\RuntimeException::class);
368+
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
369+
$this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)');
370+
JsonRpcMessage::fromJson($json);
371+
}
372+
373+
public function testFromJsonErrorResponseMissingId(): void
374+
{
375+
// This JSON represents an error response but is missing the 'id' field.
376+
$json = '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}';
377+
$this->expectException(\RuntimeException::class);
378+
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
379+
$this->expectExceptionMessage('Response must include ID (even if null for some error cases as per spec, this implementation expects it)');
380+
JsonRpcMessage::fromJson($json);
381+
}
382+
383+
public function testFromJsonErrorFieldNotAnArray(): void
384+
{
385+
// This JSON represents an error response, but the 'error' field is a string, not an object/array.
386+
$json = '{"jsonrpc": "2.0", "error": "This should be an error object", "id": "err-789"}';
387+
$this->expectException(\RuntimeException::class);
388+
$this->expectExceptionCode(JsonRpcMessage::INVALID_REQUEST);
389+
$this->expectExceptionMessage('Invalid error object in JSON-RPC response');
390+
JsonRpcMessage::fromJson($json);
391+
}
392+
393+
public function testJsonSerializeRequestWithId(): void
394+
{
395+
$message = new JsonRpcMessage('test.request', ['foo' => 'bar'], 'req-789');
396+
// Ensure it's treated as a request by jsonSerialize, not result/error
397+
$message->result = null;
398+
$message->error = null;
399+
400+
$serialized = $message->jsonSerialize();
401+
402+
$this->assertArrayHasKey('id', $serialized);
403+
$this->assertEquals('req-789', $serialized['id']);
404+
$this->assertEquals('test.request', $serialized['method']);
405+
$this->assertEquals(['foo' => 'bar'], $serialized['params']);
406+
$this->assertEquals('2.0', $serialized['jsonrpc']);
407+
}
362408
}

0 commit comments

Comments
 (0)