Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed May 17, 2024
1 parent 9acbbfe commit 1805e12
Showing 1 changed file with 94 additions and 7 deletions.
101 changes: 94 additions & 7 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ public function testDecodeJSONArray(){
$this->assertEquals([],Message::decodeJSON('[]'));
}

/**
* @return void
* @throws JSONRPCException
*/
public function testEncodeJSONString(){
$this->assertEquals('"abc"',Message::encodeJSON('abc'));
}

/**
* @return void
* @throws JSONRPCException
*/
public function testEncodeJSONObject(){
$this->assertEquals('{}',Message::encodeJSON((object) []));
}

/**
* @return void
* @throws JSONRPCException
*/
public function testEncodeJSONArray(){
$this->assertEquals('[]',Message::encodeJSON([]));
}

public function testIsBatch(){
$this->assertTrue(Message::isBatch([]));

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

/**
* @return void
* @throws JSONRPCException
*/
public function testParseResponseV1WithResultNullAndErrorNumber(){
$this->expectException(JSONRPCException::class);
$this->expectExceptionMessage('[V1] The "error" property in request MUST be an string, object or null.');

Message::parseObject((object) ['result'=>null,'error'=>12.34]);
}

/**
* @return void
* @throws JSONRPCException
Expand Down Expand Up @@ -300,13 +335,65 @@ public function testParseUnknownVersion(){
* @return void
* @throws JSONRPCException
*/
public function testMessages(){
$this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>[]],Message::createRequestMessageV1(123,'myMethod')->toObject());
$this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>["a",1,false,12.34]],Message::createRequestMessageV1(123,'myMethod',['a',1,false,12.34])->toObject());
$this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>[]],Message::createNotificationMessageV1('myMethod')->toObject());
$this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>["b",0,true,34.12]],Message::createNotificationMessageV1('myMethod',['b',0,true,34.12])->toObject());
$this->assertEquals((object) ["id"=>123,"result"=>"myResult","error"=>null],Message::createResponseMessageV1(123,'myResult')->toObject());
$this->assertEquals((object) ["id"=>123,"result"=>null,"error"=>"myError"],Message::createResponseMessageV1(123,null,'myError')->toObject());
public function testIsRequest(){
$this->assertTrue(Message::createRequestMessageV1(123,'myMethod')->isRequest());
$this->assertTrue(Message::createNotificationMessageV1('myMethod')->isRequest());
$this->assertFalse(Message::createResponseMessageV1(123,'myResult')->isRequest());
}

/**
* @return void
* @throws JSONRPCException
*/
public function testIsNotification(){
$this->assertFalse(Message::createRequestMessageV1(123,'myMethod')->isNotification());
$this->assertTrue(Message::createNotificationMessageV1('myMethod')->isNotification());
$this->assertFalse(Message::createResponseMessageV1(123,'myResult')->isNotification());
}

/**
* @return void
* @throws JSONRPCException
*/
public function testIsResponse(){
$this->assertFalse(Message::createRequestMessageV1(123,'myMethod')->isResponse());
$this->assertFalse(Message::createNotificationMessageV1('myMethod')->isResponse());
$this->assertTrue(Message::createResponseMessageV1(123,'myResult')->isResponse());
}

/**
* @return void
* @throws JSONRPCException
*/
public function testCreateResponseMessageV1WithResultAndError(){
$this->expectException(JSONRPCException::class);
$this->expectExceptionMessage('[V1] Only one property "result" or "error" can be non null.');

Message::createResponseMessageV1(123,'abc','def');
}

/**
* @return void
* @throws JSONRPCException
*/
public function testCreateResponseMessageV1WithErrorFalse(){
$this->expectException(JSONRPCException::class);
$this->expectExceptionMessage('[V1] The "error" property in request MUST be an string, object or null.');

Message::createResponseMessageV1(123,null,false);
}

// /**
// * @return void
// * @throws JSONRPCException
// */
// public function testMessages(){
// $this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>[]],Message::createRequestMessageV1(123,'myMethod')->toObject());
// $this->assertEquals((object) ["id"=>123,"method"=>"myMethod","params"=>["a",1,false,12.34]],Message::createRequestMessageV1(123,'myMethod',['a',1,false,12.34])->toObject());
// $this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>[]],Message::createNotificationMessageV1('myMethod')->toObject());
// $this->assertEquals((object) ["id"=>null,"method"=>"myMethod","params"=>["b",0,true,34.12]],Message::createNotificationMessageV1('myMethod',['b',0,true,34.12])->toObject());
// $this->assertEquals((object) ["id"=>123,"result"=>"myResult","error"=>null],Message::createResponseMessageV1(123,'myResult')->toObject());
// $this->assertEquals((object) ["id"=>123,"result"=>null,"error"=>"myError"],Message::createResponseMessageV1(123,null,'myError')->toObject());
// }

}

0 comments on commit 1805e12

Please sign in to comment.