testApp = TransferApp::create([ 'keyname' => 'test_app', 'title' => '测试应用', 'description' => '用于单元测试的应用', 'out_id' => 1001, 'currency_id' => 2, 'fund_id' => 2, 'exchange_rate' => 1.0000, 'is_enabled' => true, ]); } /** * 测试创建转入订单 */ public function test_create_transfer_in_order() { $data = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_IN_' . time(), 'user_id' => 1, 'amount' => '100.0000000000', 'remark' => '测试转入', ]; $result = TransferService::createTransferIn($data); $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result); $this->assertEquals(TransferType::IN, $result->type); $this->assertEquals(TransferStatus::CREATED, $result->status); $this->assertEquals('100.0000000000', $result->amount); } /** * 测试创建转出订单 */ public function test_create_transfer_out_order() { $data = [ 'transfer_app_id' => $this->testApp->id, 'user_id' => 1, 'amount' => '50.0000000000', 'password' => 'test123', 'remark' => '测试转出', ]; $result = TransferService::createTransferOut($data); $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result); $this->assertEquals(TransferType::OUT, $result->type); $this->assertEquals(TransferStatus::CREATED, $result->status); $this->assertEquals('50.0000000000', $result->amount); } /** * 测试获取订单详情 */ public function test_get_order_detail() { // 创建测试订单 $order = TransferOrder::create([ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_ORDER_' . time(), 'user_id' => 1, 'type' => TransferType::IN, 'status' => TransferStatus::COMPLETED, 'out_amount' => '100.0000000000', 'amount' => '100.0000000000', 'exchange_rate' => 1.0000, 'currency_id' => 2, 'fund_id' => 2, ]); $result = TransferService::getOrderDetail($order->id); $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result); $this->assertEquals($order->id, $result->id); $this->assertEquals($order->out_order_id, $result->outOrderId); } /** * 测试获取用户订单列表 */ public function test_get_user_orders() { $userId = 1; // 创建多个测试订单 for ($i = 1; $i <= 3; $i++) { TransferOrder::create([ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_ORDER_' . $i, 'user_id' => $userId, 'type' => $i % 2 == 0 ? TransferType::OUT : TransferType::IN, 'status' => TransferStatus::COMPLETED, 'out_amount' => '100.0000000000', 'amount' => '100.0000000000', 'exchange_rate' => 1.0000, 'currency_id' => 2, 'fund_id' => 2, ]); } $result = TransferService::getUserOrders($userId); $this->assertIsArray($result); $this->assertCount(3, $result); $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferOrderDto::class, $result[0]); } /** * 测试应用不存在的情况 */ public function test_create_order_with_invalid_app() { $this->expectException(TransferException::class); $data = [ 'transfer_app_id' => 99999, // 不存在的应用ID 'out_order_id' => 'TEST_INVALID', 'user_id' => 1, 'amount' => '100.0000000000', ]; TransferService::createTransferIn($data); } /** * 测试重复业务ID的情况 */ public function test_create_order_with_duplicate_out_order_id() { $businessId = 'TEST_DUPLICATE_' . time(); // 创建第一个订单 $data1 = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => $businessId, 'user_id' => 1, 'amount' => '100.0000000000', ]; TransferService::createTransferIn($data1); // 尝试创建重复业务ID的订单 $this->expectException(TransferException::class); $data2 = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => $businessId, 'user_id' => 2, 'amount' => '200.0000000000', ]; TransferService::createTransferIn($data2); } /** * 测试金额验证 */ public function test_create_order_with_invalid_amount() { $this->expectException(TransferException::class); $data = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_INVALID_AMOUNT', 'user_id' => 1, 'amount' => '-100.0000000000', // 负数金额 ]; TransferService::createTransferIn($data); } /** * 测试禁用应用的情况 */ public function test_create_order_with_disabled_app() { // 禁用应用 $this->testApp->update(['is_enabled' => false]); $this->expectException(TransferException::class); $data = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_DISABLED_APP', 'user_id' => 1, 'amount' => '100.0000000000', ]; TransferService::createTransferIn($data); } /** * 测试汇率计算 */ public function test_exchange_rate_calculation() { // 设置汇率为2.0 $this->testApp->update(['exchange_rate' => 2.0000]); $data = [ 'transfer_app_id' => $this->testApp->id, 'out_order_id' => 'TEST_EXCHANGE_RATE', 'user_id' => 1, 'amount' => '100.0000000000', // 外部金额 ]; $result = TransferService::createTransferIn($data); // 内部金额应该是外部金额除以汇率 $this->assertEquals('50.0000000000', $result->amount); $this->assertEquals('100.0000000000', $result->outAmount); $this->assertEquals(2.0000, $result->exchangeRate); } /** * 测试获取应用配置 */ public function test_get_app_config() { $result = TransferService::getAppConfig($this->testApp->id); $this->assertInstanceOf(\App\Module\Transfer\Dtos\TransferAppDto::class, $result); $this->assertEquals($this->testApp->id, $result->id); $this->assertEquals($this->testApp->keyname, $result->keyname); $this->assertEquals($this->testApp->title, $result->title); } /** * 测试获取不存在的应用配置 */ public function test_get_invalid_app_config() { $this->expectException(TransferException::class); TransferService::getAppConfig(99999); } }