From 9aee619482ee417932631f2755bc142cb24f67fc Mon Sep 17 00:00:00 2001 From: Tom Schuermans Date: Thu, 7 Jan 2016 11:00:16 +0100 Subject: [PATCH 1/3] Added a method to fetch RPC error data, as defined in http://www.jsonrpc.org/specification#error_object --- src/Message/Response.php | 10 ++++++++++ src/Message/ResponseInterface.php | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/Message/Response.php b/src/Message/Response.php index 74b8e58..867b932 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -39,6 +39,16 @@ public function getRpcErrorMessage() return isset($error['message']) ? $error['message'] : null; } + /** + * {@inheritdoc} + */ + public function getRpcErrorData() + { + $error = $this->getFieldFromBody('error'); + + return isset($error['data']) ? $error['data'] : null; + } + /** * {@inheritdoc} */ diff --git a/src/Message/ResponseInterface.php b/src/Message/ResponseInterface.php index f902a4e..80dc065 100644 --- a/src/Message/ResponseInterface.php +++ b/src/Message/ResponseInterface.php @@ -28,6 +28,11 @@ public function getRpcErrorCode(); */ public function getRpcErrorMessage(); + /** + * @return mixed + */ + public function getRpcErrorData(); + /** * @return mixed */ From 44f862c48c8a62e99818b3a5abe3dff5bc32b438 Mon Sep 17 00:00:00 2001 From: Tom Schuermans Date: Thu, 7 Jan 2016 14:46:11 +0100 Subject: [PATCH 2/3] Updated unit & functional tests --- test/functional/RequestFunctionalTest.php | 8 ++++++++ test/unit/Message/ResponseTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/test/functional/RequestFunctionalTest.php b/test/functional/RequestFunctionalTest.php index 019e623..71417a0 100644 --- a/test/functional/RequestFunctionalTest.php +++ b/test/functional/RequestFunctionalTest.php @@ -48,6 +48,7 @@ public function testConcatRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); } public function testConcatAsyncRequest() @@ -69,6 +70,7 @@ public function testConcatAsyncRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); }); } @@ -90,6 +92,7 @@ public function testSumRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); } public function testSumAsyncRequest() @@ -111,6 +114,7 @@ public function testSumAsyncRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); }); } @@ -131,6 +135,7 @@ public function testFooRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); } public function testFooAsyncRequest() @@ -151,6 +156,7 @@ public function testFooAsyncRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertEquals(null, $response->getRpcErrorCode()); $this->assertEquals(null, $response->getRpcErrorMessage()); + $this->assertEquals(null, $response->getRpcErrorData()); }); } @@ -171,6 +177,7 @@ public function testBarRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertTrue(is_int($response->getRpcErrorCode())); $this->assertTrue(is_string($response->getRpcErrorMessage())); + $this->assertEquals(null, $response->getRpcErrorData()); } public function testBarAsyncRequest() @@ -191,6 +198,7 @@ public function testBarAsyncRequest() $this->assertEquals($id, $response->getRpcId()); $this->assertTrue(is_int($response->getRpcErrorCode())); $this->assertTrue(is_string($response->getRpcErrorMessage())); + $this->assertEquals(null, $response->getRpcErrorData()); }); } diff --git a/test/unit/Message/ResponseTest.php b/test/unit/Message/ResponseTest.php index 0a32193..3503f72 100644 --- a/test/unit/Message/ResponseTest.php +++ b/test/unit/Message/ResponseTest.php @@ -76,6 +76,22 @@ public function testGetRpcErrorMessageIsNull() $this->assertNull($response->getRpcErrorCode()); } + public function testGetRpcErrorData() + { + $response = new Response(200, [], json_encode([ + 'error' => ['data' => array()] + ])); + + $this->assertEquals(array(), $response->getRpcErrorMessage()); + } + + public function testGetRpcErrorDataIsNull() + { + $response = new Response(200); + + $this->assertNull($response->getRpcErrorData()); + } + public function testGetRpcResult() { $response = new Response(200, [], json_encode([ From f39fbc1ba4803401d618d159062495ac4d10ddab Mon Sep 17 00:00:00 2001 From: Tom Schuermans Date: Thu, 7 Jan 2016 14:56:04 +0100 Subject: [PATCH 3/3] Fixed failing test and updated testGetRpcErrorMessageIsNull --- test/unit/Message/ResponseTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/Message/ResponseTest.php b/test/unit/Message/ResponseTest.php index 3503f72..3cdccf8 100644 --- a/test/unit/Message/ResponseTest.php +++ b/test/unit/Message/ResponseTest.php @@ -73,7 +73,7 @@ public function testGetRpcErrorMessageIsNull() { $response = new Response(200); - $this->assertNull($response->getRpcErrorCode()); + $this->assertNull($response->getRpcErrorMessage()); } public function testGetRpcErrorData() @@ -82,7 +82,7 @@ public function testGetRpcErrorData() 'error' => ['data' => array()] ])); - $this->assertEquals(array(), $response->getRpcErrorMessage()); + $this->assertEquals(array(), $response->getRpcErrorData()); } public function testGetRpcErrorDataIsNull()