| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace Tests\Unit\Transfer;
- use Tests\TestCase;
- use App\Module\Transfer\Services\FeeService;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\User\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\DB;
- /**
- * FeeService转移方法测试
- */
- class FeeServiceTransferTest extends TestCase
- {
- use RefreshDatabase;
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试数据
- $this->createTestData();
- }
- /**
- * 创建测试数据
- */
- private function createTestData()
- {
- // 创建测试应用
- DB::table('kku_transfer_apps')->insert([
- 'id' => 1,
- 'keyname' => 'test_app',
- 'title' => '测试应用',
- 'description' => '用于测试的应用',
- 'out_id' => 1001,
- 'currency_id' => 1,
- 'fund_id' => 1,
- 'exchange_rate' => 1.0000,
- 'is_enabled' => true,
- 'allow_transfer_in' => true,
- 'allow_transfer_out' => true,
- 'fee_in_rate' => 0.0050,
- 'fee_out_rate' => 0.0100,
- 'fee_in_min' => 0.1000,
- 'fee_in_max' => 10.0000,
- 'fee_out_min' => 0.2000,
- 'fee_out_max' => 20.0000,
- 'fee_account_uid' => 1,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- // 创建测试用户
- DB::table('kku_users')->insert([
- 'id' => 1,
- 'username' => 'fee_account',
- 'email' => 'fee@test.com',
- 'password' => bcrypt('password'),
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- DB::table('kku_users')->insert([
- 'id' => 2,
- 'username' => 'target_user',
- 'email' => 'target@test.com',
- 'password' => bcrypt('password'),
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- // 创建资金账户
- DB::table('kku_fund')->insert([
- 'user_id' => 1,
- 'fund_id' => 1,
- 'balance' => 1000.0000,
- 'create_time' => time(),
- 'update_time' => time(),
- ]);
- DB::table('kku_fund')->insert([
- 'user_id' => 2,
- 'fund_id' => 1,
- 'balance' => 0.0000,
- 'create_time' => time(),
- 'update_time' => time(),
- ]);
- }
- /**
- * 测试成功转移手续费
- */
- public function testSuccessfulTransfer()
- {
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 2,
- amount: 10.0,
- orderId: 12345
- );
- $this->assertTrue($result, '手续费转移应该成功');
- }
- /**
- * 测试自定义转移类型
- */
- public function testCustomTransferType()
- {
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 2,
- amount: 10.0,
- orderId: 12345,
- transferType: 'custom_transfer'
- );
- $this->assertTrue($result, '自定义转移类型应该成功');
- }
- /**
- * 测试无效金额
- */
- public function testInvalidAmount()
- {
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 2,
- amount: 0,
- orderId: 12345
- );
- $this->assertEquals('转移金额必须大于0', $result);
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 2,
- amount: -10.0,
- orderId: 12345
- );
- $this->assertEquals('转移金额必须大于0', $result);
- }
- /**
- * 测试应用不存在
- */
- public function testAppNotExists()
- {
- $result = FeeService::transfer(
- appId: 999,
- targetUserId: 2,
- amount: 10.0,
- orderId: 12345
- );
- $this->assertEquals('应用不存在', $result);
- }
- /**
- * 测试目标用户不存在
- */
- public function testTargetUserNotExists()
- {
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 999,
- amount: 10.0,
- orderId: 12345
- );
- $this->assertEquals('目标用户不存在', $result);
- }
- /**
- * 测试余额不足
- */
- public function testInsufficientBalance()
- {
- // 清空手续费账户余额
- DB::table('kku_fund')
- ->where('user_id', 1)
- ->where('fund_id', 1)
- ->update(['balance' => 0.0000]);
- $result = FeeService::transfer(
- appId: 1,
- targetUserId: 2,
- amount: 10.0,
- orderId: 12345
- );
- $this->assertIsString($result);
- $this->assertStringContains('not-sufficient-funds', $result);
- }
- }
|