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