|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouterTests\Response; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Vectorface\SnappyRouter\Request\JsonRpcRequest; |
|
8
|
|
|
use Vectorface\SnappyRouter\Response\JsonRpcResponse; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Tests the JsonRpcResponse class. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
14
|
|
|
*/ |
|
15
|
|
|
class JsonRpcResponseTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* An overview of how to use the JsonRpcResponse class. |
|
19
|
|
|
* @test |
|
20
|
|
|
*/ |
|
21
|
|
|
public function synopsis() |
|
22
|
|
|
{ |
|
23
|
|
|
/* Needs to be based on a related request */ |
|
24
|
|
|
$request = new JsonRpcRequest('MyService', (object)array( |
|
25
|
|
|
'jsonrpc' => '2.0', |
|
26
|
|
|
'method' => 'remoteProcedure', |
|
27
|
|
|
'params' => array(1, 2, 3), |
|
28
|
|
|
'id' => 'identifier' |
|
29
|
|
|
)); |
|
30
|
|
|
|
|
31
|
|
|
$response = new JsonRpcResponse('object, array, or scalar', null, $request); |
|
32
|
|
|
$obj = $response->getResponseObject(); |
|
33
|
|
|
$this->assertEquals("2.0", $obj->jsonrpc, "Responds with the same version"); |
|
34
|
|
|
$this->assertEquals('object, array, or scalar', $obj->result, "Result is passed through"); |
|
35
|
|
|
$this->assertEquals("identifier", $obj->id, "Request ID is passed back"); |
|
36
|
|
|
|
|
37
|
|
|
/* Notifications generate no response */ |
|
38
|
|
|
$request = new JsonRpcRequest('MyService', (object)array('method' => 'notifyProcedure')); |
|
39
|
|
|
$response = new JsonRpcResponse("anything", null, $request); |
|
40
|
|
|
$this->assertEquals("", $response->getResponseObject()); |
|
41
|
|
|
|
|
42
|
|
|
/* An error passes back a message and a code */ |
|
43
|
|
|
$request = new JsonRpcRequest('MyService', (object)array('method' => 'any', 'id' => 123)); |
|
44
|
|
|
$response = new JsonRpcResponse(null, new Exception("ex", 123), $request); |
|
45
|
|
|
$obj = $response->getResponseObject(); |
|
46
|
|
|
$this->assertEquals(123, $obj->error->code); |
|
47
|
|
|
$this->assertEquals("ex", $obj->error->message); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|