1 | <?php |
||
10 | final class ResponseBodyValidatorTest extends TestCase |
||
11 | { |
||
12 | public function invalidResponseBodyProvider() |
||
13 | { |
||
14 | return [ |
||
15 | 'empty body' => [(object)null], |
||
16 | 'only version' => [(object)['jsonrpc' => '2.0']], |
||
17 | 'invalid version' => [(object)['jsonrpc' => '1.1']], |
||
18 | 'only result' => [(object)['result' => (object)['success' => true]]], |
||
19 | 'only id' => [(object)['id' => 1234]], |
||
20 | 'result and id' => [(object)['result' => (object)['success' => true], 'id' => 1234]], |
||
21 | 'result and id and invalid version' => [ |
||
22 | (object)[ |
||
23 | 'jsonrpc' => '1.1', |
||
24 | 'result' => (object)['success' => true], |
||
25 | 'id' => 1234, |
||
26 | ], |
||
27 | ], |
||
28 | 'both result and error present' => [ |
||
29 | (object)[ |
||
30 | 'jsonrpc' => '2.0', |
||
31 | 'result' => (object)['success' => true], |
||
32 | 'id' => 1234, |
||
33 | 'error' => (object)[ |
||
34 | 'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
||
35 | 'message' => 'Test error', |
||
36 | ], |
||
37 | ], |
||
38 | ], |
||
39 | 'error is not an object' => [ |
||
40 | (object)[ |
||
41 | 'jsonrpc' => '2.0', |
||
42 | 'id' => 1234, |
||
43 | 'error' => [ |
||
44 | JsonRpcErrorInterface::INTERNAL_ERROR, |
||
45 | 'Test error', |
||
46 | ], |
||
47 | ], |
||
48 | ], |
||
49 | 'no error code' => [ |
||
50 | (object)[ |
||
51 | 'jsonrpc' => '2.0', |
||
52 | 'id' => 1234, |
||
53 | 'error' => (object)[ |
||
54 | 'message' => 'Test error', |
||
55 | ], |
||
56 | ], |
||
57 | ], |
||
58 | 'code is not an int' => [ |
||
59 | (object)[ |
||
60 | 'jsonrpc' => '2.0', |
||
61 | 'id' => 1234, |
||
62 | 'error' => (object)[ |
||
63 | 'code' => 'string', |
||
64 | ], |
||
65 | ], |
||
66 | ], |
||
67 | 'no error message' => [ |
||
68 | (object)[ |
||
69 | 'jsonrpc' => '2.0', |
||
70 | 'id' => 1234, |
||
71 | 'error' => (object)[ |
||
72 | 'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
||
73 | ], |
||
74 | ], |
||
75 | ], |
||
76 | 'message is not a string' => [ |
||
77 | (object)[ |
||
78 | 'jsonrpc' => '2.0', |
||
79 | 'id' => 1234, |
||
80 | 'error' => (object)[ |
||
81 | 'code' => 1, |
||
82 | 'message' => [], |
||
83 | ], |
||
84 | ], |
||
85 | ], |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | public function validResponseBodyProvider() |
||
90 | { |
||
91 | return [ |
||
92 | 'valid response' => [(object)['jsonrpc' => '2.0', 'result' => (object)['success' => true], 'id' => 1234]], |
||
93 | 'valid empty response' => [(object)['jsonrpc' => '2.0', 'result' => null, 'id' => 1234]], |
||
94 | 'valid error' => [ |
||
95 | (object)[ |
||
96 | 'jsonrpc' => '2.0', |
||
97 | 'error' => (object)[ |
||
98 | 'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
||
99 | 'message' => 'Test error', |
||
100 | ], |
||
101 | 'id' => 1234, |
||
102 | ], |
||
103 | ], |
||
104 | 'valid error w\ data' => [ |
||
105 | (object)[ |
||
106 | 'jsonrpc' => '2.0', |
||
107 | 'error' => (object)[ |
||
108 | 'code' => JsonRpcErrorInterface::INTERNAL_ERROR, |
||
109 | 'message' => 'Test error', |
||
110 | 'data' => 'Test error data', |
||
111 | ], |
||
112 | 'id' => 1234, |
||
113 | ], |
||
114 | ], |
||
115 | ]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param \stdClass $body |
||
120 | * |
||
121 | * @dataProvider invalidResponseBodyProvider |
||
122 | * @expectedException \ScayTrase\Api\JsonRpc\Exception\ResponseParseException |
||
123 | */ |
||
124 | public function testInvalidBody(\stdClass $body) |
||
128 | |||
129 | /** |
||
130 | * @param \stdClass $body |
||
131 | * |
||
132 | * @dataProvider validResponseBodyProvider |
||
133 | */ |
||
134 | public function testValidBody(\stdClass $body) |
||
140 | } |
||
141 |