| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace App\Module\Mex\Tests;
- use App\Module\Mex\Logic\MexAccountLogic;
- use App\Module\Mex\Logic\MexOrderLogic;
- use App\Module\Mex\Models\MexPriceConfig;
- use App\Module\Fund\Services\FundService;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\GameItems\Services\ItemService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- /**
- * Mex账户流转逻辑测试
- */
- class MexAccountLogicTest extends TestCase
- {
- use RefreshDatabase;
- private int $testUserId = 1001;
- private int $testItemId = 10001;
- private int $warehouseUserId = 15;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- // 初始化测试数据
- $this->initTestData();
- }
- /**
- * 初始化测试数据
- */
- private function initTestData(): void
- {
- // 创建价格配置
- MexPriceConfig::create([
- 'item_id' => $this->testItemId,
- 'min_price' => '1.00000',
- 'max_price' => '10.00000',
- 'protection_threshold' => 1000,
- 'is_enabled' => true,
- ]);
- // 给测试用户添加物品
- ItemService::addItem($this->testUserId, $this->testItemId, 100, [
- 'reason' => 'test_init',
- 'remark' => '测试初始化物品'
- ]);
- // 给仓库用户添加资金
- $warehouseFundService = new FundService($this->warehouseUserId, FUND_TYPE::FUND1);
- $warehouseFundService->admin_operate(
- 1, // admin_id
- FUND_TYPE::FUND1,
- 1000000, // 10000.00元
- '测试初始化仓库资金'
- );
- // 给测试用户添加资金
- $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
- $userFundService->admin_operate(
- 1, // admin_id
- FUND_TYPE::FUND1,
- 500000, // 5000.00元
- '测试初始化用户资金'
- );
- }
- /**
- * 测试用户卖出订单处理
- */
- public function testUserSellOrder(): void
- {
- // 创建卖出订单
- $result = MexOrderLogic::createSellOrder(
- $this->testUserId,
- $this->testItemId,
- 10,
- '2.50000'
- );
- $this->assertTrue($result['success'], '卖出订单创建应该成功');
- $this->assertArrayHasKey('order_id', $result);
- $this->assertArrayHasKey('result', $result);
-
- $orderResult = $result['result'];
- $this->assertTrue($orderResult['success'], '卖出订单处理应该成功');
- $this->assertArrayHasKey('transaction_id', $orderResult);
- // 验证用户物品减少
- $userItems = ItemService::getUserItems($this->testUserId, ['item_id' => $this->testItemId]);
- $totalQuantity = $userItems->sum('quantity');
- $this->assertEquals(90, $totalQuantity, '用户物品应该减少10个');
- // 验证用户资金增加
- $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
- $userBalance = $userFundService->balance();
- $this->assertGreaterThan(500000, $userBalance, '用户资金应该增加');
- }
- /**
- * 测试用户买入订单处理
- */
- public function testUserBuyOrder(): void
- {
- // 先给仓库添加一些物品
- ItemService::addItem($this->warehouseUserId, $this->testItemId, 50, [
- 'reason' => 'test_warehouse_stock',
- 'remark' => '测试仓库库存'
- ]);
- // 创建买入订单
- $result = MexOrderLogic::createBuyOrder(
- $this->testUserId,
- $this->testItemId,
- 5,
- '8.00000'
- );
- $this->assertTrue($result['success'], '买入订单创建应该成功');
- $this->assertArrayHasKey('order_id', $result);
- $this->assertArrayHasKey('result', $result);
-
- $orderResult = $result['result'];
- $this->assertTrue($orderResult['success'], '买入订单处理应该成功');
- $this->assertArrayHasKey('transaction_id', $orderResult);
- // 验证用户物品增加
- $userItems = ItemService::getUserItems($this->testUserId, ['item_id' => $this->testItemId]);
- $totalQuantity = $userItems->sum('quantity');
- $this->assertEquals(105, $totalQuantity, '用户物品应该增加5个');
- // 验证用户资金减少
- $userFundService = new FundService($this->testUserId, FUND_TYPE::FUND1);
- $userBalance = $userFundService->balance();
- $this->assertLessThan(500000, $userBalance, '用户资金应该减少');
- }
- /**
- * 测试资金不足的买入订单
- */
- public function testBuyOrderInsufficientFunds(): void
- {
- // 先给仓库添加一些物品
- ItemService::addItem($this->warehouseUserId, $this->testItemId, 50, [
- 'reason' => 'test_warehouse_stock',
- 'remark' => '测试仓库库存'
- ]);
- // 尝试创建超出资金能力的买入订单
- $result = MexOrderLogic::createBuyOrder(
- $this->testUserId,
- $this->testItemId,
- 1000, // 大量购买
- '10.00000' // 高价
- );
- $this->assertTrue($result['success'], '订单创建应该成功');
-
- $orderResult = $result['result'];
- $this->assertFalse($orderResult['success'], '订单处理应该失败');
- $this->assertStringContainsString('资金不足', $orderResult['message']);
- }
- /**
- * 测试物品不足的卖出订单
- */
- public function testSellOrderInsufficientItems(): void
- {
- // 尝试卖出超过拥有数量的物品
- $result = MexOrderLogic::createSellOrder(
- $this->testUserId,
- $this->testItemId,
- 200, // 超过拥有的100个
- '2.50000'
- );
- $this->assertTrue($result['success'], '订单创建应该成功');
-
- $orderResult = $result['result'];
- $this->assertFalse($orderResult['success'], '订单处理应该失败');
- $this->assertStringContainsString('不足', $orderResult['message']);
- }
- }
|