| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Module\Blockchain\Tests\Unit\Models;
- use App\Module\Blockchain\Models\BlockchainTransaction;
- use App\Module\Blockchain\Models\BlockchainWallet;
- use App\Module\Blockchain\Enums\TokenType;
- use Tests\TestCase;
- /**
- * 区块链交易模型单元测试
- */
- class BlockchainTransactionTest extends TestCase
- {
- protected string $fromAddress = '0x1234567890123456789012345678901234567890';
- protected string $toAddress = '0x0987654321098765432109876543210987654321';
- /**
- * 测试创建交易记录
- * @test
- */
- public function testCreateTransaction()
- {
- $transaction = BlockchainTransaction::create([
- 'tx_hash' => '0x' . str_repeat('a', 64),
- 'from_address' => $this->fromAddress,
- 'to_address' => $this->toAddress,
- 'amount' => '1.0',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 0,
- 'block_number' => null
- ]);
- $this->assertInstanceOf(BlockchainTransaction::class, $transaction);
- $this->assertEquals($this->fromAddress, $transaction->from_address);
- $this->assertEquals($this->toAddress, $transaction->to_address);
- $this->assertEquals('1.000000000000000000', $transaction->amount);
- $this->assertEquals(TokenType::BNB, $transaction->token_type);
- $this->assertEquals('5.000000000000000000', $transaction->gas_price);
- $this->assertEquals(21000, $transaction->gas_used);
- $this->assertEquals(0, $transaction->status);
- $this->assertNull($transaction->block_number);
- }
- /**
- * 测试交易哈希唯一性约束
- * @test
- */
- public function testTransactionHashUnique()
- {
- $txHash = '0x' . str_repeat('a', 64);
- BlockchainTransaction::create([
- 'tx_hash' => $txHash,
- 'from_address' => $this->fromAddress,
- 'to_address' => $this->toAddress,
- 'amount' => '1.0',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 0
- ]);
- $this->expectException(\Illuminate\Database\QueryException::class);
- // 尝试创建相同交易哈希的记录应该失败
- BlockchainTransaction::create([
- 'tx_hash' => $txHash,
- 'from_address' => $this->fromAddress,
- 'to_address' => $this->toAddress,
- 'amount' => '2.0',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 0
- ]);
- }
- /**
- * 测试交易状态标签
- * @test
- */
- public function testTransactionStatusLabel()
- {
- $transaction = new BlockchainTransaction();
- $transaction->status = 0;
- $this->assertEquals('待确认', $transaction->status_label);
- $transaction->status = 1;
- $this->assertEquals('成功', $transaction->status_label);
- $transaction->status = 2;
- $this->assertEquals('失败', $transaction->status_label);
- $transaction->status = 99;
- $this->assertEquals('未知', $transaction->status_label);
- }
- /**
- * 测试钱包关联关系
- * @test
- */
- public function testWalletRelations()
- {
- // 创建发送方钱包
- $fromWallet = BlockchainWallet::create([
- 'address' => $this->fromAddress,
- 'type' => TokenType::BNB,
- 'balance' => '10.0',
- 'status' => true
- ]);
- // 创建接收方钱包
- $toWallet = BlockchainWallet::create([
- 'address' => $this->toAddress,
- 'type' => TokenType::BNB,
- 'balance' => '0.0',
- 'status' => true
- ]);
- // 创建交易记录
- $transaction = BlockchainTransaction::create([
- 'tx_hash' => '0x' . str_repeat('a', 64),
- 'from_address' => $this->fromAddress,
- 'to_address' => $this->toAddress,
- 'amount' => '1.0',
- 'token_type' => TokenType::BNB,
- 'gas_price' => '5.0',
- 'gas_used' => 21000,
- 'status' => 1,
- 'block_number' => 12345678
- ]);
- // 测试关联关系
- $this->assertInstanceOf(BlockchainWallet::class, $transaction->fromWallet);
- $this->assertInstanceOf(BlockchainWallet::class, $transaction->toWallet);
- $this->assertEquals($fromWallet->id, $transaction->fromWallet->id);
- $this->assertEquals($toWallet->id, $transaction->toWallet->id);
- }
- }
|