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
|
|
|
|
14
|
|
|
namespace Graze\GuzzleHttp\JsonRpc; |
15
|
|
|
|
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface; |
17
|
|
|
use Graze\GuzzleHttp\JsonRpc\Test\UnitTestCase; |
18
|
|
|
use Mockery; |
19
|
|
|
|
20
|
|
|
class ClientTest extends UnitTestCase |
21
|
|
|
{ |
22
|
|
|
/** @var mixed */ |
23
|
|
|
private $httpClient; |
24
|
|
|
/** @var mixed */ |
25
|
|
|
private $httpHandler; |
26
|
|
|
/** @var mixed */ |
27
|
|
|
private $messageFactory; |
28
|
|
|
/** @var Client */ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
public function setUp(): void |
32
|
|
|
{ |
33
|
|
|
$this->httpClient = $this->mockHttpClient(); |
34
|
|
|
$this->httpHandler = $this->mockHttpHandler(); |
35
|
|
|
$this->messageFactory = $this->mockMessageFactory(); |
36
|
|
|
|
37
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('handler')->andReturn($this->httpHandler); |
38
|
|
|
$this->httpHandler->shouldReceive('push')->times(4); |
39
|
|
|
|
40
|
|
|
$this->client = new Client($this->httpClient, $this->messageFactory); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testInterface() |
44
|
|
|
{ |
45
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', $this->client); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testStaticFactory() |
49
|
|
|
{ |
50
|
|
|
$this->assertInstanceOf('Graze\GuzzleHttp\JsonRpc\ClientInterface', Client::factory('http://foo')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testNotification() |
54
|
|
|
{ |
55
|
|
|
$request = $this->mockRequest(); |
56
|
|
|
$jsonrpc = ['jsonrpc' => ClientInterface::SPEC, 'method' => 'foo']; |
57
|
|
|
$type = RequestInterface::NOTIFICATION; |
58
|
|
|
$uri = 'http://foo'; |
59
|
|
|
|
60
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
61
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
62
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request); |
63
|
|
|
|
64
|
|
|
$this->assertSame($request, $this->client->notification('foo')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testNotificationWithParams() |
68
|
|
|
{ |
69
|
|
|
$request = $this->mockRequest(); |
70
|
|
|
$jsonrpc = ['jsonrpc' => ClientInterface::SPEC, 'method' => 'foo', 'params' => ['bar' => true]]; |
71
|
|
|
$type = RequestInterface::NOTIFICATION; |
72
|
|
|
$uri = 'http://foo'; |
73
|
|
|
|
74
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
75
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
76
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request); |
77
|
|
|
|
78
|
|
|
$this->assertSame($request, $this->client->notification('foo', ['bar' => true])); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testRequest() |
82
|
|
|
{ |
83
|
|
|
$request = $this->mockRequest(); |
84
|
|
|
$jsonrpc = ['jsonrpc' => ClientInterface::SPEC, 'method' => 'foo', 'id' => 123]; |
85
|
|
|
$type = RequestInterface::REQUEST; |
86
|
|
|
$uri = 'http://foo'; |
87
|
|
|
|
88
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
89
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
90
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request); |
91
|
|
|
|
92
|
|
|
$this->assertSame($request, $this->client->request(123, 'foo')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testRequestWithParams() |
96
|
|
|
{ |
97
|
|
|
$request = $this->mockRequest(); |
98
|
|
|
$jsonrpc = ['jsonrpc' => ClientInterface::SPEC, 'method' => 'foo', 'params' => ['bar' => true], 'id' => 123]; |
99
|
|
|
$type = RequestInterface::REQUEST; |
100
|
|
|
$uri = 'http://foo'; |
101
|
|
|
|
102
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
103
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
104
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request); |
105
|
|
|
|
106
|
|
|
$this->assertSame($request, $this->client->request(123, 'foo', ['bar' => true])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testRequestWithEmptyParams() |
110
|
|
|
{ |
111
|
|
|
$request = $this->mockRequest(); |
112
|
|
|
$jsonrpc = ['jsonrpc' => ClientInterface::SPEC, 'method' => 'foo', 'id' => 123]; |
113
|
|
|
$type = RequestInterface::REQUEST; |
114
|
|
|
$uri = 'http://foo'; |
115
|
|
|
|
116
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
117
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
118
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], $jsonrpc)->andReturn($request); |
119
|
|
|
|
120
|
|
|
$this->assertSame($request, $this->client->request(123, 'foo', [])); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testSendNotification() |
124
|
|
|
{ |
125
|
|
|
$request = $this->mockRequest(); |
126
|
|
|
$response = $this->mockResponse(); |
127
|
|
|
$promise = $this->mockPromise(); |
128
|
|
|
|
129
|
|
|
$request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null); |
130
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise); |
131
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) { |
132
|
|
|
return null === $args($response); |
133
|
|
|
}))->andReturn($promise); |
134
|
|
|
$promise->shouldReceive('wait')->once()->withNoArgs()->andReturn(null); |
135
|
|
|
|
136
|
|
|
$this->assertNull($this->client->send($request)); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testSendNotificationAsync() |
140
|
|
|
{ |
141
|
|
|
$request = $this->mockRequest(); |
142
|
|
|
$response = $this->mockResponse(); |
143
|
|
|
$promise = $this->mockPromise(); |
144
|
|
|
|
145
|
|
|
$request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn(null); |
146
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise); |
147
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) { |
148
|
|
|
return null === $args($response); |
149
|
|
|
}))->andReturn($promise); |
150
|
|
|
|
151
|
|
|
$this->assertSame($promise, $this->client->sendAsync($request)); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testSendRequest() |
155
|
|
|
{ |
156
|
|
|
$request = $this->mockRequest(); |
157
|
|
|
$response = $this->mockResponse(); |
158
|
|
|
$promise = $this->mockPromise(); |
159
|
|
|
|
160
|
|
|
$request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo'); |
161
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise); |
162
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) { |
163
|
|
|
return $response === $args($response); |
164
|
|
|
}))->andReturn($promise); |
165
|
|
|
$promise->shouldReceive('wait')->once()->withNoArgs()->andReturn($response); |
166
|
|
|
|
167
|
|
|
$this->assertSame($response, $this->client->send($request)); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testSendRequestAsync() |
171
|
|
|
{ |
172
|
|
|
$request = $this->mockRequest(); |
173
|
|
|
$response = $this->mockResponse(); |
174
|
|
|
$promise = $this->mockPromise(); |
175
|
|
|
|
176
|
|
|
$request->shouldReceive('getRpcId')->once()->withNoArgs()->andReturn('foo'); |
177
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($request)->andReturn($promise); |
178
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($response) { |
179
|
|
|
return $response === $args($response); |
180
|
|
|
}))->andReturn($promise); |
181
|
|
|
|
182
|
|
|
$this->assertSame($promise, $this->client->sendAsync($request)); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testSendAll() |
186
|
|
|
{ |
187
|
|
|
$promise = $this->mockPromise(); |
188
|
|
|
$batchRequest = $this->mockRequest(); |
189
|
|
|
$requestA = $this->mockRequest(); |
190
|
|
|
$requestB = $this->mockRequest(); |
191
|
|
|
$batchResponse = $this->mockResponse(); |
192
|
|
|
$responseA = $this->mockResponse(); |
193
|
|
|
$responseB = $this->mockResponse(); |
194
|
|
|
|
195
|
|
|
$factory = $this->mockMessageFactory(); |
196
|
|
|
$this->httpClient->messageFactory = $factory; |
197
|
|
|
|
198
|
|
|
$requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]'); |
199
|
|
|
$requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]'); |
200
|
|
|
|
201
|
|
|
$type = RequestInterface::BATCH; |
202
|
|
|
$uri = 'http://foo'; |
203
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest); |
204
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
205
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
206
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise); |
207
|
|
|
|
208
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) { |
209
|
|
|
return [$responseA, $responseB] === $args($batchResponse); |
210
|
|
|
}))->andReturn($promise); |
211
|
|
|
$promise->shouldReceive('wait')->once()->withNoArgs()->andReturn([$responseA, $responseB]); |
212
|
|
|
|
213
|
|
|
$batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]'); |
214
|
|
|
$batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200); |
215
|
|
|
$batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']); |
216
|
|
|
|
217
|
|
|
$this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA); |
218
|
|
|
$this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB); |
219
|
|
|
|
220
|
|
|
$this->assertSame([$responseA, $responseB], $this->client->sendAll([$requestA, $requestB])); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testSendAllAsync() |
224
|
|
|
{ |
225
|
|
|
$promise = $this->mockPromise(); |
226
|
|
|
$batchRequest = $this->mockRequest(); |
227
|
|
|
$requestA = $this->mockRequest(); |
228
|
|
|
$requestB = $this->mockRequest(); |
229
|
|
|
$batchResponse = $this->mockResponse(); |
230
|
|
|
$responseA = $this->mockResponse(); |
231
|
|
|
$responseB = $this->mockResponse(); |
232
|
|
|
|
233
|
|
|
$factory = $this->mockMessageFactory(); |
234
|
|
|
$this->httpClient->messageFactory = $factory; |
235
|
|
|
|
236
|
|
|
$requestA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["foo"]'); |
237
|
|
|
$requestB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('["bar"]'); |
238
|
|
|
|
239
|
|
|
$type = RequestInterface::BATCH; |
240
|
|
|
$uri = 'http://foo'; |
241
|
|
|
$this->messageFactory->shouldReceive('createRequest')->once()->with($type, $uri, [], [['foo'], ['bar']])->andReturn($batchRequest); |
242
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('base_uri')->andReturn($uri); |
243
|
|
|
$this->httpClient->shouldReceive('getConfig')->once()->with('defaults')->andReturn([]); |
244
|
|
|
$this->httpClient->shouldReceive('sendAsync')->once()->with($batchRequest)->andReturn($promise); |
245
|
|
|
|
246
|
|
|
$promise->shouldReceive('then')->once()->with(Mockery::on(function ($args) use ($batchResponse, $responseA, $responseB) { |
247
|
|
|
return [$responseA, $responseB] === $args($batchResponse); |
248
|
|
|
}))->andReturn($promise); |
249
|
|
|
|
250
|
|
|
$batchResponse->shouldReceive('getBody')->once()->withNoArgs()->andReturn('[["foo"], ["bar"]]'); |
251
|
|
|
$batchResponse->shouldReceive('getStatusCode')->times(2)->withNoArgs()->andReturn(200); |
252
|
|
|
$batchResponse->shouldReceive('getHeaders')->times(2)->withNoArgs()->andReturn(['headers']); |
253
|
|
|
|
254
|
|
|
$this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['foo'])->andReturn($responseA); |
255
|
|
|
$this->messageFactory->shouldReceive('createResponse')->once()->with(200, ['headers'], ['bar'])->andReturn($responseB); |
256
|
|
|
|
257
|
|
|
$this->assertSame($promise, $this->client->sendAllAsync([$requestA, $requestB])); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|