BscScanServiceTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Module\Blockchain\Tests\Unit;
  3. use App\Module\Blockchain\Services\BscScanService;
  4. use App\Module\Blockchain\Enums\TokenType;
  5. use Tests\TestCase;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Facades\Cache;
  8. /**
  9. * BSCScan 服务单元测试
  10. */
  11. class BscScanServiceTest extends TestCase
  12. {
  13. protected BscScanService $service;
  14. protected string $testAddress = '0x1234567890123456789012345678901234567890';
  15. protected string $testTxHash = '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->service = new BscScanService();
  20. }
  21. /**
  22. * 测试获取BNB余额
  23. * @test
  24. */
  25. public function testGetBnbBalance()
  26. {
  27. Http::fake([
  28. '*' => Http::response([
  29. 'status' => '1',
  30. 'message' => 'OK',
  31. 'result' => '1000000000000000000' // 1 BNB
  32. ])
  33. ]);
  34. $balance = $this->service->getBalance($this->testAddress, TokenType::BNB);
  35. $this->assertEquals(1.0, $balance);
  36. }
  37. /**
  38. * 测试获取代币余额
  39. * @test
  40. */
  41. public function testGetTokenBalance()
  42. {
  43. Http::fake([
  44. '*' => Http::response([
  45. 'status' => '1',
  46. 'message' => 'OK',
  47. 'result' => '1000000000000000000000' // 1000 USDT
  48. ])
  49. ]);
  50. $balance = $this->service->getBalance($this->testAddress, TokenType::USDT);
  51. $this->assertEquals(1000.0, $balance);
  52. }
  53. /**
  54. * 测试地址验证
  55. * @test
  56. */
  57. public function testAddressValidation()
  58. {
  59. $this->assertTrue($this->service->isValidAddress($this->testAddress));
  60. $this->assertFalse($this->service->isValidAddress('invalid_address'));
  61. }
  62. /**
  63. * 测试获取交易状态
  64. * @test
  65. */
  66. public function testGetTransactionStatus()
  67. {
  68. Http::fake([
  69. '*' => Http::response([
  70. 'result' => [
  71. 'blockNumber' => '0x100',
  72. 'gas' => '0x5208',
  73. 'gasPrice' => '0x4a817c800'
  74. ]
  75. ])
  76. ]);
  77. $status = $this->service->getTransactionStatus($this->testTxHash);
  78. $this->assertArrayHasKey('status', $status);
  79. $this->assertArrayHasKey('blockNumber', $status);
  80. }
  81. /**
  82. * 测试获取交易收据
  83. * @test
  84. */
  85. public function testGetTransactionReceipt()
  86. {
  87. Http::fake([
  88. '*' => Http::response([
  89. 'result' => [
  90. 'status' => '0x1',
  91. 'blockNumber' => '0x100',
  92. 'gasUsed' => '0x5208'
  93. ]
  94. ])
  95. ]);
  96. $receipt = $this->service->getTransactionReceipt($this->testTxHash);
  97. $this->assertArrayHasKey('status', $receipt);
  98. }
  99. /**
  100. * 测试估算Gas费用
  101. * @test
  102. */
  103. public function testEstimateGasFee()
  104. {
  105. Http::fake([
  106. '*' => Http::response([
  107. 'result' => '0x4a817c800' // 20 GWEI
  108. ])
  109. ]);
  110. $fee = $this->service->estimateGasFee(
  111. $this->testAddress,
  112. '0x0987654321098765432109876543210987654321',
  113. TokenType::BNB,
  114. 1.0
  115. );
  116. $this->assertIsFloat($fee);
  117. }
  118. /**
  119. * 测试获取交易历史
  120. * @test
  121. */
  122. public function testGetTransactionHistory()
  123. {
  124. Http::fake([
  125. '*' => Http::response([
  126. 'status' => '1',
  127. 'message' => 'OK',
  128. 'result' => [
  129. [
  130. 'hash' => $this->testTxHash,
  131. 'from' => $this->testAddress,
  132. 'to' => '0x0987654321098765432109876543210987654321',
  133. 'value' => '1000000000000000000'
  134. ]
  135. ]
  136. ])
  137. ]);
  138. $history = $this->service->getTransactionHistory($this->testAddress, TokenType::BNB);
  139. $this->assertIsArray($history);
  140. $this->assertNotEmpty($history);
  141. }
  142. /**
  143. * 测试缓存机制
  144. * @test
  145. */
  146. public function testBalanceCaching()
  147. {
  148. Http::fake([
  149. '*' => Http::response([
  150. 'status' => '1',
  151. 'message' => 'OK',
  152. 'result' => '1000000000000000000'
  153. ])
  154. ]);
  155. // 第一次调用会发起HTTP请求
  156. $balance1 = $this->service->getBalance($this->testAddress, TokenType::BNB);
  157. // 第二次调用应该使用缓存
  158. $balance2 = $this->service->getBalance($this->testAddress, TokenType::BNB);
  159. $this->assertEquals($balance1, $balance2);
  160. Http::assertSentCount(1); // 确保只发送了一次HTTP请求
  161. }
  162. }