| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Module\Blockchain\Tests\Unit;
- use App\Module\Blockchain\Services\BscScanService;
- use App\Module\Blockchain\Enums\TokenType;
- use Tests\TestCase;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Cache;
- /**
- * BSCScan 服务单元测试
- */
- class BscScanServiceTest extends TestCase
- {
- protected BscScanService $service;
- protected string $testAddress = '0x1234567890123456789012345678901234567890';
- protected string $testTxHash = '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
- protected function setUp(): void
- {
- parent::setUp();
- $this->service = new BscScanService();
- }
- /**
- * 测试获取BNB余额
- * @test
- */
- public function testGetBnbBalance()
- {
- Http::fake([
- '*' => Http::response([
- 'status' => '1',
- 'message' => 'OK',
- 'result' => '1000000000000000000' // 1 BNB
- ])
- ]);
- $balance = $this->service->getBalance($this->testAddress, TokenType::BNB);
- $this->assertEquals(1.0, $balance);
- }
- /**
- * 测试获取代币余额
- * @test
- */
- public function testGetTokenBalance()
- {
- Http::fake([
- '*' => Http::response([
- 'status' => '1',
- 'message' => 'OK',
- 'result' => '1000000000000000000000' // 1000 USDT
- ])
- ]);
- $balance = $this->service->getBalance($this->testAddress, TokenType::USDT);
- $this->assertEquals(1000.0, $balance);
- }
- /**
- * 测试地址验证
- * @test
- */
- public function testAddressValidation()
- {
- $this->assertTrue($this->service->isValidAddress($this->testAddress));
- $this->assertFalse($this->service->isValidAddress('invalid_address'));
- }
- /**
- * 测试获取交易状态
- * @test
- */
- public function testGetTransactionStatus()
- {
- Http::fake([
- '*' => Http::response([
- 'result' => [
- 'blockNumber' => '0x100',
- 'gas' => '0x5208',
- 'gasPrice' => '0x4a817c800'
- ]
- ])
- ]);
- $status = $this->service->getTransactionStatus($this->testTxHash);
- $this->assertArrayHasKey('status', $status);
- $this->assertArrayHasKey('blockNumber', $status);
- }
- /**
- * 测试获取交易收据
- * @test
- */
- public function testGetTransactionReceipt()
- {
- Http::fake([
- '*' => Http::response([
- 'result' => [
- 'status' => '0x1',
- 'blockNumber' => '0x100',
- 'gasUsed' => '0x5208'
- ]
- ])
- ]);
- $receipt = $this->service->getTransactionReceipt($this->testTxHash);
- $this->assertArrayHasKey('status', $receipt);
- }
- /**
- * 测试估算Gas费用
- * @test
- */
- public function testEstimateGasFee()
- {
- Http::fake([
- '*' => Http::response([
- 'result' => '0x4a817c800' // 20 GWEI
- ])
- ]);
- $fee = $this->service->estimateGasFee(
- $this->testAddress,
- '0x0987654321098765432109876543210987654321',
- TokenType::BNB,
- 1.0
- );
- $this->assertIsFloat($fee);
- }
- /**
- * 测试获取交易历史
- * @test
- */
- public function testGetTransactionHistory()
- {
- Http::fake([
- '*' => Http::response([
- 'status' => '1',
- 'message' => 'OK',
- 'result' => [
- [
- 'hash' => $this->testTxHash,
- 'from' => $this->testAddress,
- 'to' => '0x0987654321098765432109876543210987654321',
- 'value' => '1000000000000000000'
- ]
- ]
- ])
- ]);
- $history = $this->service->getTransactionHistory($this->testAddress, TokenType::BNB);
- $this->assertIsArray($history);
- $this->assertNotEmpty($history);
- }
- /**
- * 测试缓存机制
- * @test
- */
- public function testBalanceCaching()
- {
- Http::fake([
- '*' => Http::response([
- 'status' => '1',
- 'message' => 'OK',
- 'result' => '1000000000000000000'
- ])
- ]);
- // 第一次调用会发起HTTP请求
- $balance1 = $this->service->getBalance($this->testAddress, TokenType::BNB);
- // 第二次调用应该使用缓存
- $balance2 = $this->service->getBalance($this->testAddress, TokenType::BNB);
- $this->assertEquals($balance1, $balance2);
- Http::assertSentCount(1); // 确保只发送了一次HTTP请求
- }
- }
|