Passed
Push — master ( b091a1...a43e6f )
by Pavel
03:13
created

JsonRpcClientTest::getProphetClient()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 2.0013

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
ccs 27
cts 29
cp 0.931
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 25
nc 1
nop 2
crap 2.0013

1 Method

Rating   Name   Duplication   Size   Complexity  
A JsonRpcClientTest::testSingleSuccessfulRequest() 0 16 1
1
<?php
2
3
namespace ScayTrase\Api\JsonRpc\Tests;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ConnectException;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Psr7\Response;
10
use GuzzleHttp\Psr7\Uri;
11
use ScayTrase\Api\JsonRpc\JsonRpcClient;
12
use ScayTrase\Api\JsonRpc\JsonRpcError;
13
use ScayTrase\Api\JsonRpc\JsonRpcErrorInterface;
14
use ScayTrase\Api\JsonRpc\JsonRpcNotification;
15
use ScayTrase\Api\JsonRpc\JsonRpcRequest;
16
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface;
17
use ScayTrase\Api\Rpc\Exception\RemoteCallFailedException;
18
use ScayTrase\Api\Rpc\RpcErrorInterface;
19
20
final class JsonRpcClientTest extends AbstractJsonRpcClientTest
21
{
22
    public function paramsProvider()
23
    {
24
        return [
25
            'Null' => [null],
26
            'Empty array' => [[]],
27
            'Scalar' => [5], // This really should be an exception
28
            'Named Params' => [(object)['test' => 'test']],
29
            'Positional params' => [['test']],
30
        ];
31
    }
32
33
    /**
34
     * @dataProvider paramsProvider
35
     * @param $params
36
     */
37
    public function testSingleRequestFormatting($params)
38
    {
39
        $client = $this->getProphetClient('test', $params, false);
40
        $client->invoke(new JsonRpcRequest('test', $params, 'test'));
41
    }
42
43
    /**
44
     * @dataProvider paramsProvider
45
     * @param $params
46
     */
47
    public function testBatchRequestFormatting($params)
48
    {
49
        $client = $this->getProphetClient('test', $params, true);
50
        $client->invoke([new JsonRpcRequest('test', $params, 'test')]);
51
    }
52
53
    public function testSingleSuccessfulRequest()
54
    {
55
        $client = new JsonRpcClient($this->getClient(), new Uri('http://localhost/'));
56
57
        $request = $this->createRequestForSingleInvocation('/test', ['parameter' => 'test'], ['foo' => 'bar']);
58
        $collection = $client->invoke([$request]);
59
60
        /** @var JsonRpcResponseInterface $response */
61
        $response = $collection->getResponse($request);
62
        self::assertTrue($response->isSuccessful());
63
        self::assertNotNull($response->getBody());
64
        self::assertEquals($client::VERSION, $response->getVersion());
65
        self::assertNull($response->getError());
66
        self::assertObjectHasAttribute('foo', $response->getBody());
67
        self::assertEquals('bar', $response->getBody()->foo);
68
    }
69
70
    public function testSingleFailedRequest()
71
    {
72
        $client = new JsonRpcClient($this->getClient(), new Uri('http://localhost/'));
73
74
        $request = $this->createRequestForSingleInvocation(
75
            '/test',
76
            ['parameter' => 'test'],
77
            new JsonRpcError(JsonRpcErrorInterface::INTERNAL_ERROR, 'Test error')
78
        );
79
        $collection = $client->invoke([$request]);
80
81
        /** @var JsonRpcResponseInterface $response */
82
        $response = $collection->getResponse($request);
83
        self::assertFalse($response->isSuccessful());
84
        self::assertNull($response->getBody());
85
        self::assertNotNull($response->getError());
86
        self::assertEquals($client::VERSION, $response->getVersion());
87
        self::assertInstanceOf(RpcErrorInterface::class, $response->getError());
88
        self::assertEquals(JsonRpcErrorInterface::INTERNAL_ERROR, $response->getError()->getCode());
89
        self::assertEquals('Test error', $response->getError()->getMessage());
90
    }
91
92
    public function testSingleNotification()
93
    {
94
        $client = new JsonRpcClient($this->getClient(), new Uri('http://localhost/'));
95
96
        $notification = $this->createNotificationForSingleInvocation('/test-notify', ['parameter' => 'test']);
97
        $collection = $client->invoke([$notification]);
98
99
        $response = $collection->getResponse($notification);
100
        self::assertTrue($response->isSuccessful());
101
        self::assertNull($response->getBody());
102
        self::assertNull($response->getError());
103
        self::assertEquals($client::VERSION, $response->getVersion());
104
    }
105
106
    public function testMultipleMixedRequests()
107
    {
108
        $handler = HandlerStack::create($this->getQueue());
109
        $this->client = new Client(['handler' => $handler]);
110
111
        $client = new JsonRpcClient($this->getClient(), new Uri('http://localhost/'));
112
113
        $request = new JsonRpcRequest('/test-request', [], $this->getRandomHash());
114
        $notification = new JsonRpcNotification('/test-notify', []);
115
        $error = new JsonRpcRequest('/test-error', [], $this->getRandomHash());
116
117
        $this->getQueue()->append(
118
            new Response(
119
                200, [], json_encode(
120
                [
121
                    ['jsonrpc' => '2.0', 'id' => $request->getId(), 'result' => ['success' => true]],
122
                    [
123
                        'jsonrpc' => '2.0',
124
                        'id' => $error->getId(),
125
                        'error' => ['code' => JsonRpcErrorInterface::INTERNAL_ERROR, 'message' => 'Test error'],
126
                    ],
127
                ]
128
            )
129
            )
130
        );
131
132
        $calls = [$request, $notification, $error];
133
        $collection = $client->invoke($calls);
134
135
        $rResponse = $collection->getResponse($request);
136
        $nResponse = $collection->getResponse($notification);
137
        $eResponse = $collection->getResponse($error);
138
139
        self::assertNotNull($rResponse);
140
        self::assertInstanceOf(JsonRpcResponseInterface::class, $rResponse);
141
        self::assertTrue($rResponse->isSuccessful());
142
        self::assertObjectHasAttribute('success', $rResponse->getBody());
143
        self::assertTrue($rResponse->getBody()->success);
144
145
        self::assertNotNull($nResponse);
146
        self::assertInstanceOf(JsonRpcResponseInterface::class, $nResponse);
147
        self::assertTrue($nResponse->isSuccessful());
148
149
        self::assertNotNull($eResponse);
150
        self::assertInstanceOf(JsonRpcResponseInterface::class, $eResponse);
151
        self::assertFalse($eResponse->isSuccessful());
152
        self::assertInstanceOf(JsonRpcErrorInterface::class, $eResponse->getError());
153
        self::assertEquals('Test error', $eResponse->getError()->getMessage());
154
        self::assertEquals(JsonRpcErrorInterface::INTERNAL_ERROR, $eResponse->getError()->getCode());
155
    }
156
157
    /** @expectedException \ScayTrase\Api\Rpc\Exception\RemoteCallFailedException */
158
    public function testFailedCall()
159
    {
160
        $client = new JsonRpcClient($this->getClient(), new Uri('http://localhost/'));
161
162
        $request = $this->createRequestForSingleInvocation(
163
            '/test-notify',
164
            ['parameter' => 'test'],
165
            new ConnectException('Connection failed', new Request('POST', 'test'))
166
        );
167
168
        $client->invoke([$request])->getResponse($request);
169
    }
170
}
171