1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\JsonRpc\Test\Tests; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\JsonRpc\JsonRpcBundle; |
6
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcError; |
7
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface; |
8
|
|
|
use ScayTrase\Api\JsonRpc\SyncResponse; |
9
|
|
|
|
10
|
|
|
class ExceptionHandlingTest extends JsonRpcTestCase |
11
|
|
|
{ |
12
|
|
|
public function testBatchWithFailingMethod() |
13
|
|
|
{ |
14
|
|
|
$client = self::createClient(); |
15
|
|
|
|
16
|
|
|
$response = $this->sendRequest( |
17
|
|
|
$client, |
18
|
|
|
'/test/', |
19
|
|
|
[ |
20
|
|
|
[ |
21
|
|
|
'jsonrpc' => JsonRpcBundle::VERSION, |
22
|
|
|
'method' => 'sample', |
23
|
|
|
'id' => 'test', |
24
|
|
|
'params' => [ |
25
|
|
|
'param1' => 'value1', |
26
|
|
|
'param2' => 100500, |
27
|
|
|
], |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'jsonrpc' => JsonRpcBundle::VERSION, |
31
|
|
|
'method' => 'exception', |
32
|
|
|
'id' => 'test2', |
33
|
|
|
'params' => [ |
34
|
|
|
'param1' => 'value1', |
35
|
|
|
'param2' => 100500, |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
[ |
39
|
|
|
//notification - no id |
40
|
|
|
'jsonrpc' => JsonRpcBundle::VERSION, |
41
|
|
|
'method' => 'notification', |
42
|
|
|
'param' => [ |
43
|
|
|
'notification' => 'message', |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
self::assertTrue($response->isSuccessful(), $response->getContent()); |
50
|
|
|
$body = json_decode($response->getContent()); |
51
|
|
|
self::assertTrue(is_array($body)); |
52
|
|
|
self::assertCount(2, $body, json_encode($body, JSON_PRETTY_PRINT)); |
53
|
|
|
|
54
|
|
|
/** @var JsonRpcResponseInterface[] $jsonResponses */ |
55
|
|
|
$jsonResponses = []; |
56
|
|
|
foreach ($body as $responseBody) { |
57
|
|
|
$jsonResponses[] = new SyncResponse($responseBody); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
self::assertTrue($jsonResponses[0]->isSuccessful()); |
61
|
|
|
self::assertFalse($jsonResponses[1]->isSuccessful()); |
62
|
|
|
self::assertEquals(JsonRpcError::INTERNAL_ERROR, $jsonResponses[1]->getError()->getCode()); |
63
|
|
|
self::assertEquals('This is the malfunction controller', $jsonResponses[1]->getError()->getMessage()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getInvalidRequests() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'No method' => [ |
70
|
|
|
[ |
71
|
|
|
'jsonrpc' => JsonRpcBundle::VERSION, |
72
|
|
|
'id' => 'test', |
73
|
|
|
'params' => [], |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
'No version' => [ |
77
|
|
|
[ |
78
|
|
|
'id' => 'test', |
79
|
|
|
'method' => 'sample', |
80
|
|
|
'params' => [], |
81
|
|
|
], |
82
|
|
|
], |
83
|
|
|
'No id, no method' => [ |
84
|
|
|
[ |
85
|
|
|
'id' => 'test', |
86
|
|
|
'params' => [], |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @dataProvider getInvalidRequests |
94
|
|
|
* |
95
|
|
|
* @param array $request |
96
|
|
|
* |
97
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
98
|
|
|
* @expectedExceptionMessageRegExp |^Invalid JSONRPC 2.0 Request. Missing fields .+$| |
99
|
|
|
*/ |
100
|
|
|
public function testInvalidJsonRpcResultsIn400Response(array $request) |
101
|
|
|
{ |
102
|
|
|
$client = self::createClient(); |
103
|
|
|
|
104
|
|
|
$this->sendRequest( |
105
|
|
|
$client, |
106
|
|
|
'/test/', |
107
|
|
|
$request |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|