summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/Response/ResponseParserTest.php
blob: f195014fb3f6b11707419da76fcdd53b3158e506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

use JsonRPC\Response\ResponseParser;

require_once __DIR__.'/../../../../vendor/autoload.php';

class ResponseParserTest extends PHPUnit_Framework_TestCase
{
    public function testSingleRequest()
    {
        $result = ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "result": "foobar", "id": "1"}', true))
            ->parse();

        $this->assertEquals('foobar', $result);
    }

    public function testWithBadJsonFormat()
    {
        $this->setExpectedException('\JsonRPC\Exception\InvalidJsonFormatException');

        ResponseParser::create()
            ->withPayload('foobar')
            ->parse();
    }

    public function testWithBadProcedure()
    {
        $this->setExpectedException('BadFunctionCallException');

        ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}', true))
            ->parse();
    }

    public function testWithInvalidArgs()
    {
        $this->setExpectedException('InvalidArgumentException');

        ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": "1"}', true))
            ->parse();
    }

    public function testWithInvalidRequest()
    {
        $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');

        ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}', true))
            ->parse();
    }

    public function testWithParseError()
    {
        $this->setExpectedException('\JsonRPC\Exception\InvalidJsonFormatException');

        ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}', true))
            ->parse();
    }

    public function testWithOtherError()
    {
        $this->setExpectedException('\JsonRPC\Exception\ResponseException');

        ResponseParser::create()
            ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": 42, "message": "Something", "data": "foobar"}, "id": null}', true))
            ->parse();
    }

    public function testBatch()
    {
        $payload = '[
            {"jsonrpc": "2.0", "result": 7, "id": "1"},
            {"jsonrpc": "2.0", "result": 19, "id": "2"}
        ]';

        $result = ResponseParser::create()
            ->withPayload(json_decode($payload, true))
            ->parse();

        $this->assertEquals(array(7, 19), $result);
    }

    public function testBatchWithError()
    {
        $payload = '[
            {"jsonrpc": "2.0", "result": 7, "id": "1"},
            {"jsonrpc": "2.0", "result": 19, "id": "2"},
            {"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": "1"}
        ]';

        $this->setExpectedException('InvalidArgumentException');

        ResponseParser::create()
            ->withPayload(json_decode($payload, true))
            ->parse();
    }
}