ClientTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 144
c 4
b 0
f 0
dl 0
loc 238
rs 10
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testInterface() 0 3 1
A testStaticFactory() 0 3 1
A testNotificationWithParams() 0 12 1
A testRequestWithEmptyParams() 0 12 1
A setup() 0 10 1
A testNotification() 0 12 1
A testSendAllAsync() 0 35 1
A testSendNotificationAsync() 0 13 1
A testRequest() 0 12 1
A testSendAll() 0 36 1
A testRequestWithParams() 0 12 1
A testSendRequest() 0 14 1
A testSendNotification() 0 14 1
A testSendRequestAsync() 0 13 1
1
<?php
2
/*
3
 * This file is part of Guzzle HTTP JSON-RPC
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
11
 * @link http://github.com/graze/guzzle-jsonrpc
12
 */
13
namespace Graze\GuzzleHttp\JsonRpc;
14
15
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
16
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase;
17
use Mockery;
18
19
class ClientTest extends UnitTestCase
20
{
21
    /** @var mixed */
22
    private $httpClient;
23
    /** @var mixed */
24
    private $httpHandler;
25
    /** @var mixed */
26
    private $messageFactory;
27
    /** @var Client */
28
    private $client;
29
30
    public function setup()
31
    {
32
        $this->httpClient = $this->mockHttpClient();
33
        $this->httpHandler = $this->mockHttpHandler();
34
        $this->messageFactory = $this->mockMessageFactory();
35
36
        $this->httpClient->shouldReceive('getConfig')->once()->with('handler')->andReturn($this->httpHandler);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getConfig'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->httpClient->/** @scrutinizer ignore-call */ 
37
                           shouldReceive('getConfig')->once()->with('handler')->andReturn($this->httpHandler);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
        $this->httpHandler->shouldReceive('push')->times(4);
38
39
        $this->client = new Client($this->httpClient, $this->messageFactory);
0 ignored issues
show
Bug introduced by
$this->httpClient of type Mockery\MockInterface is incompatible with the type GuzzleHttp\ClientInterface expected by parameter $httpClient of Graze\GuzzleHttp\JsonRpc\Client::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $this->client = new Client(/** @scrutinizer ignore-type */ $this->httpClient, $this->messageFactory);
Loading history...
Bug introduced by
$this->messageFactory of type Mockery\MockInterface is incompatible with the type Graze\GuzzleHttp\JsonRpc...MessageFactoryInterface expected by parameter $factory of Graze\GuzzleHttp\JsonRpc\Client::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $this->client = new Client($this->httpClient, /** @scrutinizer ignore-type */ $this->messageFactory);
Loading history...
40
    }
41
42
    public function testInterface()
43
    {
44
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', $this->client);
45
    }
46
47
    public function testStaticFactory()
48
    {
49
        $this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', Client::factory('http://foo'));
50
    }
51
52
    public function testNotification()
53
    {
54
        $request = $this->mockRequest();
55
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo'];
56
        $type = RequestInterface::NOTIFICATION;
57
        $uri = 'http://foo';
58
59
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
60
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
61
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
62
63
        $this->assertSame($request, $this->client->notification('foo'));
64
    }
65
66
    public function testNotificationWithParams()
67
    {
68
        $request = $this->mockRequest();
69
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'params'=>['bar'=>true]];
70
        $type = RequestInterface::NOTIFICATION;
71
        $uri = 'http://foo';
72
73
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
74
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
75
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
76
77
        $this->assertSame($request, $this->client->notification('foo', ['bar'=>true]));
78
    }
79
80
    public function testRequest()
81
    {
82
        $request = $this->mockRequest();
83
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'id'=>123];
84
        $type = RequestInterface::REQUEST;
85
        $uri = 'http://foo';
86
87
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
88
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
89
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
90
91
        $this->assertSame($request, $this->client->request(123, 'foo'));
92
    }
93
94
    public function testRequestWithParams()
95
    {
96
        $request = $this->mockRequest();
97
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'params'=>['bar'=>true], 'id'=>123];
98
        $type = RequestInterface::REQUEST;
99
        $uri = 'http://foo';
100
101
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
102
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
103
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
104
105
        $this->assertSame($request, $this->client->request(123, 'foo', ['bar'=>true]));
106
    }
107
108
    public function testRequestWithEmptyParams()
109
    {
110
        $request = $this->mockRequest();
111
        $jsonrpc = ['jsonrpc'=>ClientInterface::SPEC, 'method'=>'foo', 'id'=>123];
112
        $type = RequestInterface::REQUEST;
113
        $uri = 'http://foo';
114
115
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
116
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
117
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request);
118
119
        $this->assertSame($request, $this->client->request(123, 'foo', []));
120
    }
121
122
    public function testSendNotification()
123
    {
124
        $request = $this->mockRequest();
125
        $response = $this->mockResponse();
126
        $promise = $this->mockPromise();
127
128
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getRpcId'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
        $request->/** @scrutinizer ignore-call */ 
129
                  shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
129
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
130
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
131
            return null === $args($response);
132
        }))->andReturn($promise);
133
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn(null);
134
135
        $this->assertNull($this->client->send($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\MockInterface is incompatible with the type Graze\GuzzleHttp\JsonRpc\Message\RequestInterface expected by parameter $request of Graze\GuzzleHttp\JsonRpc\Client::send(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $this->assertNull($this->client->send(/** @scrutinizer ignore-type */ $request));
Loading history...
136
    }
137
138
    public function testSendNotificationAsync()
139
    {
140
        $request = $this->mockRequest();
141
        $response = $this->mockResponse();
142
        $promise = $this->mockPromise();
143
144
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getRpcId'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        $request->/** @scrutinizer ignore-call */ 
145
                  shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
145
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
146
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
147
            return null === $args($response);
148
        }))->andReturn($promise);
149
150
        $this->assertSame($promise, $this->client->sendAsync($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\MockInterface is incompatible with the type Graze\GuzzleHttp\JsonRpc\Message\RequestInterface expected by parameter $request of Graze\GuzzleHttp\JsonRpc\Client::sendAsync(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        $this->assertSame($promise, $this->client->sendAsync(/** @scrutinizer ignore-type */ $request));
Loading history...
151
    }
152
153
    public function testSendRequest()
154
    {
155
        $request = $this->mockRequest();
156
        $response = $this->mockResponse();
157
        $promise = $this->mockPromise();
158
159
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getRpcId'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
        $request->/** @scrutinizer ignore-call */ 
160
                  shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
160
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
161
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
162
            return $response === $args($response);
163
        }))->andReturn($promise);
164
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn($response);
165
166
        $this->assertSame($response, $this->client->send($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\MockInterface is incompatible with the type Graze\GuzzleHttp\JsonRpc\Message\RequestInterface expected by parameter $request of Graze\GuzzleHttp\JsonRpc\Client::send(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
        $this->assertSame($response, $this->client->send(/** @scrutinizer ignore-type */ $request));
Loading history...
167
    }
168
169
    public function testSendRequestAsync()
170
    {
171
        $request = $this->mockRequest();
172
        $response = $this->mockResponse();
173
        $promise = $this->mockPromise();
174
175
        $request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getRpcId'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
        $request->/** @scrutinizer ignore-call */ 
176
                  shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
176
        $this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise);
177
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) {
178
            return $response === $args($response);
179
        }))->andReturn($promise);
180
181
        $this->assertSame($promise, $this->client->sendAsync($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\MockInterface is incompatible with the type Graze\GuzzleHttp\JsonRpc\Message\RequestInterface expected by parameter $request of Graze\GuzzleHttp\JsonRpc\Client::sendAsync(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
        $this->assertSame($promise, $this->client->sendAsync(/** @scrutinizer ignore-type */ $request));
Loading history...
182
    }
183
184
    public function testSendAll()
185
    {
186
        $promise = $this->mockPromise();
187
        $batchRequest = $this->mockRequest();
188
        $requestA = $this->mockRequest();
189
        $requestB = $this->mockRequest();
190
        $batchResponse = $this->mockResponse();
191
        $responseA = $this->mockResponse();
192
        $responseB = $this->mockResponse();
193
194
        $factory = $this->mockMessageFactory();
195
        $this->httpClient->messageFactory = $factory;
196
197
        $requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getBody'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
        $requestA->/** @scrutinizer ignore-call */ 
198
                   shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
198
        $requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]');
199
200
        $type = RequestInterface::BATCH;
201
        $uri = 'http://foo';
202
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest);
203
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
204
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
205
        $this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise);
206
207
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) {
208
            return [$responseA, $responseB] === $args($batchResponse);
209
        }))->andReturn($promise);
210
        $promise->shouldReceive('wait')->once()->withNoArgs()->andReturn([$responseA, $responseB]);
211
212
        $batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]');
213
        $batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200);
214
        $batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']);
215
216
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA);
217
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB);
218
219
        $this->assertSame([$responseA, $responseB], $this->client->sendAll([$requestA, $requestB]));
220
    }
221
222
    public function testSendAllAsync()
223
    {
224
        $promise = $this->mockPromise();
225
        $batchRequest = $this->mockRequest();
226
        $requestA = $this->mockRequest();
227
        $requestB = $this->mockRequest();
228
        $batchResponse = $this->mockResponse();
229
        $responseA = $this->mockResponse();
230
        $responseB = $this->mockResponse();
231
232
        $factory = $this->mockMessageFactory();
233
        $this->httpClient->messageFactory = $factory;
234
235
        $requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getBody'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
        $requestA->/** @scrutinizer ignore-call */ 
236
                   shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
236
        $requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]');
237
238
        $type = RequestInterface::BATCH;
239
        $uri = 'http://foo';
240
        $this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest);
241
        $this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri);
242
        $this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]);
243
        $this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise);
244
245
        $promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) {
246
            return [$responseA, $responseB] === $args($batchResponse);
247
        }))->andReturn($promise);
248
249
        $batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]');
250
        $batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200);
251
        $batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']);
252
253
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA);
254
        $this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB);
255
256
        $this->assertSame($promise, $this->client->sendAllAsync([$requestA, $requestB]));
257
    }
258
}
259