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