Web3ApiTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Test\Unit;
  3. use RuntimeException;
  4. use InvalidArgumentException;
  5. use Test\TestCase;
  6. class Web3ApiTest extends TestCase
  7. {
  8. /**
  9. * testHex
  10. * 'hello world'
  11. * you can check by call pack('H*', $hex)
  12. *
  13. * @var string
  14. */
  15. protected $testHex = '0x68656c6c6f20776f726c64';
  16. /**
  17. * testHash
  18. *
  19. * @var string
  20. */
  21. protected $testHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad';
  22. /**
  23. * setUp
  24. *
  25. * @return void
  26. */
  27. public function setUp()
  28. {
  29. parent::setUp();
  30. }
  31. /**
  32. * testClientVersion
  33. *
  34. * @return void
  35. */
  36. public function testClientVersion()
  37. {
  38. $web3 = $this->web3;
  39. $web3->clientVersion(function ($err, $version) {
  40. if ($err !== null) {
  41. return $this->fail($err->getMessage());
  42. }
  43. $this->assertTrue(is_string($version));
  44. });
  45. }
  46. /**
  47. * testSha3
  48. *
  49. * @return void
  50. */
  51. public function testSha3()
  52. {
  53. $web3 = $this->web3;
  54. $web3->sha3($this->testHex, function ($err, $hash) {
  55. if ($err !== null) {
  56. return $this->fail($err->getMessage());
  57. }
  58. $this->assertEquals($hash, $this->testHash);
  59. });
  60. $web3->sha3('hello world', function ($err, $hash) {
  61. if ($err !== null) {
  62. return $this->fail($err->getMessage());
  63. }
  64. $this->assertEquals($hash, $this->testHash);
  65. });
  66. }
  67. /**
  68. * testUnallowedMethod
  69. *
  70. * @return void
  71. */
  72. public function testUnallowedMethod()
  73. {
  74. $this->expectException(RuntimeException::class);
  75. $web3 = $this->web3;
  76. $web3->hello(function ($err, $hello) {
  77. if ($err !== null) {
  78. return $this->fail($err->getMessage());
  79. }
  80. $this->assertTrue(true);
  81. });
  82. }
  83. /**
  84. * testWrongParam
  85. * We transform data and throw invalid argument exception
  86. * instead of runtime exception.
  87. *
  88. * @return void
  89. */
  90. public function testWrongParam()
  91. {
  92. $this->expectException(RuntimeException::class);
  93. $web3 = $this->web3;
  94. $web3->sha3($web3, function ($err, $hash) {
  95. if ($err !== null) {
  96. return $this->fail($err->getMessage());
  97. }
  98. $this->assertTrue(true);
  99. });
  100. }
  101. /**
  102. * testWrongCallback
  103. *
  104. * @return void
  105. */
  106. public function testWrongCallback()
  107. {
  108. $this->expectException(InvalidArgumentException::class);
  109. $web3 = $this->web3;
  110. $web3->sha3('hello world');
  111. }
  112. }