|
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\ResponseInterface; |
|
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Test\FunctionalTestCase; |
|
17
|
|
|
|
|
18
|
|
|
class BatchFunctionalTest extends FunctionalTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Client */ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->client = $this->createClient(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testBatchRequestWithOneChild() |
|
29
|
|
|
{ |
|
30
|
|
|
$id = 'abc'; |
|
31
|
|
|
$method = 'sum'; |
|
32
|
|
|
$params = ['foo'=>123, 'bar'=>456]; |
|
33
|
|
|
$request = $this->client->request($id, $method, $params); |
|
34
|
|
|
$responses = $this->client->sendAll([$request]); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
37
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
38
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
39
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertTrue(is_array($responses)); |
|
42
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion()); |
|
43
|
|
|
$this->assertEquals(array_sum($params), $responses[0]->getRpcResult()); |
|
44
|
|
|
$this->assertEquals($id, $responses[0]->getRpcId()); |
|
45
|
|
|
$this->assertEquals(null, $responses[0]->getRpcErrorCode()); |
|
46
|
|
|
$this->assertEquals(null, $responses[0]->getRpcErrorMessage()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testAsyncBatchRequestWithOneChild() |
|
50
|
|
|
{ |
|
51
|
|
|
$id = 'abc'; |
|
52
|
|
|
$method = 'sum'; |
|
53
|
|
|
$params = ['foo' => 123, 'bar' => 456]; |
|
54
|
|
|
$request = $this->client->request($id, $method, $params); |
|
55
|
|
|
$promise = $this->client->sendAllAsync([$request]); |
|
56
|
|
|
|
|
57
|
|
|
$promise->then(function ($responses) use ($request, $id, $method, $params) { |
|
58
|
|
|
$this->assertEquals(ClientInterface::SPEC, $request->getRpcVersion()); |
|
59
|
|
|
$this->assertEquals($id, $request->getRpcId()); |
|
60
|
|
|
$this->assertEquals($method, $request->getRpcMethod()); |
|
61
|
|
|
$this->assertEquals($params, $request->getRpcParams()); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertTrue(is_array($responses)); |
|
64
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responses[0]->getRpcVersion()); |
|
65
|
|
|
$this->assertEquals(array_sum($params), $responses[0]->getRpcResult()); |
|
66
|
|
|
$this->assertEquals($id, $responses[0]->getRpcId()); |
|
67
|
|
|
$this->assertEquals(null, $responses[0]->getRpcErrorCode()); |
|
68
|
|
|
$this->assertEquals(null, $responses[0]->getRpcErrorMessage()); |
|
69
|
|
|
})->wait(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param ResponseInterface[] $responses |
|
74
|
|
|
* @param string|int $id |
|
75
|
|
|
* |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getResponseFromArray(array $responses = [], $id) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$filtered = array_values(array_filter($responses, function (ResponseInterface $response) use ($id) { |
|
81
|
|
|
return $response->getRpcId() == $id; |
|
82
|
|
|
})); |
|
83
|
|
|
|
|
84
|
|
|
if (count($filtered) === 0) { |
|
85
|
|
|
$this->fail('Unable to find response with id:' . $id); |
|
86
|
|
|
} |
|
87
|
|
|
return reset($filtered); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testBatchRequestWithMultipleChildren() |
|
91
|
|
|
{ |
|
92
|
|
|
$idA = 123; |
|
93
|
|
|
$idC = 'abc'; |
|
94
|
|
|
$idD = 'def'; |
|
95
|
|
|
$methodA = 'concat'; |
|
96
|
|
|
$methodB = 'nofify'; |
|
97
|
|
|
$methodC = 'sum'; |
|
98
|
|
|
$methodD = 'bar'; |
|
99
|
|
|
$paramsA = ['foo'=>'abc', 'bar'=>'def']; |
|
100
|
|
|
$paramsB = ['foo'=>false]; |
|
101
|
|
|
$paramsC = ['foo'=>123, 'bar'=>456]; |
|
102
|
|
|
$paramsD = ['foo'=>123, 'bar'=>456]; |
|
103
|
|
|
$requestA = $this->client->request($idA, $methodA, $paramsA); |
|
104
|
|
|
$requestB = $this->client->notification($methodB, $paramsB); |
|
105
|
|
|
$requestC = $this->client->request($idC, $methodC, $paramsC); |
|
106
|
|
|
$requestD = $this->client->request($idD, $methodD, $paramsD); |
|
107
|
|
|
$responses = $this->client->sendAll([$requestA, $requestB, $requestC, $requestD]); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion()); |
|
110
|
|
|
$this->assertEquals($idA, $requestA->getRpcId()); |
|
111
|
|
|
$this->assertEquals($methodA, $requestA->getRpcMethod()); |
|
112
|
|
|
$this->assertEquals($paramsA, $requestA->getRpcParams()); |
|
113
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion()); |
|
114
|
|
|
$this->assertEquals(null, $requestB->getRpcId()); |
|
115
|
|
|
$this->assertEquals($methodB, $requestB->getRpcMethod()); |
|
116
|
|
|
$this->assertEquals($paramsB, $requestB->getRpcParams()); |
|
117
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion()); |
|
118
|
|
|
$this->assertEquals($idC, $requestC->getRpcId()); |
|
119
|
|
|
$this->assertEquals($methodC, $requestC->getRpcMethod()); |
|
120
|
|
|
$this->assertEquals($paramsC, $requestC->getRpcParams()); |
|
121
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion()); |
|
122
|
|
|
$this->assertEquals($idD, $requestD->getRpcId()); |
|
123
|
|
|
$this->assertEquals($methodD, $requestD->getRpcMethod()); |
|
124
|
|
|
$this->assertEquals($paramsD, $requestD->getRpcParams()); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertTrue(is_array($responses)); |
|
127
|
|
|
$this->assertEquals(3, count($responses)); |
|
128
|
|
|
|
|
129
|
|
|
$responseA = $this->getResponseFromArray($responses, $idA); |
|
130
|
|
|
$responseC = $this->getResponseFromArray($responses, $idC); |
|
131
|
|
|
$responseD = $this->getResponseFromArray($responses, $idD); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion()); |
|
134
|
|
|
$this->assertEquals(implode('', $paramsA), $responseA->getRpcResult()); |
|
135
|
|
|
$this->assertEquals($idA, $responseA->getRpcId()); |
|
136
|
|
|
$this->assertEquals(null, $responseA->getRpcErrorCode()); |
|
137
|
|
|
$this->assertEquals(null, $responseA->getRpcErrorMessage()); |
|
138
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion()); |
|
139
|
|
|
$this->assertEquals(array_sum($paramsC), $responseC->getRpcResult()); |
|
140
|
|
|
$this->assertEquals($idC, $responseC->getRpcId()); |
|
141
|
|
|
$this->assertEquals(null, $responseC->getRpcErrorCode()); |
|
142
|
|
|
$this->assertEquals(null, $responseC->getRpcErrorMessage()); |
|
143
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion()); |
|
144
|
|
|
$this->assertEquals(null, $responseD->getRpcResult()); |
|
145
|
|
|
$this->assertEquals($idD, $responseD->getRpcId()); |
|
146
|
|
|
$this->assertTrue(is_int($responseD->getRpcErrorCode())); |
|
147
|
|
|
$this->assertTrue(is_string($responseD->getRpcErrorMessage())); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testAsyncBatchRequestWithMultipleChildren() |
|
151
|
|
|
{ |
|
152
|
|
|
$idA = 123; |
|
153
|
|
|
$idC = 'abc'; |
|
154
|
|
|
$idD = 'def'; |
|
155
|
|
|
$methodA = 'concat'; |
|
156
|
|
|
$methodB = 'nofify'; |
|
157
|
|
|
$methodC = 'sum'; |
|
158
|
|
|
$methodD = 'bar'; |
|
159
|
|
|
$paramsA = ['foo'=>'abc', 'bar'=>'def']; |
|
160
|
|
|
$paramsB = ['foo'=>false]; |
|
161
|
|
|
$paramsC = ['foo'=>123, 'bar'=>456]; |
|
162
|
|
|
$paramsD = ['foo'=>123, 'bar'=>456]; |
|
163
|
|
|
$requestA = $this->client->request($idA, $methodA, $paramsA); |
|
164
|
|
|
$requestB = $this->client->notification($methodB, $paramsB); |
|
165
|
|
|
$requestC = $this->client->request($idC, $methodC, $paramsC); |
|
166
|
|
|
$requestD = $this->client->request($idD, $methodD, $paramsD); |
|
167
|
|
|
$promise = $this->client->sendAllAsync([$requestA, $requestB, $requestC, $requestD]); |
|
168
|
|
|
|
|
169
|
|
|
$promise->then(function ($responses) use ($requestA, $requestB, $requestC, $requestD, $idA, $idC, $idD, $methodA, $methodB, $methodC, $methodD, $paramsA, $paramsB, $paramsC, $paramsD) { |
|
170
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestA->getRpcVersion()); |
|
171
|
|
|
$this->assertEquals($idA, $requestA->getRpcId()); |
|
172
|
|
|
$this->assertEquals($methodA, $requestA->getRpcMethod()); |
|
173
|
|
|
$this->assertEquals($paramsA, $requestA->getRpcParams()); |
|
174
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestB->getRpcVersion()); |
|
175
|
|
|
$this->assertEquals(null, $requestB->getRpcId()); |
|
176
|
|
|
$this->assertEquals($methodB, $requestB->getRpcMethod()); |
|
177
|
|
|
$this->assertEquals($paramsB, $requestB->getRpcParams()); |
|
178
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestC->getRpcVersion()); |
|
179
|
|
|
$this->assertEquals($idC, $requestC->getRpcId()); |
|
180
|
|
|
$this->assertEquals($methodC, $requestC->getRpcMethod()); |
|
181
|
|
|
$this->assertEquals($paramsC, $requestC->getRpcParams()); |
|
182
|
|
|
$this->assertEquals(ClientInterface::SPEC, $requestD->getRpcVersion()); |
|
183
|
|
|
$this->assertEquals($idD, $requestD->getRpcId()); |
|
184
|
|
|
$this->assertEquals($methodD, $requestD->getRpcMethod()); |
|
185
|
|
|
$this->assertEquals($paramsD, $requestD->getRpcParams()); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertTrue(is_array($responses)); |
|
188
|
|
|
$this->assertEquals(3, count($responses)); |
|
189
|
|
|
|
|
190
|
|
|
$responseA = $this->getResponseFromArray($responses, $idA); |
|
191
|
|
|
$responseC = $this->getResponseFromArray($responses, $idC); |
|
192
|
|
|
$responseD = $this->getResponseFromArray($responses, $idD); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseA->getRpcVersion()); |
|
195
|
|
|
$this->assertEquals(implode('', $paramsA), $responseA->getRpcResult()); |
|
196
|
|
|
$this->assertEquals($idA, $responseA->getRpcId()); |
|
197
|
|
|
$this->assertEquals(null, $responseA->getRpcErrorCode()); |
|
198
|
|
|
$this->assertEquals(null, $responseA->getRpcErrorMessage()); |
|
199
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseC->getRpcVersion()); |
|
200
|
|
|
$this->assertEquals(array_sum($paramsC), $responseC->getRpcResult()); |
|
201
|
|
|
$this->assertEquals($idC, $responseC->getRpcId()); |
|
202
|
|
|
$this->assertEquals(null, $responseC->getRpcErrorCode()); |
|
203
|
|
|
$this->assertEquals(null, $responseC->getRpcErrorMessage()); |
|
204
|
|
|
$this->assertEquals(ClientInterface::SPEC, $responseD->getRpcVersion()); |
|
205
|
|
|
$this->assertEquals(null, $responseD->getRpcResult()); |
|
206
|
|
|
$this->assertEquals($idD, $responseD->getRpcId()); |
|
207
|
|
|
$this->assertTrue(is_int($responseD->getRpcErrorCode())); |
|
208
|
|
|
$this->assertTrue(is_string($responseD->getRpcErrorMessage())); |
|
209
|
|
|
})->wait(); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: