PersonalApiTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * testLockAccount
  109. *
  110. * @return void
  111. */
  112. public function testLockAccount()
  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. $personal->unlockAccount($this->newAccount, '123456', function ($err, $unlocked) {
  124. if ($err !== null) {
  125. return $this->fail($err->getMessage());
  126. }
  127. $this->assertTrue($unlocked);
  128. });
  129. $personal->lockAccount($this->newAccount, function ($err, $locked) {
  130. if ($err !== null) {
  131. return $this->fail($err->getMessage());
  132. }
  133. $this->assertTrue($locked);
  134. });
  135. }
  136. /**
  137. * testSendTransaction
  138. *
  139. * @return void
  140. */
  141. public function testSendTransaction()
  142. {
  143. $personal = $this->personal;
  144. // create account
  145. $personal->newAccount('123456', function ($err, $account) {
  146. if ($err !== null) {
  147. return $this->fail($err->getMessage());
  148. }
  149. $this->newAccount = $account;
  150. $this->assertTrue(is_string($account));
  151. });
  152. $this->web3->eth->sendTransaction([
  153. 'from' => $this->coinbase,
  154. 'to' => $this->newAccount,
  155. 'value' => '0xfffffffff',
  156. ], function ($err, $transaction) {
  157. if ($err !== null) {
  158. return $this->fail($err->getMessage());
  159. }
  160. $this->assertTrue(is_string($transaction));
  161. $this->assertTrue(mb_strlen($transaction) === 66);
  162. });
  163. $personal->sendTransaction([
  164. 'from' => $this->newAccount,
  165. 'to' => $this->coinbase,
  166. 'value' => '0x01',
  167. ], '123456', function ($err, $transaction) {
  168. if ($err !== null) {
  169. return $this->fail($err->getMessage());
  170. }
  171. $this->assertTrue(is_string($transaction));
  172. $this->assertTrue(mb_strlen($transaction) === 66);
  173. });
  174. }
  175. /**
  176. * testUnallowedMethod
  177. *
  178. * @return void
  179. */
  180. public function testUnallowedMethod()
  181. {
  182. $this->expectException(RuntimeException::class);
  183. $personal = $this->personal;
  184. $personal->hello(function ($err, $hello) {
  185. if ($err !== null) {
  186. return $this->fail($err->getMessage());
  187. }
  188. $this->assertTrue(true);
  189. });
  190. }
  191. /**
  192. * testWrongParam
  193. *
  194. * @return void
  195. */
  196. public function testWrongParam()
  197. {
  198. $this->expectException(RuntimeException::class);
  199. $personal = $this->personal;
  200. $personal->newAccount($personal, function ($err, $account) {
  201. if ($err !== null) {
  202. return $this->fail($err->getMessage());
  203. }
  204. $this->assertTrue(is_string($account));
  205. });
  206. }
  207. /**
  208. * testWrongCallback
  209. *
  210. * @return void
  211. */
  212. public function testWrongCallback()
  213. {
  214. $this->expectException(InvalidArgumentException::class);
  215. $personal = $this->personal;
  216. $personal->newAccount('123456');
  217. }
  218. }