| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace App\Module\Blockchain\Tests\Unit\Services;
- use App\Module\Blockchain\Services\BnbChainService;
- use App\Module\Blockchain\Services\BscScanService;
- use App\Module\Blockchain\Enums\TokenType;
- use Tests\TestCase;
- use Illuminate\Support\Facades\Cache;
- use Web3\Web3;
- /**
- * BNB Chain 服务单元测试
- */
- class BnbChainServiceTest extends TestCase
- {
- protected BnbChainService $service;
- protected BscScanService $bscScanService;
- protected string $testAddress = '0x1234567890123456789012345678901234567890';
- protected string $testTxHash = '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
- protected function setUp(): void
- {
- parent::setUp();
- $this->bscScanService = $this->createMock(BscScanService::class);
- $this->service = new BnbChainService($this->bscScanService, 'testnet');
- }
- /**
- * 测试获取网络配置
- * @test
- */
- public function testGetNetwork()
- {
- $network = $this->service->getNetwork();
- $this->assertEquals('BNB Smart Chain Testnet', $network['name']);
- $this->assertEquals(97, $network['chain_id']);
- $this->assertEquals('https://testnet.bscscan.com', $network['explorer']);
- }
- /**
- * 测试获取链 ID
- * @test
- */
- public function testGetChainId()
- {
- $this->assertEquals(97, $this->service->getChainId());
- }
- /**
- * 测试获取浏览器 URL
- * @test
- */
- public function testGetExplorerUrls()
- {
- $this->assertEquals(
- 'https://testnet.bscscan.com',
- $this->service->getExplorerUrl()
- );
- $this->assertEquals(
- 'https://testnet.bscscan.com/tx/' . $this->testTxHash,
- $this->service->getTransactionUrl($this->testTxHash)
- );
- $this->assertEquals(
- 'https://testnet.bscscan.com/address/' . $this->testAddress,
- $this->service->getAddressUrl($this->testAddress)
- );
- $tokenUrl = $this->service->getTokenUrl(TokenType::USDT);
- $this->assertStringContainsString('https://testnet.bscscan.com/token/', $tokenUrl);
- }
- /**
- * 测试 Gas 价格计算
- * @test
- */
- public function testGasPriceCalculation()
- {
- Cache::shouldReceive('remember')
- ->once()
- ->andReturn([
- 'safe_low' => 5,
- 'standard' => 6,
- 'fast' => 7,
- 'current' => 5.5
- ]);
- $gasPrice = $this->service->getGasPrice();
- $this->assertArrayHasKey('safe_low', $gasPrice);
- $this->assertArrayHasKey('standard', $gasPrice);
- $this->assertArrayHasKey('fast', $gasPrice);
- $this->assertArrayHasKey('current', $gasPrice);
- $this->assertGreaterThanOrEqual(5, $gasPrice['safe_low']);
- $this->assertGreaterThanOrEqual($gasPrice['safe_low'], $gasPrice['standard']);
- $this->assertGreaterThanOrEqual($gasPrice['standard'], $gasPrice['fast']);
- }
- /**
- * 测试 MEV 保护
- * @test
- */
- public function testMevProtection()
- {
- Cache::shouldReceive('remember')
- ->once()
- ->andReturn([
- 'safe_low' => 5,
- 'standard' => 6,
- 'fast' => 7,
- 'current' => 5.5
- ]);
- $mevPrice = $this->service->getMevProtectedGasPrice();
- $this->assertArrayHasKey('max_priority_fee_per_gas', $mevPrice);
- $this->assertArrayHasKey('max_fee_per_gas', $mevPrice);
- $this->assertLessThanOrEqual(10, $mevPrice['max_fee_per_gas']);
- $this->assertGreaterThan(0, $mevPrice['max_priority_fee_per_gas']);
- }
- /**
- * 测试交易确认检查
- * @test
- */
- public function testTransactionConfirmations()
- {
- // Mock 交易收据
- $this->bscScanService->method('getTransactionReceipt')
- ->willReturn([
- 'blockNumber' => '0x100' // 区块 256
- ]);
- // Mock 最新区块号
- Cache::shouldReceive('remember')
- ->once()
- ->andReturn(276); // 当前区块 276
- $confirmations = $this->service->getTransactionConfirmations($this->testTxHash);
- $this->assertEquals(20, $confirmations); // 276 - 256 = 20
- $isSafe = $this->service->isTransactionSafe($this->testTxHash);
- $this->assertTrue($isSafe); // 20 确认数已经安全
- }
- /**
- * 测试余额查询代理到 BscScanService
- * @test
- */
- public function testBalanceQueryDelegation()
- {
- $this->bscScanService->expects($this->once())
- ->method('getBalance')
- ->with($this->testAddress, TokenType::BNB)
- ->willReturn(1.5);
- $balance = $this->service->getBalance($this->testAddress, TokenType::BNB);
- $this->assertEquals(1.5, $balance);
- }
- /**
- * 测试交易历史查询代理到 BscScanService
- * @test
- */
- public function testTransactionHistoryDelegation()
- {
- $expectedHistory = [
- [
- 'hash' => $this->testTxHash,
- 'from' => $this->testAddress,
- 'to' => '0x0987654321098765432109876543210987654321',
- 'value' => '1000000000000000000'
- ]
- ];
- $this->bscScanService->expects($this->once())
- ->method('getTransactionHistory')
- ->with($this->testAddress, TokenType::BNB, 1, 10)
- ->willReturn($expectedHistory);
- $history = $this->service->getTransactionHistory($this->testAddress, TokenType::BNB);
- $this->assertEquals($expectedHistory, $history);
- }
- }
|