Completed
Pull Request — dev (#11)
by
unknown
04:51
created

JsonRpcResponseTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 36
rs 10

1 Method

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