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); } }