| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace App\Module\Blockchain\Tests\Unit\Models;
- use App\Module\Blockchain\Models\BlockchainWallet;
- use App\Module\Blockchain\Models\BlockchainTransaction;
- use App\Module\Blockchain\Enums\TokenType;
- use Tests\TestCase;
- /**
- * 区块链钱包模型单元测试
- */
- class BlockchainWalletTest extends TestCase
- {
- protected string $testAddress = '0x1234567890123456789012345678901234567890';
- /**
- * 测试创建钱包
- * @test
- */
- public function testCreateWallet()
- {
- $now = now();
- $wallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '1.0',
- 'balance_updated_at' => $now,
- 'status' => true
- ]);
- $this->assertInstanceOf(BlockchainWallet::class, $wallet);
- $this->assertEquals($this->testAddress, $wallet->address);
- $this->assertEquals(TokenType::BNB, $wallet->type);
- $this->assertEquals('1.000000000000000000', $wallet->balance);
- $this->assertEquals($now->timestamp, $wallet->balance_updated_at->timestamp);
- $this->assertTrue($wallet->status);
- }
- /**
- * 测试余额更新时间
- * @test
- */
- public function testBalanceUpdateTime()
- {
- $wallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '1.0',
- 'status' => true
- ]);
- $this->assertNull($wallet->balance_updated_at);
- $now = now();
- $wallet->update([
- 'balance' => '2.0',
- 'balance_updated_at' => $now
- ]);
- $this->assertEquals('2.000000000000000000', $wallet->balance);
- $this->assertEquals($now->timestamp, $wallet->balance_updated_at->timestamp);
- }
- /**
- * 测试钱包关联交易
- * @test
- */
- public function testWalletTransactions()
- {
- $wallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '1.0',
- 'status' => true
- ]);
- // 创建发送交易
- BlockchainTransaction::create([
- 'tx_hash' => '0x' . str_repeat('a', 64),
- 'from_address' => $this->testAddress,
- 'to_address' => '0x' . str_repeat('b', 40),
- 'amount' => '0.1',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 1,
- 'block_number' => 12345678
- ]);
- // 创建接收交易
- BlockchainTransaction::create([
- 'tx_hash' => '0x' . str_repeat('c', 64),
- 'from_address' => '0x' . str_repeat('b', 40),
- 'to_address' => $this->testAddress,
- 'amount' => '0.2',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 1,
- 'block_number' => 12345679
- ]);
- // 测试发送交易关联
- $this->assertCount(1, $wallet->transactions);
- $this->assertEquals('0.100000000000000000', $wallet->transactions->first()->amount);
- // 测试接收交易关联
- $this->assertCount(1, $wallet->receivedTransactions);
- $this->assertEquals('0.200000000000000000', $wallet->receivedTransactions->first()->amount);
- }
- /**
- * 测试钱包地址唯一性约束
- * @test
- */
- public function testWalletAddressUnique()
- {
- BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '1.0',
- 'status' => true
- ]);
- $this->expectException(\Illuminate\Database\QueryException::class);
- // 尝试创建相同地址和类型的钱包应该失败
- BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '2.0',
- 'status' => true
- ]);
- }
- /**
- * 测试不同代币类型的钱包
- * @test
- */
- public function testMultipleTokenTypes()
- {
- // 创建BNB钱包
- $bnbWallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::BNB,
- 'balance' => '1.0',
- 'status' => true
- ]);
- // 创建USDT钱包
- $usdtWallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::USDT,
- 'balance' => '1000.0',
- 'status' => true
- ]);
- // 创建URAUS钱包
- $urausWallet = BlockchainWallet::create([
- 'address' => $this->testAddress,
- 'type' => TokenType::URAUS,
- 'balance' => '500.0',
- 'status' => true
- ]);
- $this->assertEquals(TokenType::BNB, $bnbWallet->type);
- $this->assertEquals(TokenType::USDT, $usdtWallet->type);
- $this->assertEquals(TokenType::URAUS, $urausWallet->type);
- $this->assertEquals('1.000000000000000000', $bnbWallet->balance);
- $this->assertEquals('1000.000000000000000000', $usdtWallet->balance);
- $this->assertEquals('500.000000000000000000', $urausWallet->balance);
- }
- }
|