| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace App\Module\Transfer\Tests\Unit;
- use App\Module\Transfer\Services\TransferService;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Enums\TransferType;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Exceptions\TransferException;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- /**
- * Transfer服务单元测试
- */
- class TransferServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected TransferApp $testApp;
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试应用
- $this->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,
- 'business_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
- 'business_id' => 'TEST_INVALID',
- 'user_id' => 1,
- 'amount' => '100.0000000000',
- ];
- TransferService::createTransferIn($data);
- }
- /**
- * 测试重复业务ID的情况
- */
- public function test_create_order_with_duplicate_business_id()
- {
- $businessId = 'TEST_DUPLICATE_' . time();
-
- // 创建第一个订单
- $data1 = [
- 'transfer_app_id' => $this->testApp->id,
- 'business_id' => $businessId,
- 'user_id' => 1,
- 'amount' => '100.0000000000',
- ];
-
- TransferService::createTransferIn($data1);
- // 尝试创建重复业务ID的订单
- $this->expectException(TransferException::class);
-
- $data2 = [
- 'transfer_app_id' => $this->testApp->id,
- 'business_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,
- 'business_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,
- 'business_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,
- 'business_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);
- }
- }
|