BlockchainTransactionTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Module\Blockchain\Tests\Unit\Models;
  3. use App\Module\Blockchain\Models\BlockchainTransaction;
  4. use App\Module\Blockchain\Models\BlockchainWallet;
  5. use App\Module\Blockchain\Enums\TokenType;
  6. use Tests\TestCase;
  7. /**
  8. * 区块链交易模型单元测试
  9. */
  10. class BlockchainTransactionTest extends TestCase
  11. {
  12. protected string $fromAddress = '0x1234567890123456789012345678901234567890';
  13. protected string $toAddress = '0x0987654321098765432109876543210987654321';
  14. /**
  15. * 测试创建交易记录
  16. * @test
  17. */
  18. public function testCreateTransaction()
  19. {
  20. $transaction = BlockchainTransaction::create([
  21. 'tx_hash' => '0x' . str_repeat('a', 64),
  22. 'from_address' => $this->fromAddress,
  23. 'to_address' => $this->toAddress,
  24. 'amount' => '1.0',
  25. 'token_type' => TokenType::BNB,
  26. 'gas_price' => '5.0',
  27. 'gas_used' => 21000,
  28. 'status' => 0,
  29. 'block_number' => null
  30. ]);
  31. $this->assertInstanceOf(BlockchainTransaction::class, $transaction);
  32. $this->assertEquals($this->fromAddress, $transaction->from_address);
  33. $this->assertEquals($this->toAddress, $transaction->to_address);
  34. $this->assertEquals('1.000000000000000000', $transaction->amount);
  35. $this->assertEquals(TokenType::BNB, $transaction->token_type);
  36. $this->assertEquals('5.000000000000000000', $transaction->gas_price);
  37. $this->assertEquals(21000, $transaction->gas_used);
  38. $this->assertEquals(0, $transaction->status);
  39. $this->assertNull($transaction->block_number);
  40. }
  41. /**
  42. * 测试交易哈希唯一性约束
  43. * @test
  44. */
  45. public function testTransactionHashUnique()
  46. {
  47. $txHash = '0x' . str_repeat('a', 64);
  48. BlockchainTransaction::create([
  49. 'tx_hash' => $txHash,
  50. 'from_address' => $this->fromAddress,
  51. 'to_address' => $this->toAddress,
  52. 'amount' => '1.0',
  53. 'token_type' => TokenType::BNB,
  54. 'gas_price' => '5.0',
  55. 'gas_used' => 21000,
  56. 'status' => 0
  57. ]);
  58. $this->expectException(\Illuminate\Database\QueryException::class);
  59. // 尝试创建相同交易哈希的记录应该失败
  60. BlockchainTransaction::create([
  61. 'tx_hash' => $txHash,
  62. 'from_address' => $this->fromAddress,
  63. 'to_address' => $this->toAddress,
  64. 'amount' => '2.0',
  65. 'token_type' => TokenType::BNB,
  66. 'gas_price' => '5.0',
  67. 'gas_used' => 21000,
  68. 'status' => 0
  69. ]);
  70. }
  71. /**
  72. * 测试交易状态标签
  73. * @test
  74. */
  75. public function testTransactionStatusLabel()
  76. {
  77. $transaction = new BlockchainTransaction();
  78. $transaction->status = 0;
  79. $this->assertEquals('待确认', $transaction->status_label);
  80. $transaction->status = 1;
  81. $this->assertEquals('成功', $transaction->status_label);
  82. $transaction->status = 2;
  83. $this->assertEquals('失败', $transaction->status_label);
  84. $transaction->status = 99;
  85. $this->assertEquals('未知', $transaction->status_label);
  86. }
  87. /**
  88. * 测试钱包关联关系
  89. * @test
  90. */
  91. public function testWalletRelations()
  92. {
  93. // 创建发送方钱包
  94. $fromWallet = BlockchainWallet::create([
  95. 'address' => $this->fromAddress,
  96. 'type' => TokenType::BNB,
  97. 'balance' => '10.0',
  98. 'status' => true
  99. ]);
  100. // 创建接收方钱包
  101. $toWallet = BlockchainWallet::create([
  102. 'address' => $this->toAddress,
  103. 'type' => TokenType::BNB,
  104. 'balance' => '0.0',
  105. 'status' => true
  106. ]);
  107. // 创建交易记录
  108. $transaction = BlockchainTransaction::create([
  109. 'tx_hash' => '0x' . str_repeat('a', 64),
  110. 'from_address' => $this->fromAddress,
  111. 'to_address' => $this->toAddress,
  112. 'amount' => '1.0',
  113. 'token_type' => TokenType::BNB,
  114. 'gas_price' => '5.0',
  115. 'gas_used' => 21000,
  116. 'status' => 1,
  117. 'block_number' => 12345678
  118. ]);
  119. // 测试关联关系
  120. $this->assertInstanceOf(BlockchainWallet::class, $transaction->fromWallet);
  121. $this->assertInstanceOf(BlockchainWallet::class, $transaction->toWallet);
  122. $this->assertEquals($fromWallet->id, $transaction->fromWallet->id);
  123. $this->assertEquals($toWallet->id, $transaction->toWallet->id);
  124. }
  125. }