Skip to content

Commit 1805e12

Browse files
committed
Add more tests
1 parent 9acbbfe commit 1805e12

File tree

1 file changed

+94
-7
lines changed

1 file changed

+94
-7
lines changed

tests/MessageTest.php

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ public function testDecodeJSONArray(){
4242
$this->assertEquals([],Message::decodeJSON('[]'));
4343
}
4444

45+
/**
46+
* @return void
47+
* @throws JSONRPCException
48+
*/
49+
public function testEncodeJSONString(){
50+
$this->assertEquals('"abc"',Message::encodeJSON('abc'));
51+
}
52+
53+
/**
54+
* @return void
55+
* @throws JSONRPCException
56+
*/
57+
public function testEncodeJSONObject(){
58+
$this->assertEquals('{}',Message::encodeJSON((object) []));
59+
}
60+
61+
/**
62+
* @return void
63+
* @throws JSONRPCException
64+
*/
65+
public function testEncodeJSONArray(){
66+
$this->assertEquals('[]',Message::encodeJSON([]));
67+
}
68+
4569
public function testIsBatch(){
4670
$this->assertTrue(Message::isBatch([]));
4771

@@ -234,6 +258,17 @@ public function testParseResponseV1WithResultAndError(){
234258
Message::parseObject((object) ['result'=>'abc','error'=>'def']);
235259
}
236260

261+
/**
262+
* @return void
263+
* @throws JSONRPCException
264+
*/
265+
public function testParseResponseV1WithResultNullAndErrorNumber(){
266+
$this->expectException(JSONRPCException::class);
267+
$this->expectExceptionMessage('[V1] The "error" property in request MUST be an string, object or null.');
268+
269+
Message::parseObject((object) ['result'=>null,'error'=>12.34]);
270+
}
271+
237272
/**
238273
* @return void
239274
* @throws JSONRPCException
@@ -300,13 +335,65 @@ public function testParseUnknownVersion(){
300335
* @return void
301336
* @throws JSONRPCException
302337
*/
303-
public function testMessages(){
304-
$this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>[]],Message::createRequestMessageV1(123,'myMethod')->toObject());
305-
$this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>["a",1,false,12.34]],Message::createRequestMessageV1(123,'myMethod',['a',1,false,12.34])->toObject());
306-
$this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>[]],Message::createNotificationMessageV1('myMethod')->toObject());
307-
$this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>["b",0,true,34.12]],Message::createNotificationMessageV1('myMethod',['b',0,true,34.12])->toObject());
308-
$this->assertEquals((object) ["id"=>123,"result"=>"myResult","error"=>null],Message::createResponseMessageV1(123,'myResult')->toObject());
309-
$this->assertEquals((object) ["id"=>123,"result"=>null,"error"=>"myError"],Message::createResponseMessageV1(123,null,'myError')->toObject());
338+
public function testIsRequest(){
339+
$this->assertTrue(Message::createRequestMessageV1(123,'myMethod')->isRequest());
340+
$this->assertTrue(Message::createNotificationMessageV1('myMethod')->isRequest());
341+
$this->assertFalse(Message::createResponseMessageV1(123,'myResult')->isRequest());
342+
}
343+
344+
/**
345+
* @return void
346+
* @throws JSONRPCException
347+
*/
348+
public function testIsNotification(){
349+
$this->assertFalse(Message::createRequestMessageV1(123,'myMethod')->isNotification());
350+
$this->assertTrue(Message::createNotificationMessageV1('myMethod')->isNotification());
351+
$this->assertFalse(Message::createResponseMessageV1(123,'myResult')->isNotification());
352+
}
353+
354+
/**
355+
* @return void
356+
* @throws JSONRPCException
357+
*/
358+
public function testIsResponse(){
359+
$this->assertFalse(Message::createRequestMessageV1(123,'myMethod')->isResponse());
360+
$this->assertFalse(Message::createNotificationMessageV1('myMethod')->isResponse());
361+
$this->assertTrue(Message::createResponseMessageV1(123,'myResult')->isResponse());
362+
}
363+
364+
/**
365+
* @return void
366+
* @throws JSONRPCException
367+
*/
368+
public function testCreateResponseMessageV1WithResultAndError(){
369+
$this->expectException(JSONRPCException::class);
370+
$this->expectExceptionMessage('[V1] Only one property "result" or "error" can be non null.');
371+
372+
Message::createResponseMessageV1(123,'abc','def');
373+
}
374+
375+
/**
376+
* @return void
377+
* @throws JSONRPCException
378+
*/
379+
public function testCreateResponseMessageV1WithErrorFalse(){
380+
$this->expectException(JSONRPCException::class);
381+
$this->expectExceptionMessage('[V1] The "error" property in request MUST be an string, object or null.');
382+
383+
Message::createResponseMessageV1(123,null,false);
310384
}
311385

386+
// /**
387+
// * @return void
388+
// * @throws JSONRPCException
389+
// */
390+
// public function testMessages(){
391+
// $this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>[]],Message::createRequestMessageV1(123,'myMethod')->toObject());
392+
// $this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>["a",1,false,12.34]],Message::createRequestMessageV1(123,'myMethod',['a',1,false,12.34])->toObject());
393+
// $this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>[]],Message::createNotificationMessageV1('myMethod')->toObject());
394+
// $this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>["b",0,true,34.12]],Message::createNotificationMessageV1('myMethod',['b',0,true,34.12])->toObject());
395+
// $this->assertEquals((object) ["id"=>123,"result"=>"myResult","error"=>null],Message::createResponseMessageV1(123,'myResult')->toObject());
396+
// $this->assertEquals((object) ["id"=>123,"result"=>null,"error"=>"myError"],Message::createResponseMessageV1(123,null,'myError')->toObject());
397+
// }
398+
312399
}

0 commit comments

Comments
 (0)