Skip to content

Commit 8d01ed2

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

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/Message/JsonRpcMessageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,28 @@ public function testToResponseArrayForNotificationThrowsException(): void
335335
$this->expectExceptionMessage('Message is not a response or error, cannot convert to response array.');
336336
$message->toResponseArray();
337337
}
338+
339+
public function testFromJsonWithResultField(): void
340+
{
341+
$json = '{"jsonrpc":"2.0","result":{"data":"success"},"id":"res-123"}';
342+
$message = JsonRpcMessage::fromJson($json);
343+
344+
$this->assertSame('res-123', $message->id);
345+
$this->assertSame(['data' => 'success'], $message->result);
346+
$this->assertSame('', $message->method);
347+
$this->assertNull($message->params);
348+
$this->assertNull($message->error);
349+
}
350+
351+
public function testFromJsonWithErrorField(): void
352+
{
353+
$json = '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":"err-456"}';
354+
$message = JsonRpcMessage::fromJson($json);
355+
356+
$this->assertSame('err-456', $message->id);
357+
$this->assertSame(['code' => -32600, 'message' => 'Invalid Request'], $message->error);
358+
$this->assertSame('', $message->method);
359+
$this->assertNull($message->params);
360+
$this->assertNull($message->result);
361+
}
338362
}

0 commit comments

Comments
 (0)