1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\JsonRpc\Tests; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\JsonRpc\Controller\JsonRpcController; |
6
|
|
|
use Bankiru\Api\JsonRpc\Http\JsonRpcHttpResponse; |
7
|
|
|
use Bankiru\Api\JsonRpc\Specification\JsonRpcResponse; |
8
|
|
|
use Bankiru\Api\Rpc\Event\FilterControllerEvent; |
9
|
|
|
use Bankiru\Api\Rpc\Event\FilterResponseEvent; |
10
|
|
|
use Bankiru\Api\Rpc\Event\FinishRequestEvent; |
11
|
|
|
use Bankiru\Api\Rpc\Event\GetExceptionResponseEvent; |
12
|
|
|
use Bankiru\Api\Rpc\Event\GetResponseEvent; |
13
|
|
|
use Bankiru\Api\Rpc\Routing\ControllerResolver\ControllerResolverInterface; |
14
|
|
|
use Bankiru\Api\Rpc\RpcEvents; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcError; |
18
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface; |
19
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
24
|
|
|
|
25
|
|
|
final class JsonRpcControllerTest extends TestCase |
26
|
|
|
{ |
27
|
1 |
View Code Duplication |
public function testEmptyBatchRequestHandling() |
|
|
|
|
28
|
|
|
{ |
29
|
1 |
|
$controller = $this->createController(); |
30
|
|
|
|
31
|
1 |
|
$request = $this->createJsonRequest('/', []); |
32
|
1 |
|
$response = $controller->jsonRpcAction($request); |
33
|
|
|
|
34
|
1 |
|
self::assertInstanceOf(JsonRpcHttpResponse::class, $response); |
35
|
1 |
|
self::assertTrue($response->isSuccessful()); |
36
|
1 |
|
self::assertEquals('[]', $response->getContent()); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function getInvalidJsonRequests() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'empty' => [null], |
43
|
|
|
'string' => ['test'], |
44
|
1 |
|
'invalid_json' => [substr(json_encode(['test']), 2)], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider getInvalidJsonRequests |
50
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
51
|
|
|
* |
52
|
|
|
* @param $content |
53
|
|
|
*/ |
54
|
3 |
|
public function testInvalidJsonHandling($content) |
55
|
|
|
{ |
56
|
3 |
|
$controller = $this->createController(); |
57
|
|
|
|
58
|
3 |
|
$request = $this->createJsonRequest('/', $content); |
59
|
3 |
|
$controller->jsonRpcAction($request); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getInvalidJsonRpcRequests() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'invalid version' => [['jsonrpc' => '1.0']], |
66
|
|
|
'no method' => [['jsonrpc' => '2.0']], |
67
|
|
|
'no version' => [['method' => 'test']], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider getInvalidJsonRpcRequests |
73
|
|
|
* @expectedException \Bankiru\Api\JsonRpc\Exception\InvalidRequestException |
74
|
|
|
* |
75
|
|
|
* @param $content |
76
|
|
|
*/ |
77
|
4 |
|
public function testInvalidJsonRpcHandling($content) |
78
|
|
|
{ |
79
|
4 |
|
$controller = $this->createController(); |
80
|
|
|
|
81
|
3 |
|
$request = $this->createJsonRequest('/', $content); |
82
|
3 |
|
$controller->jsonRpcAction($request); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getValidResponseAndRequests() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'single' => [ |
89
|
|
|
[ |
90
|
|
|
'jsonrpc' => '2.0', |
91
|
|
|
'id' => 'test', |
92
|
|
|
'method' => 'test', |
93
|
|
|
], |
94
|
|
|
'{"jsonrpc":"2.0","id":"test","result":{"success":true}}', |
95
|
|
|
], |
96
|
|
|
'batch' => [ |
97
|
|
|
[ |
98
|
|
|
[ |
99
|
|
|
'jsonrpc' => '2.0', |
100
|
|
|
'id' => 'test', |
101
|
|
|
'method' => 'test', |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
'[{"jsonrpc":"2.0","id":"test","result":{"success":true}}]', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider getValidResponseAndRequests |
111
|
|
|
* |
112
|
|
|
* @param array $content |
113
|
|
|
* @param string $responseBody |
114
|
|
|
*/ |
115
|
2 |
View Code Duplication |
public function testValidRequestHandling($content, $responseBody) |
|
|
|
|
116
|
|
|
{ |
117
|
2 |
|
$controller = $this->createController(); |
118
|
2 |
|
$request = $this->createJsonRequest( |
119
|
2 |
|
'/', |
120
|
|
|
$content |
121
|
2 |
|
); |
122
|
2 |
|
$response = $controller->jsonRpcAction($request); |
123
|
|
|
|
124
|
2 |
|
self::assertTrue($response->isSuccessful()); |
125
|
2 |
|
self::assertEquals($responseBody, $response->getContent()); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
1 |
|
public function testExceptionHandling() |
129
|
|
|
{ |
130
|
1 |
|
$controller = $this->createController(); |
131
|
1 |
|
$request = $this->createJsonRequest( |
132
|
1 |
|
'/', |
133
|
|
|
[ |
134
|
1 |
|
'jsonrpc' => '2.0', |
135
|
1 |
|
'id' => 'test', |
136
|
1 |
|
'method' => 'exception', |
137
|
|
|
] |
138
|
1 |
|
); |
139
|
1 |
|
$response = $controller->jsonRpcAction($request); |
140
|
|
|
|
141
|
1 |
|
self::assertTrue($response->isSuccessful()); |
142
|
1 |
|
self::assertEquals( |
143
|
1 |
|
'{"jsonrpc":"2.0","id":"test","error":{"code":-32603,"message":"Failure!","data":null}}', |
144
|
1 |
|
$response->getContent() |
145
|
1 |
|
); |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
public function testExceptionHandlingAmongBatchRequest() |
149
|
|
|
{ |
150
|
1 |
|
$controller = $this->createController(); |
151
|
1 |
|
$request = $this->createJsonRequest( |
152
|
1 |
|
'/', |
153
|
|
|
[ |
154
|
|
|
[ |
155
|
1 |
|
'jsonrpc' => '2.0', |
156
|
1 |
|
'id' => 'test1', |
157
|
1 |
|
'method' => 'test', |
158
|
1 |
|
], |
159
|
|
|
[ |
160
|
1 |
|
'jsonrpc' => '2.0', |
161
|
1 |
|
'id' => 'test2', |
162
|
1 |
|
'method' => 'exception', |
163
|
1 |
|
], |
164
|
|
|
] |
165
|
1 |
|
); |
166
|
1 |
|
$response = $controller->jsonRpcAction($request); |
167
|
|
|
|
168
|
1 |
|
self::assertTrue($response->isSuccessful()); |
169
|
1 |
|
self::assertEquals( |
170
|
1 |
|
'[{"jsonrpc":"2.0","id":"test1","result":{"success":true}},{"jsonrpc":"2.0","id":"test2","error":{"code":-32603,"message":"Failure!","data":null}}]', |
171
|
1 |
|
$response->getContent() |
172
|
1 |
|
); |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $uri |
177
|
|
|
* @param mixed $content |
178
|
|
|
* |
179
|
|
|
* @return Request |
180
|
|
|
*/ |
181
|
11 |
|
private function createJsonRequest($uri, $content) |
182
|
|
|
{ |
183
|
11 |
|
return Request::create($uri, 'POST', [], [], [], [], json_encode($content)); |
184
|
|
|
} |
185
|
|
|
|
186
|
11 |
|
private function getContainerMock() |
187
|
|
|
{ |
188
|
11 |
|
$mock = $this->prophesize(ContainerInterface::class); |
189
|
11 |
|
$kernel = $this->prophesize(KernelInterface::class); |
190
|
11 |
|
$resolver = $this->prophesize(ControllerResolverInterface::class); |
191
|
11 |
|
$resolver->getController(Argument::type(RpcRequestInterface::class))->willReturn( |
192
|
|
|
function (JsonRpcRequestInterface $request) { |
193
|
4 |
|
if ($request->getMethod() === 'exception') { |
194
|
2 |
|
throw new \LogicException('Failure!'); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
return new JsonRpcResponse($request->getId(), (object)['success' => true]); |
198
|
|
|
} |
199
|
11 |
|
); |
200
|
11 |
|
$resolver->getArguments(Argument::type(RpcRequestInterface::class), Argument::any())->will( |
201
|
|
|
function (array $args) { |
202
|
|
|
return [ |
203
|
4 |
|
$args[0], |
204
|
4 |
|
]; |
205
|
|
|
} |
206
|
11 |
|
); |
207
|
|
|
|
208
|
11 |
|
$evm = $this->prophesize(EventDispatcherInterface::class); |
209
|
11 |
|
$evm->dispatch(Argument::exact(RpcEvents::EXCEPTION), Argument::type(GetExceptionResponseEvent::class)) |
210
|
11 |
|
->will( |
211
|
2 |
|
function ($args) { |
212
|
|
|
/** @var GetExceptionResponseEvent $event */ |
213
|
2 |
|
$event = $args[1]; |
214
|
|
|
|
215
|
|
|
/** @var JsonRpcRequestInterface $request */ |
216
|
2 |
|
$request = $event->getRequest(); |
217
|
|
|
|
218
|
2 |
|
$event->setResponse( |
219
|
2 |
|
new JsonRpcResponse( |
220
|
2 |
|
$request->getId(), |
221
|
2 |
|
null, new JsonRpcError( |
222
|
2 |
|
JsonRpcError::INTERNAL_ERROR, |
223
|
2 |
|
$event->getException()->getMessage() |
224
|
2 |
|
) |
225
|
2 |
|
) |
226
|
2 |
|
); |
227
|
2 |
|
} |
228
|
11 |
|
); |
229
|
11 |
|
$evm->dispatch(Argument::exact(RpcEvents::FINISH_REQUEST), Argument::type(FinishRequestEvent::class)) |
230
|
11 |
|
->willReturn(null); |
231
|
11 |
|
$evm->dispatch(Argument::exact(RpcEvents::CONTROLLER), Argument::type(FilterControllerEvent::class)) |
232
|
11 |
|
->willReturn(null); |
233
|
11 |
|
$evm->dispatch(Argument::exact(RpcEvents::REQUEST), Argument::type(GetResponseEvent::class))->willReturn(null); |
234
|
11 |
|
$evm->dispatch(Argument::exact(RpcEvents::RESPONSE), Argument::type(FilterResponseEvent::class)) |
235
|
11 |
|
->willReturn(null); |
236
|
|
|
|
237
|
11 |
|
$mock->get(Argument::exact('jsonrpc_server.controller_resolver'))->willReturn($resolver->reveal()); |
238
|
11 |
|
$mock->get(Argument::exact('event_dispatcher'))->willReturn($evm->reveal()); |
239
|
11 |
|
$mock->get(Argument::exact('kernel'))->willReturn($kernel->reveal()); |
240
|
|
|
|
241
|
11 |
|
return $mock->reveal(); |
242
|
1 |
|
} |
243
|
|
|
|
244
|
11 |
|
private function createController() |
245
|
|
|
{ |
246
|
11 |
|
$controller = new JsonRpcController(); |
247
|
11 |
|
$controller->setContainer($this->getContainerMock()); |
248
|
|
|
|
249
|
11 |
|
return $controller; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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.