TestCase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Test;
  3. use \PHPUnit\Framework\TestCase as BaseTestCase;
  4. use Web3\Web3;
  5. use Web3\Providers\HttpAsyncProvider;
  6. use Web3\Providers\HttpProvider;
  7. class TestCase extends BaseTestCase
  8. {
  9. /**
  10. * web3
  11. *
  12. * @var \Web3\Web3
  13. */
  14. protected $web3;
  15. /**
  16. * testHost2
  17. *
  18. * @var string
  19. */
  20. protected $testHost2 = 'https://eth-mainnet.g.alchemy.com/v2/notavalidkey';
  21. /**
  22. * testHost
  23. *
  24. * @var string
  25. */
  26. protected $testHost = 'http://localhost:8545';
  27. /**
  28. * testWsHost
  29. *
  30. * @var string
  31. */
  32. protected $testWsHost = 'ws://localhost:8545';
  33. /**
  34. * coinbase
  35. *
  36. * @var string
  37. */
  38. protected $coinbase;
  39. /**
  40. * asyncHttpProvider
  41. *
  42. * @var \Web3\Providers\HttpAsyncProvider
  43. */
  44. protected $asyncHttpProvider;
  45. /**
  46. * EMPTY_ADDRESS
  47. *
  48. * @var string
  49. */
  50. protected $EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
  51. /**
  52. * loadFixtureJsonFile
  53. */
  54. public function loadFixtureJsonFile($fixtureFileName) {
  55. $json = \file_get_contents($fixtureFileName);
  56. if (false === $json) {
  57. throw new \RuntimeException("Unable to load file {$fixtureFileName}");
  58. }
  59. return \json_decode($json, true);
  60. }
  61. /**
  62. * setUp
  63. */
  64. public function setUp(): void
  65. {
  66. $web3 = new Web3($this->testHost);
  67. $this->web3 = $web3;
  68. $asyncHttpProvider = new HttpAsyncProvider($this->testHost);
  69. $this->asyncHttpProvider = $asyncHttpProvider;
  70. $web3->eth->coinbase(function ($err, $coinbase) use ($web3) {
  71. if ($err !== null) {
  72. return $this->fail($err->getMessage());
  73. }
  74. // if ($coinbase === $this->EMPTY_ADDRESS) {
  75. // $web3->eth->accounts(function ($err, $accounts) {
  76. // if ($err !== null) {
  77. // return $this->fail($err->getMessage());
  78. // }
  79. // $this->coinbase = $accounts[rand(0, count($accounts) - 1)];
  80. // });
  81. // } else {
  82. $this->coinbase = $coinbase;
  83. // }
  84. });
  85. }
  86. /**
  87. * tearDown
  88. */
  89. public function tearDown(): void {}
  90. }