Skip to content

Commit 9acbbfe

Browse files
committed
Fix lazy boolean evaluation
1 parent 7d841b5 commit 9acbbfe

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use YOCLIB\JSONRPC\Message;
2121

2222
$message = Message::createRequestMessageV1(123,'getInfo',['payments']); // Create request (version 1.0)
2323
$message = Message::createNotificationMessageV1('notificationEvent',['payed']); // Create notification (version 1.0)
24-
$message = Message::createResponseMessageV1(123,['payments'=>[]]); // Create response (version 1.0)
24+
$message = Message::createResponseMessageV1(123,['payments'=>['$10.12','$23.45','$12.34']]); // Create response (version 1.0)
2525

2626
$object = $message->toObject();
2727

src/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private static function validateParamsPropertyV1($message){
194194
if(!property_exists($message,'params')){
195195
throw new JSONRPCException('[V1] Missing "params" property in request.');
196196
}
197-
if(!is_array($message['params'])){
197+
if(!is_array($message->params)){
198198
throw new JSONRPCException('[V1] The "params" property in request MUST be an array.');
199199
}
200200
}
@@ -235,7 +235,7 @@ private static function handleMessageV1($message,bool $strictId=true){
235235
self::validateMethodPropertyV1($message);
236236
self::validateParamsPropertyV1($message);
237237

238-
if(property_exists($message,'id') && $strictId?($message->id!==null):($message->id)){
238+
if(property_exists($message,'id') && ($strictId?($message->id!==null):($message->id))){
239239
return new RequestMessage($message);
240240
}else{
241241
return new NotificationMessage($message);

tests/MessageTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use YOCLIB\JSONRPC\JSONRPCException;
77
use YOCLIB\JSONRPC\Message;
8+
use YOCLIB\JSONRPC\NotificationMessage;
9+
use YOCLIB\JSONRPC\RequestMessage;
810
use YOCLIB\JSONRPC\ResponseMessage;
911

1012
class MessageTest extends TestCase{
@@ -140,6 +142,17 @@ public function testParseRequestV1WithMethod(){
140142
Message::parseObject((object) ['method'=>null]);
141143
}
142144

145+
/**
146+
* @return void
147+
* @throws JSONRPCException
148+
*/
149+
public function testParseRequestV1WithMethodString(){
150+
$this->expectException(JSONRPCException::class);
151+
$this->expectExceptionMessage('[V1] Missing "params" property in request.');
152+
153+
Message::parseObject((object) ['method'=>'abc']);
154+
}
155+
143156
/**
144157
* @return void
145158
* @throws JSONRPCException
@@ -151,6 +164,43 @@ public function testParseRequestV1WithParams(){
151164
Message::parseObject((object) ['params'=>null]);
152165
}
153166

167+
/**
168+
* @return void
169+
* @throws JSONRPCException
170+
*/
171+
public function testParseRequestV1WithMethodStringAndParams(){
172+
$this->expectException(JSONRPCException::class);
173+
$this->expectExceptionMessage('[V1] The "params" property in request MUST be an array.');
174+
175+
Message::parseObject((object) ['method'=>'abc','params'=>null]);
176+
}
177+
178+
179+
/**
180+
* @return void
181+
* @throws JSONRPCException
182+
*/
183+
public function testParseRequestV1WithMethodStringAndParamsArray(){
184+
$this->assertInstanceOf(NotificationMessage::class,Message::parseObject((object) ['method'=>'abc','params'=>[]]));
185+
}
186+
187+
/**
188+
* @return void
189+
* @throws JSONRPCException
190+
*/
191+
public function testParseRequestV1WithIdNullAndMethodStringAndParamsArray(){
192+
$this->assertInstanceOf(NotificationMessage::class,Message::parseObject((object) ['id'=>null,'method'=>'abc','params'=>[]]));
193+
}
194+
195+
/**
196+
* @return void
197+
* @throws JSONRPCException
198+
*/
199+
public function testParseRequestV1WithIdFalsyAndMethodStringAndParamsArray(){
200+
$this->assertInstanceOf(RequestMessage::class,Message::parseObject((object) ['id'=>false,'method'=>'abc','params'=>[]]));
201+
$this->assertInstanceOf(NotificationMessage::class,Message::parseObject((object) ['id'=>false,'method'=>'abc','params'=>[]],false));
202+
}
203+
154204
/**
155205
* @return void
156206
* @throws JSONRPCException
@@ -217,6 +267,7 @@ public function testParseResponseV1WithIdNullAndResultAndNullError(){
217267
*/
218268
public function testParseResponseV1WithIdFalsyAndResultAndNullError(){
219269
$this->assertInstanceOf(ResponseMessage::class,Message::parseObject((object) ['id'=>false,'result'=>'abc','error'=>null]));
270+
$this->assertInstanceOf(ResponseMessage::class,Message::parseObject((object) ['id'=>false,'result'=>'abc','error'=>null],false));
220271
}
221272

222273
/**

0 commit comments

Comments
 (0)