PersonalApiTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(): void
  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->accounts(function ($err, $accounts) use ($personal) {
  153. $this->web3->eth->sendTransaction([
  154. 'from' => $accounts[0],
  155. 'to' => $this->newAccount,
  156. 'value' => '0xfffffffffffff',
  157. ], function ($err, $transaction) {
  158. if ($err !== null) {
  159. return $this->fail($err->getMessage());
  160. }
  161. $this->assertTrue(is_string($transaction));
  162. $this->assertTrue(mb_strlen($transaction) === 66);
  163. });
  164. });
  165. $this->web3->eth->accounts(function ($err, $accounts) use ($personal) {
  166. $personal->sendTransaction([
  167. 'from' => $this->newAccount,
  168. 'to' => $accounts[0],
  169. 'value' => '0x01',
  170. 'gasLimit' => 21000,
  171. 'gasPrice' => 5000000000,
  172. ], '123456', function ($err, $transaction) {
  173. if ($err !== null) {
  174. return $this->fail($err->getMessage());
  175. }
  176. $this->assertTrue(is_string($transaction));
  177. $this->assertTrue(mb_strlen($transaction) === 66);
  178. });
  179. });
  180. }
  181. /**
  182. * testUnallowedMethod
  183. *
  184. * @return void
  185. */
  186. public function testUnallowedMethod()
  187. {
  188. $this->expectException(RuntimeException::class);
  189. $personal = $this->personal;
  190. $personal->hello(function ($err, $hello) {
  191. if ($err !== null) {
  192. return $this->fail($err->getMessage());
  193. }
  194. $this->assertTrue(true);
  195. });
  196. }
  197. /**
  198. * testWrongParam
  199. *
  200. * @return void
  201. */
  202. public function testWrongParam()
  203. {
  204. $this->expectException(RuntimeException::class);
  205. $personal = $this->personal;
  206. $personal->newAccount($personal, function ($err, $account) {
  207. if ($err !== null) {
  208. return $this->fail($err->getMessage());
  209. }
  210. $this->assertTrue(is_string($account));
  211. });
  212. }
  213. /**
  214. * testWrongCallback
  215. *
  216. * @return void
  217. */
  218. public function testWrongCallback()
  219. {
  220. $this->expectException(InvalidArgumentException::class);
  221. $personal = $this->personal;
  222. $personal->newAccount('123456');
  223. }
  224. }