JSONRPCTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Test\Unit;
  3. use InvalidArgumentException;
  4. use Test\TestCase;
  5. use Web3\Methods\JSONRPC;
  6. class JSONRPCTest extends TestCase
  7. {
  8. /**
  9. * testJSONRPC
  10. *
  11. * @return void
  12. */
  13. public function testJSONRPC()
  14. {
  15. $id = rand();
  16. $params = [
  17. 'hello world'
  18. ];
  19. $method = 'echo';
  20. $rpc = new JSONRPC($method, $params);
  21. $rpc->id = $id;
  22. $this->assertEquals($id, $rpc->id);
  23. $this->assertEquals('2.0', $rpc->rpcVersion);
  24. $this->assertEquals($method, $rpc->method);
  25. $this->assertEquals($params, $rpc->arguments);
  26. $this->assertEquals([
  27. 'id' => $id,
  28. 'jsonrpc' => '2.0',
  29. 'method' => $method,
  30. 'params' => $params
  31. ], $rpc->toPayload());
  32. $this->assertEquals(json_encode($rpc->toPayload()), (string) $rpc);
  33. $params = [
  34. 'hello ethereum'
  35. ];
  36. $rpc->arguments = $params;
  37. $this->assertEquals($params, $rpc->arguments);
  38. $this->assertEquals([
  39. 'id' => $id,
  40. 'jsonrpc' => '2.0',
  41. 'method' => $method,
  42. 'params' => $params
  43. ], $rpc->toPayload());
  44. $this->assertEquals(json_encode($rpc->toPayload()), (string) $rpc);
  45. }
  46. /**
  47. * testThrowException
  48. *
  49. * @return void
  50. */
  51. public function testThrowException()
  52. {
  53. $id = 'zzz';
  54. $params = [
  55. 'hello world'
  56. ];
  57. $method = 'echo';
  58. $rpc = new JSONRPC($method, $params);
  59. try {
  60. // id is not integer
  61. $rpc->id = $id;
  62. } catch (InvalidArgumentException $e) {
  63. $this->assertTrue($e !== null);
  64. }
  65. try {
  66. // arguments is not array
  67. $rpc->arguments = $id;
  68. } catch (InvalidArgumentException $e) {
  69. $this->assertTrue($e !== null);
  70. }
  71. }
  72. }