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