|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\JsonRpc\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcClient; |
|
7
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcErrorInterface; |
|
8
|
|
|
use ScayTrase\Api\JsonRpc\ResponseBodyValidator; |
|
9
|
|
|
use ScayTrase\Api\JsonRpc\SyncResponse; |
|
10
|
|
|
|
|
11
|
|
|
class ResponseBodyValidatorTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
1 |
|
public function invalidResponseBodyProvider() |
|
14
|
|
|
{ |
|
15
|
|
|
return [ |
|
16
|
|
|
'empty body' => [(object)null], |
|
17
|
|
|
'only version' => [(object)['jsonrpc' => '2.0']], |
|
18
|
|
|
'invalid version' => [(object)['jsonrpc' => '1.1']], |
|
19
|
|
|
'only result' => [(object)['result' => (object)['success' => true]]], |
|
20
|
|
|
'only id' => [(object)['id' => 1234]], |
|
21
|
1 |
|
'result and id' => [(object)['result' => (object)['success' => true], 'id' => 1234]], |
|
22
|
|
|
'result and id and invalid version' => [ |
|
23
|
|
|
(object)[ |
|
24
|
|
|
'jsonrpc' => '1.1', |
|
25
|
|
|
'result' => (object)['success' => true], |
|
26
|
|
|
'id' => 1234, |
|
27
|
|
|
], |
|
28
|
|
|
], |
|
29
|
|
|
'both result and error present' => [ |
|
30
|
|
|
(object)[ |
|
31
|
|
|
'jsonrpc' => '2.0', |
|
32
|
|
|
'result' => (object)['success' => true], |
|
33
|
|
|
'id' => 1234, |
|
34
|
|
|
'error' => (object)[ |
|
35
|
|
|
'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
|
36
|
|
|
'message' => 'Test error', |
|
37
|
|
|
], |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function validResponseBodyProvider() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
'valid response' => [(object)['jsonrpc' => '2.0', 'result' => ['success' => true], 'id' => 1234]], |
|
47
|
|
|
'valid empty response' => [(object)['jsonrpc' => '2.0', 'result' => null, 'id' => 1234]], |
|
48
|
|
|
'valid error' => [ |
|
49
|
|
|
(object)[ |
|
50
|
|
|
'jsonrpc' => '2.0', |
|
51
|
|
|
'error' => (object)[ |
|
52
|
|
|
'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
|
53
|
|
|
'message' => 'Test error', |
|
54
|
|
|
], |
|
55
|
|
|
'id' => 1234, |
|
56
|
|
|
], |
|
57
|
|
|
], |
|
58
|
|
|
'valid error w\ data' => [ |
|
59
|
|
|
(object)[ |
|
60
|
|
|
'jsonrpc' => '2.0', |
|
61
|
|
|
'error' => (object)[ |
|
62
|
|
|
'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
|
63
|
|
|
'message' => 'Test error', |
|
64
|
|
|
'data' => 'Test error data', |
|
65
|
|
|
], |
|
66
|
|
|
'id' => 1234, |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \stdClass $body |
|
74
|
|
|
* |
|
75
|
|
|
* @dataProvider invalidResponseBodyProvider |
|
76
|
|
|
* @expectedException \ScayTrase\Api\JsonRpc\Exception\ResponseParseException |
|
77
|
|
|
*/ |
|
78
|
8 |
|
public function testInvalidBody(\stdClass $body) |
|
79
|
|
|
{ |
|
80
|
8 |
|
$parser = new ResponseBodyValidator(); |
|
81
|
8 |
|
$parser->validate($body); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \stdClass $body |
|
86
|
|
|
* |
|
87
|
|
|
* @dataProvider validResponseBodyProvider |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function testValidBody(\stdClass $body) |
|
90
|
|
|
{ |
|
91
|
4 |
|
self::assertEquals(JsonRpcClient::VERSION, (new SyncResponse($body))->getVersion()); |
|
92
|
4 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|