|
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\Exception\ClientException; |
|
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Test\FunctionalTestCase; |
|
17
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
18
|
|
|
|
|
19
|
|
|
class RequestFunctionalTest extends FunctionalTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var Client */ |
|
22
|
|
|
private $client; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->client = $this->createClient(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
public function testConcatRequest() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$id = 123; |
|
32
|
|
|
$method = 'concat'; |
|
33
|
|
|
$params = ['foo'=>'abc', 'bar'=>'def']; |
|
34
|
|
|
$request = $this->client->request($id, $method, $params); |
|
35
|
|
|
$response = $this->client->send($request); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
38
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
39
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
40
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
43
|
|
|
$this->assertEquals(implode('', $params), $response->getRpcResult()); |
|
44
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
45
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
46
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
47
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
public function testConcatAsyncRequest() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$id = 123; |
|
53
|
|
|
$method = 'concat'; |
|
54
|
|
|
$params = ['foo'=>'abc', 'bar'=>'def']; |
|
55
|
|
|
$request = $this->client->request($id, $method, $params); |
|
56
|
|
|
$promise = $this->client->sendAsync($request); |
|
57
|
|
|
|
|
58
|
|
|
$promise->then(function ($response) use ($request, $id, $method, $params) { |
|
59
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
60
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
61
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
62
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
65
|
|
|
$this->assertEquals(implode('', $params), $response->getRpcResult()); |
|
66
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
67
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
68
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
69
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
70
|
|
|
})->wait(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public function testSumRequest() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$id = 'abc'; |
|
76
|
|
|
$method = 'sum'; |
|
77
|
|
|
$params = ['foo'=>123, 'bar'=>456]; |
|
78
|
|
|
$request = $this->client->request($id, $method, $params); |
|
79
|
|
|
$response = $this->client->send($request); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
82
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
83
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
84
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
87
|
|
|
$this->assertEquals(array_sum($params), $response->getRpcResult()); |
|
88
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
89
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
90
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
91
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
public function testSumAsyncRequest() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$id = 'abc'; |
|
97
|
|
|
$method = 'sum'; |
|
98
|
|
|
$params = ['foo'=>123, 'bar'=>456]; |
|
99
|
|
|
$request = $this->client->request($id, $method, $params); |
|
100
|
|
|
$promise = $this->client->sendAsync($request); |
|
101
|
|
|
|
|
102
|
|
|
$promise->then(function ($response) use ($request, $id, $method, $params) { |
|
103
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
104
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
105
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
106
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
109
|
|
|
$this->assertEquals(array_sum($params), $response->getRpcResult()); |
|
110
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
111
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
112
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
113
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
114
|
|
|
})->wait(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
public function testFooRequest() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$id = 'abc'; |
|
120
|
|
|
$method = 'foo'; |
|
121
|
|
|
$request = $this->client->request($id, $method, []); |
|
122
|
|
|
$response = $this->client->send($request); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
125
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
126
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
127
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
130
|
|
|
$this->assertEquals('foo', $response->getRpcResult()); |
|
131
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
132
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
133
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
134
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
View Code Duplication |
public function testFooAsyncRequest() |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
$id = 'abc'; |
|
140
|
|
|
$method = 'foo'; |
|
141
|
|
|
$request = $this->client->request($id, $method, []); |
|
142
|
|
|
$promise = $this->client->sendAsync($request); |
|
143
|
|
|
|
|
144
|
|
|
$promise->then(function ($response) use ($request, $id, $method) { |
|
145
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
146
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
147
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
148
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
151
|
|
|
$this->assertEquals('foo', $response->getRpcResult()); |
|
152
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
153
|
|
|
$this->assertEquals(null, $response->getRpcErrorCode()); |
|
154
|
|
|
$this->assertEquals(null, $response->getRpcErrorMessage()); |
|
155
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
156
|
|
|
})->wait(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
View Code Duplication |
public function testBarRequest() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$id = 'abc'; |
|
162
|
|
|
$method = 'bar'; |
|
163
|
|
|
$request = $this->client->request($id, $method, []); |
|
164
|
|
|
$response = $this->client->send($request); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
167
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
168
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
169
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
172
|
|
|
$this->assertEquals(null, $response->getRpcResult()); |
|
173
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
174
|
|
|
$this->assertTrue(is_int($response->getRpcErrorCode())); |
|
175
|
|
|
$this->assertTrue(is_string($response->getRpcErrorMessage())); |
|
176
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
View Code Duplication |
public function testBarAsyncRequest() |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$id = 'abc'; |
|
182
|
|
|
$method = 'bar'; |
|
183
|
|
|
$request = $this->client->request($id, $method, []); |
|
184
|
|
|
$promise = $this->client->sendAsync($request); |
|
185
|
|
|
|
|
186
|
|
|
$promise->then(function ($response) use ($request, $id, $method) { |
|
187
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
188
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
189
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
190
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertEquals(ClientInterface::SPEC, $response->getRpcVersion()); |
|
193
|
|
|
$this->assertEquals(null, $response->getRpcResult()); |
|
194
|
|
|
$this->assertEquals($id, $response->getRpcId()); |
|
195
|
|
|
$this->assertTrue(is_int($response->getRpcErrorCode())); |
|
196
|
|
|
$this->assertTrue(is_string($response->getRpcErrorMessage())); |
|
197
|
|
|
$this->assertEquals(null, $response->getRpcErrorData()); |
|
198
|
|
|
})->wait(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @expectedException \Graze\GuzzleHttp\JsonRpc\Exception\ClientException |
|
203
|
|
|
*/ |
|
204
|
|
|
public function testBarRequestThrows() |
|
205
|
|
|
{ |
|
206
|
|
|
$id = 'abc'; |
|
207
|
|
|
$method = 'bar'; |
|
208
|
|
|
$client = $this->createClient(null, ['rpc_error' => true]); |
|
209
|
|
|
$request = $client->request($id, $method, []); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
212
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
213
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
214
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
215
|
|
|
|
|
216
|
|
|
$client->send($request); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function testBarAsyncRequestIsRejected() |
|
220
|
|
|
{ |
|
221
|
|
|
$id = 'abc'; |
|
222
|
|
|
$method = 'bar'; |
|
223
|
|
|
$client = $this->createClient(null, ['rpc_error' => true]); |
|
224
|
|
|
$request = $client->request($id, $method, []); |
|
225
|
|
|
$promise = $client->sendAsync($request); |
|
226
|
|
|
|
|
227
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
228
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
229
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
230
|
|
|
$this->assertEquals(null, $request->getRpcParams()); |
|
231
|
|
|
|
|
232
|
|
|
$promise->then(function () use ($request, $id, $method) { |
|
233
|
|
|
$this->fail('This promise should not be fulfilled'); |
|
234
|
|
|
}, function ($reason) { |
|
235
|
|
|
$this->assertInstanceOf(ClientException::class, $reason); |
|
236
|
|
|
})->wait(); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.