8
8
9
9
class MessageTest extends TestCase{
10
10
11
+ public function testDecodeEmptyJSON (){
12
+ $ this ->expectException (JSONRPCException::class);
13
+ $ this ->expectExceptionMessage ('Failed to decode JSON. ' );
14
+
15
+ Message::decodeJSON ('' );
16
+ }
17
+
18
+ /**
19
+ * @return void
20
+ * @throws JSONRPCException
21
+ */
22
+ public function testDecodeJSONString (){
23
+ $ this ->assertEquals ('abc ' ,Message::decodeJSON ('"abc" ' ));
24
+ }
25
+
26
+ /**
27
+ * @return void
28
+ * @throws JSONRPCException
29
+ */
30
+ public function testDecodeJSONObject (){
31
+ $ this ->assertEquals ((object ) [],Message::decodeJSON ('{} ' ));
32
+ }
33
+
34
+ /**
35
+ * @return void
36
+ * @throws JSONRPCException
37
+ */
38
+ public function testDecodeJSONArray (){
39
+ $ this ->assertEquals ([],Message::decodeJSON ('[] ' ));
40
+ }
41
+
42
+ public function testIsBatch (){
43
+ $ this ->assertTrue (Message::isBatch ([]));
44
+
45
+ $ this ->assertFalse (Message::isBatch ('abc ' ));
46
+ $ this ->assertFalse (Message::isBatch (true ));
47
+ $ this ->assertFalse (Message::isBatch (false ));
48
+ $ this ->assertFalse (Message::isBatch (123 ));
49
+ $ this ->assertFalse (Message::isBatch (123.456 ));
50
+ $ this ->assertFalse (Message::isBatch ((object ) []));
51
+ $ this ->assertFalse (Message::isBatch (null ));
52
+ }
53
+
54
+ /**
55
+ * @return void
56
+ * @throws JSONRPCException
57
+ */
58
+ public function testParseObjectString (){
59
+ $ this ->expectException (JSONRPCException::class);
60
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
61
+
62
+ Message::parseObject ('abc ' );
63
+ }
64
+
65
+ /**
66
+ * @return void
67
+ * @throws JSONRPCException
68
+ */
69
+ public function testParseObjectTrue (){
70
+ $ this ->expectException (JSONRPCException::class);
71
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
72
+
73
+ Message::parseObject (true );
74
+ }
75
+
76
+ /**
77
+ * @return void
78
+ * @throws JSONRPCException
79
+ */
80
+ public function testParseObjectFalse (){
81
+ $ this ->expectException (JSONRPCException::class);
82
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
83
+
84
+ Message::parseObject (false );
85
+ }
86
+
87
+ /**
88
+ * @return void
89
+ * @throws JSONRPCException
90
+ */
91
+ public function testParseObjectInteger (){
92
+ $ this ->expectException (JSONRPCException::class);
93
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
94
+
95
+ Message::parseObject (123 );
96
+ }
97
+
98
+ /**
99
+ * @return void
100
+ * @throws JSONRPCException
101
+ */
102
+ public function testParseObjectFloat (){
103
+ $ this ->expectException (JSONRPCException::class);
104
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
105
+
106
+ Message::parseObject (123.456 );
107
+ }
108
+
109
+ /**
110
+ * @return void
111
+ * @throws JSONRPCException
112
+ */
113
+ public function testParseObjectArray (){
114
+ $ this ->expectException (JSONRPCException::class);
115
+ $ this ->expectExceptionMessage ('A message MUST be a JSON object. ' );
116
+
117
+ Message::parseObject ([]);
118
+ }
119
+
120
+ /**
121
+ * @return void
122
+ * @throws JSONRPCException
123
+ */
124
+ public function testParseEmptyObject (){
125
+ $ this ->expectException (JSONRPCException::class);
126
+ $ this ->expectExceptionMessage ('[V1] Unknown message type. ' );
127
+
128
+ Message::parseObject ((object ) []);
129
+ }
130
+
131
+ /**
132
+ * @return void
133
+ * @throws JSONRPCException
134
+ */
135
+ public function testParseVersion2 (){
136
+ $ this ->expectException (JSONRPCException::class);
137
+ $ this ->expectExceptionMessage ('[V2] Unknown message type. ' );
138
+
139
+ Message::parseObject ((object ) [
140
+ 'jsonrpc ' => '2.0 ' ,
141
+ ]);
142
+ }
143
+
144
+ /**
145
+ * @return void
146
+ * @throws JSONRPCException
147
+ */
148
+ public function testParseUnknownVersion (){
149
+ $ this ->expectException (JSONRPCException::class);
150
+ $ this ->expectExceptionMessage ('Unknown version "1.5". ' );
151
+
152
+ Message::parseObject ((object ) [
153
+ 'jsonrpc ' => '1.5 ' ,
154
+ ]);
155
+ }
156
+
11
157
/**
12
158
* @return void
13
159
* @throws JSONRPCException
14
160
*/
15
161
public function testMessages (){
16
- $ this ->assertEquals ('{"id":123,"method":"myMethod","params":[]} ' ,Message::createRequestMessageV1 (123 ,'myMethod ' )->toJSON ());
17
- $ this ->assertEquals ('{"id":null,"method":"myMethod","params":[]} ' ,Message::createNotificationMessageV1 ('myMethod ' )->toJSON ());
18
- $ this ->assertEquals ('{"id":123,"result":"myResult","error":null} ' ,Message::createResponseMessageV1 (123 ,'myResult ' )->toJSON ());
19
- $ this ->assertEquals ('{"id":123,"result":null,"error":"myError"} ' ,Message::createResponseMessageV1 (123 ,null ,'myError ' )->toJSON ());
162
+ $ this ->assertEquals ((object ) ["id " =>123 ,"method " =>"myMethod " ,"params " =>[]],Message::createRequestMessageV1 (123 ,'myMethod ' )->toObject ());
163
+ $ this ->assertEquals ((object ) ["id " =>123 ,"method " =>"myMethod " ,"params " =>["a " ,1 ,false ,12.34 ]],Message::createRequestMessageV1 (123 ,'myMethod ' ,['a ' ,1 ,false ,12.34 ])->toObject ());
164
+ $ this ->assertEquals ((object ) ["id " =>null ,"method " =>"myMethod " ,"params " =>[]],Message::createNotificationMessageV1 ('myMethod ' )->toObject ());
165
+ $ this ->assertEquals ((object ) ["id " =>null ,"method " =>"myMethod " ,"params " =>["b " ,0 ,true ,34.12 ]],Message::createNotificationMessageV1 ('myMethod ' ,['b ' ,0 ,true ,34.12 ])->toObject ());
166
+ $ this ->assertEquals ((object ) ["id " =>123 ,"result " =>"myResult " ,"error " =>null ],Message::createResponseMessageV1 (123 ,'myResult ' )->toObject ());
167
+ $ this ->assertEquals ((object ) ["id " =>123 ,"result " =>null ,"error " =>"myError " ],Message::createResponseMessageV1 (123 ,null ,'myError ' )->toObject ());
20
168
}
21
169
22
170
}
0 commit comments