MexAccountLogicTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Module\Mex\Tests;
  3. use App\Module\Mex\Logic\MexAccountLogic;
  4. use App\Module\Mex\Logic\MexOrderLogic;
  5. use App\Module\Mex\Models\MexPriceConfig;
  6. use App\Module\Fund\Services\FundService;
  7. use App\Module\Fund\Enums\FUND_TYPE;
  8. use App\Module\GameItems\Services\ItemService;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\TestCase;
  11. /**
  12. * Mex账户流转逻辑测试
  13. */
  14. class MexAccountLogicTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private int $testUserId = 1001;
  18. private int $testItemId = 10001;
  19. private int $warehouseUserId = 15;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. // 初始化测试数据
  24. $this->initTestData();
  25. }
  26. /**
  27. * 初始化测试数据
  28. */
  29. private function initTestData(): void
  30. {
  31. // 创建价格配置
  32. MexPriceConfig::create([
  33. 'item_id' => $this->testItemId,
  34. 'min_price' => '1.00000',
  35. 'max_price' => '10.00000',
  36. 'protection_threshold' => 1000,
  37. 'is_enabled' => true,
  38. ]);
  39. // 给测试用户添加物品
  40. ItemService::addItem($this->testUserId, $this->testItemId, 100, [
  41. 'reason' => 'test_init',
  42. 'remark' => '测试初始化物品'
  43. ]);
  44. // 给仓库用户添加资金
  45. $warehouseFundService = new FundService($this->warehouseUserId, FUND_TYPE::FUND1);
  46. $warehouseFundService->admin_operate(
  47. 1, // admin_id
  48. FUND_TYPE::FUND1,
  49. 1000000, // 10000.00元
  50. '测试初始化仓库资金'
  51. );
  52. // 给测试用户添加资金
  53. $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
  54. $userFundService->admin_operate(
  55. 1, // admin_id
  56. FUND_TYPE::FUND1,
  57. 500000, // 5000.00元
  58. '测试初始化用户资金'
  59. );
  60. }
  61. /**
  62. * 测试用户卖出订单处理
  63. */
  64. public function testUserSellOrder(): void
  65. {
  66. // 创建卖出订单
  67. $result = MexOrderLogic::createSellOrder(
  68. $this->testUserId,
  69. $this->testItemId,
  70. 10,
  71. '2.50000'
  72. );
  73. $this->assertTrue($result['success'], '卖出订单创建应该成功');
  74. $this->assertArrayHasKey('order_id', $result);
  75. $this->assertArrayHasKey('result', $result);
  76. $orderResult = $result['result'];
  77. $this->assertTrue($orderResult['success'], '卖出订单处理应该成功');
  78. $this->assertArrayHasKey('transaction_id', $orderResult);
  79. // 验证用户物品减少
  80. $userItems = ItemService::getUserItems($this->testUserId, ['item_id' => $this->testItemId]);
  81. $totalQuantity = $userItems->sum('quantity');
  82. $this->assertEquals(90, $totalQuantity, '用户物品应该减少10个');
  83. // 验证用户资金增加
  84. $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
  85. $userBalance = $userFundService->balance();
  86. $this->assertGreaterThan(500000, $userBalance, '用户资金应该增加');
  87. }
  88. /**
  89. * 测试用户买入订单处理
  90. */
  91. public function testUserBuyOrder(): void
  92. {
  93. // 先给仓库添加一些物品
  94. ItemService::addItem($this->warehouseUserId, $this->testItemId, 50, [
  95. 'reason' => 'test_warehouse_stock',
  96. 'remark' => '测试仓库库存'
  97. ]);
  98. // 创建买入订单
  99. $result = MexOrderLogic::createBuyOrder(
  100. $this->testUserId,
  101. $this->testItemId,
  102. 5,
  103. '8.00000'
  104. );
  105. $this->assertTrue($result['success'], '买入订单创建应该成功');
  106. $this->assertArrayHasKey('order_id', $result);
  107. $this->assertArrayHasKey('result', $result);
  108. $orderResult = $result['result'];
  109. $this->assertTrue($orderResult['success'], '买入订单处理应该成功');
  110. $this->assertArrayHasKey('transaction_id', $orderResult);
  111. // 验证用户物品增加
  112. $userItems = ItemService::getUserItems($this->testUserId, ['item_id' => $this->testItemId]);
  113. $totalQuantity = $userItems->sum('quantity');
  114. $this->assertEquals(105, $totalQuantity, '用户物品应该增加5个');
  115. // 验证用户资金减少
  116. $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
  117. $userBalance = $userFundService->balance();
  118. $this->assertLessThan(500000, $userBalance, '用户资金应该减少');
  119. }
  120. /**
  121. * 测试资金不足的买入订单
  122. */
  123. public function testBuyOrderInsufficientFunds(): void
  124. {
  125. // 先给仓库添加一些物品
  126. ItemService::addItem($this->warehouseUserId, $this->testItemId, 50, [
  127. 'reason' => 'test_warehouse_stock',
  128. 'remark' => '测试仓库库存'
  129. ]);
  130. // 尝试创建超出资金能力的买入订单
  131. $result = MexOrderLogic::createBuyOrder(
  132. $this->testUserId,
  133. $this->testItemId,
  134. 1000, // 大量购买
  135. '10.00000' // 高价
  136. );
  137. $this->assertTrue($result['success'], '订单创建应该成功');
  138. $orderResult = $result['result'];
  139. $this->assertFalse($orderResult['success'], '订单处理应该失败');
  140. $this->assertStringContainsString('资金不足', $orderResult['message']);
  141. }
  142. /**
  143. * 测试物品不足的卖出订单
  144. */
  145. public function testSellOrderInsufficientItems(): void
  146. {
  147. // 尝试卖出超过拥有数量的物品
  148. $result = MexOrderLogic::createSellOrder(
  149. $this->testUserId,
  150. $this->testItemId,
  151. 200, // 超过拥有的100个
  152. '2.50000'
  153. );
  154. $this->assertTrue($result['success'], '订单创建应该成功');
  155. $orderResult = $result['result'];
  156. $this->assertFalse($orderResult['success'], '订单处理应该失败');
  157. $this->assertStringContainsString('不足', $orderResult['message']);
  158. }
  159. }