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); |
|
|
|
|
37
|
|
|
$this->httpHandler->shouldReceive('push')->times(4); |
38
|
|
|
|
39
|
|
|
$this->client = new Client($this->httpClient, $this->messageFactory); |
|
|
|
|
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); |
|
|
|
|
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)); |
|
|
|
|
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); |
|
|
|
|
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)); |
|
|
|
|
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'); |
|
|
|
|
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)); |
|
|
|
|
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'); |
|
|
|
|
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)); |
|
|
|
|
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"]'); |
|
|
|
|
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"]'); |
|
|
|
|
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
|
|
|
|
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.