ClientTest::testRequests()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.6
1
<?php
2
3
use Codeception\Test\Unit;
4
use veejay\jsonrpc\tests\Client;
5
use veejay\jsonrpc\batch\Request;
6
use veejay\jsonrpc\batch\Response;
7
use veejay\jsonrpc\tests\ProtectedHelper;
8
9
class ClientTest extends Unit
10
{
11
    public function testRequests()
12
    {
13
        $client = new Client('');
14
        $values = [];
15
16
        $client->query('withoutParams');
17
        $values[] = (new Request)->setProperties([
18
            'jsonrpc' => Request::VERSION,
19
            'method' => 'withoutParams',
20
            'id' => 0,
21
        ]);
22
        $this->assertEquals($values, $client->requests);
23
24
        $client->notify('withoutParams');
25
        $values[] = (new Request)->setProperties([
26
            'jsonrpc' => Request::VERSION,
27
            'method' => 'withoutParams',
28
        ]);
29
        $this->assertEquals($values, $client->requests);
30
31
        $client->query('withoutParams', ['param' => 'one']);
32
        $values[] = (new Request)->setProperties([
33
            'jsonrpc' => Request::VERSION,
34
            'method' => 'withoutParams',
35
            'params' => ['param' => 'one'],
36
            'id' => 2,
37
        ]);
38
        $this->assertEquals($values, $client->requests);
39
    }
40
41
    public function testResponses()
42
    {
43
        $client = new Client('');
44
        $values = [];
45
46
        $client->query('withoutParams');
47
        $values[0] = (new Response)->setProperties([
48
            'jsonrpc' => Response::VERSION,
49
            'id' => 0,
50
        ])->setError(Response::PARSE_ERROR);
51
        $this->assertEquals($values, $client->responses);
52
53
        $client->notify('withoutParams');
54
        $this->assertEquals($values, $client->responses);
55
56
        $client->query('withoutParams', ['param' => 'one']);
57
        $values[2] = (new Response)->setProperties([
58
            'jsonrpc' => Response::VERSION,
59
            'id' => 2,
60
        ])->setError(Response::PARSE_ERROR);
61
        $this->assertEquals($values, $client->responses);
62
    }
63
64
    public function testSend()
65
    {
66
        $client = new Client('');
67
        $actual = $client->query('withoutParams');
68
69
        // Error while getting content
70
        $client->_data = null;
71
        $client->send();
72
        $expected = (new Response)->setProperties([
73
            'jsonrpc' => Response::VERSION,
74
            'id' => 0,
75
        ])->setError(Response::PARSE_ERROR);
76
        $this->assertEquals($expected, $actual);
77
78
        // Error response
79
        $client->_data = json_decode('{"jsonrpc": "2.0", "error": {"code": -32700: "message": "Parse error"}, "id": null}');
80
        $client->send();
81
        $expected = (new Response)->setProperties([
82
            'jsonrpc' => Response::VERSION,
83
            'id' => 0,
84
        ])->setError(Response::PARSE_ERROR);
85
        $this->assertEquals($expected, $actual);
86
87
        // Single response
88
        $client->_data = json_decode('{"jsonrpc": "2.0", "result": "result_str", "id": 0}');
89
        $client->send();
90
        $expected = (new Response)->setProperties([
91
            'jsonrpc' => Response::VERSION,
92
            'result' => 'result_str',
93
            'id' => 0,
94
        ]);
95
        $this->assertEquals($expected, $actual);
96
97
        // Multiple responses
98
        $client->_data = json_decode('[{"jsonrpc": "2.0", "result": "result1", "id": 0}, {"jsonrpc": "2.0", "result": "result2", "id": 1}]');
99
        $actual2 = $client->query('withoutParams');
100
        $client->send();
101
        $expected = (new Response)->setProperties([
102
            'jsonrpc' => Response::VERSION,
103
            'result' => 'result1',
104
            'id' => 0,
105
        ]);
106
        $this->assertEquals($expected, $actual);
107
        $expected = (new Response)->setProperties([
108
            'jsonrpc' => Response::VERSION,
109
            'result' => 'result2',
110
            'id' => 1,
111
        ]);
112
        $this->assertEquals($expected, $actual2);
113
    }
114
115
    public function testSetResponsesError()
116
    {
117
        $client = new Client('');
118
        $one = $client->query('one');
119
        $two = $client->query('two');
120
121
        ProtectedHelper::callMethod($client, 'setResponsesError', [Response::INTERNAL_ERROR, 'qqq']);
122
        $this->assertEquals(
123
            ['code' => Response::INTERNAL_ERROR, 'message' => 'qqq'],
124
            (array)$one->error
125
        );
126
        $this->assertEquals(
127
            ['code' => Response::INTERNAL_ERROR, 'message' => 'qqq'],
128
            (array)$two->error
129
        );
130
    }
131
}
132