BlockchainWalletTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Module\Blockchain\Tests\Unit\Models;
  3. use App\Module\Blockchain\Models\BlockchainWallet;
  4. use App\Module\Blockchain\Models\BlockchainTransaction;
  5. use App\Module\Blockchain\Enums\TokenType;
  6. use Tests\TestCase;
  7. /**
  8. * 区块链钱包模型单元测试
  9. */
  10. class BlockchainWalletTest extends TestCase
  11. {
  12. protected string $testAddress = '0x1234567890123456789012345678901234567890';
  13. /**
  14. * 测试创建钱包
  15. * @test
  16. */
  17. public function testCreateWallet()
  18. {
  19. $now = now();
  20. $wallet = BlockchainWallet::create([
  21. 'address' => $this->testAddress,
  22. 'type' => TokenType::BNB,
  23. 'balance' => '1.0',
  24. 'balance_updated_at' => $now,
  25. 'status' => true
  26. ]);
  27. $this->assertInstanceOf(BlockchainWallet::class, $wallet);
  28. $this->assertEquals($this->testAddress, $wallet->address);
  29. $this->assertEquals(TokenType::BNB, $wallet->type);
  30. $this->assertEquals('1.000000000000000000', $wallet->balance);
  31. $this->assertEquals($now->timestamp, $wallet->balance_updated_at->timestamp);
  32. $this->assertTrue($wallet->status);
  33. }
  34. /**
  35. * 测试余额更新时间
  36. * @test
  37. */
  38. public function testBalanceUpdateTime()
  39. {
  40. $wallet = BlockchainWallet::create([
  41. 'address' => $this->testAddress,
  42. 'type' => TokenType::BNB,
  43. 'balance' => '1.0',
  44. 'status' => true
  45. ]);
  46. $this->assertNull($wallet->balance_updated_at);
  47. $now = now();
  48. $wallet->update([
  49. 'balance' => '2.0',
  50. 'balance_updated_at' => $now
  51. ]);
  52. $this->assertEquals('2.000000000000000000', $wallet->balance);
  53. $this->assertEquals($now->timestamp, $wallet->balance_updated_at->timestamp);
  54. }
  55. /**
  56. * 测试钱包关联交易
  57. * @test
  58. */
  59. public function testWalletTransactions()
  60. {
  61. $wallet = BlockchainWallet::create([
  62. 'address' => $this->testAddress,
  63. 'type' => TokenType::BNB,
  64. 'balance' => '1.0',
  65. 'status' => true
  66. ]);
  67. // 创建发送交易
  68. BlockchainTransaction::create([
  69. 'tx_hash' => '0x' . str_repeat('a', 64),
  70. 'from_address' => $this->testAddress,
  71. 'to_address' => '0x' . str_repeat('b', 40),
  72. 'amount' => '0.1',
  73. 'token_type' => TokenType::BNB,
  74. 'gas_price' => '5.0',
  75. 'gas_used' => 21000,
  76. 'status' => 1,
  77. 'block_number' => 12345678
  78. ]);
  79. // 创建接收交易
  80. BlockchainTransaction::create([
  81. 'tx_hash' => '0x' . str_repeat('c', 64),
  82. 'from_address' => '0x' . str_repeat('b', 40),
  83. 'to_address' => $this->testAddress,
  84. 'amount' => '0.2',
  85. 'token_type' => TokenType::BNB,
  86. 'gas_price' => '5.0',
  87. 'gas_used' => 21000,
  88. 'status' => 1,
  89. 'block_number' => 12345679
  90. ]);
  91. // 测试发送交易关联
  92. $this->assertCount(1, $wallet->transactions);
  93. $this->assertEquals('0.100000000000000000', $wallet->transactions->first()->amount);
  94. // 测试接收交易关联
  95. $this->assertCount(1, $wallet->receivedTransactions);
  96. $this->assertEquals('0.200000000000000000', $wallet->receivedTransactions->first()->amount);
  97. }
  98. /**
  99. * 测试钱包地址唯一性约束
  100. * @test
  101. */
  102. public function testWalletAddressUnique()
  103. {
  104. BlockchainWallet::create([
  105. 'address' => $this->testAddress,
  106. 'type' => TokenType::BNB,
  107. 'balance' => '1.0',
  108. 'status' => true
  109. ]);
  110. $this->expectException(\Illuminate\Database\QueryException::class);
  111. // 尝试创建相同地址和类型的钱包应该失败
  112. BlockchainWallet::create([
  113. 'address' => $this->testAddress,
  114. 'type' => TokenType::BNB,
  115. 'balance' => '2.0',
  116. 'status' => true
  117. ]);
  118. }
  119. /**
  120. * 测试不同代币类型的钱包
  121. * @test
  122. */
  123. public function testMultipleTokenTypes()
  124. {
  125. // 创建BNB钱包
  126. $bnbWallet = BlockchainWallet::create([
  127. 'address' => $this->testAddress,
  128. 'type' => TokenType::BNB,
  129. 'balance' => '1.0',
  130. 'status' => true
  131. ]);
  132. // 创建USDT钱包
  133. $usdtWallet = BlockchainWallet::create([
  134. 'address' => $this->testAddress,
  135. 'type' => TokenType::USDT,
  136. 'balance' => '1000.0',
  137. 'status' => true
  138. ]);
  139. // 创建URAUS钱包
  140. $urausWallet = BlockchainWallet::create([
  141. 'address' => $this->testAddress,
  142. 'type' => TokenType::URAUS,
  143. 'balance' => '500.0',
  144. 'status' => true
  145. ]);
  146. $this->assertEquals(TokenType::BNB, $bnbWallet->type);
  147. $this->assertEquals(TokenType::USDT, $usdtWallet->type);
  148. $this->assertEquals(TokenType::URAUS, $urausWallet->type);
  149. $this->assertEquals('1.000000000000000000', $bnbWallet->balance);
  150. $this->assertEquals('1000.000000000000000000', $usdtWallet->balance);
  151. $this->assertEquals('500.000000000000000000', $urausWallet->balance);
  152. }
  153. }