PersonalApiTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Test\Unit;
  3. use RuntimeException;
  4. use InvalidArgumentException;
  5. use Test\TestCase;
  6. class PersonalApiTest extends TestCase
  7. {
  8. /**
  9. * personal
  10. *
  11. * @var Web3\Personal
  12. */
  13. protected $personal;
  14. /**
  15. * newAccount
  16. *
  17. * @var string
  18. */
  19. protected $newAccount;
  20. /**
  21. * setUp
  22. *
  23. * @return void
  24. */
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. $this->personal = $this->web3->personal;
  29. }
  30. /**
  31. * testListAccounts
  32. *
  33. * @return void
  34. */
  35. public function testListAccounts()
  36. {
  37. $personal = $this->personal;
  38. $personal->listAccounts(function ($err, $accounts) {
  39. if ($err !== null) {
  40. // infura banned us to use list accounts
  41. return $this->assertTrue($err->getCode() === 405);
  42. }
  43. $this->assertTrue(is_array($accounts));
  44. });
  45. }
  46. /**
  47. * testNewAccount
  48. *
  49. * @return void
  50. */
  51. public function testNewAccount()
  52. {
  53. $personal = $this->personal;
  54. $personal->newAccount('123456', function ($err, $account) {
  55. if ($err !== null) {
  56. return $this->fail($err->getMessage());
  57. }
  58. $this->assertTrue(is_string($account));
  59. });
  60. }
  61. /**
  62. * testUnlockAccount
  63. *
  64. * @return void
  65. */
  66. public function testUnlockAccount()
  67. {
  68. $personal = $this->personal;
  69. // create account
  70. $personal->newAccount('123456', function ($err, $account) {
  71. if ($err !== null) {
  72. return $this->fail($err->getMessage());
  73. }
  74. $this->newAccount = $account;
  75. $this->assertTrue(is_string($account));
  76. });
  77. $personal->unlockAccount($this->newAccount, '123456', function ($err, $unlocked) {
  78. if ($err !== null) {
  79. return $this->fail($err->getMessage());
  80. }
  81. $this->assertTrue($unlocked);
  82. });
  83. }
  84. /**
  85. * testUnlockAccountWithDuration
  86. *
  87. * @return void
  88. */
  89. public function testUnlockAccountWithDuration()
  90. {
  91. $personal = $this->personal;
  92. // create account
  93. $personal->newAccount('123456', function ($err, $account) {
  94. if ($err !== null) {
  95. return $this->fail($err->getMessage());
  96. }
  97. $this->newAccount = $account;
  98. $this->assertTrue(is_string($account));
  99. });
  100. $personal->unlockAccount($this->newAccount, '123456', 100, function ($err, $unlocked) {
  101. if ($err !== null) {
  102. return $this->fail($err->getMessage());
  103. }
  104. $this->assertTrue($unlocked);
  105. });
  106. }
  107. /**
  108. * testSendTransaction
  109. *
  110. * @return void
  111. */
  112. public function testSendTransaction()
  113. {
  114. $personal = $this->personal;
  115. // create account
  116. $personal->newAccount('123456', function ($err, $account) {
  117. if ($err !== null) {
  118. return $this->fail($err->getMessage());
  119. }
  120. $this->newAccount = $account;
  121. $this->assertTrue(is_string($account));
  122. });
  123. $this->web3->eth->sendTransaction([
  124. 'from' => $this->coinbase,
  125. 'to' => $this->newAccount,
  126. 'value' => '0xfffffffff',
  127. ], function ($err, $transaction) {
  128. if ($err !== null) {
  129. return $this->fail($err->getMessage());
  130. }
  131. $this->assertTrue(is_string($transaction));
  132. $this->assertTrue(mb_strlen($transaction) === 66);
  133. });
  134. $personal->sendTransaction([
  135. 'from' => $this->newAccount,
  136. 'to' => $this->coinbase,
  137. 'value' => '0x01',
  138. ], '123456', function ($err, $transaction) {
  139. if ($err !== null) {
  140. return $this->fail($err->getMessage());
  141. }
  142. $this->assertTrue(is_string($transaction));
  143. $this->assertTrue(mb_strlen($transaction) === 66);
  144. });
  145. }
  146. /**
  147. * testUnallowedMethod
  148. *
  149. * @return void
  150. */
  151. public function testUnallowedMethod()
  152. {
  153. $this->expectException(RuntimeException::class);
  154. $personal = $this->personal;
  155. $personal->hello(function ($err, $hello) {
  156. if ($err !== null) {
  157. return $this->fail($err->getMessage());
  158. }
  159. $this->assertTrue(true);
  160. });
  161. }
  162. /**
  163. * testWrongParam
  164. *
  165. * @return void
  166. */
  167. public function testWrongParam()
  168. {
  169. $this->expectException(RuntimeException::class);
  170. $personal = $this->personal;
  171. $personal->newAccount($personal, function ($err, $account) {
  172. if ($err !== null) {
  173. return $this->fail($err->getMessage());
  174. }
  175. $this->assertTrue(is_string($account));
  176. });
  177. }
  178. /**
  179. * testWrongCallback
  180. *
  181. * @return void
  182. */
  183. public function testWrongCallback()
  184. {
  185. $this->expectException(InvalidArgumentException::class);
  186. $personal = $this->personal;
  187. $personal->newAccount('123456');
  188. }
  189. }