|
|
@@ -0,0 +1,210 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Mex\Tests;
|
|
|
+
|
|
|
+use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
|
|
|
+use App\Module\GameItems\Enums\FREEZE_ACTION_TYPE;
|
|
|
+use App\Module\GameItems\Models\ItemFreezeLog;
|
|
|
+use App\Module\GameItems\Models\ItemUser;
|
|
|
+use App\Module\GameItems\Services\ItemService;
|
|
|
+use App\Module\Mex\Enums\OrderStatus;
|
|
|
+use App\Module\Mex\Enums\OrderType;
|
|
|
+use App\Module\Mex\Models\MexOrder;
|
|
|
+use App\Module\Mex\Services\MexOrderService;
|
|
|
+use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 农贸市场订单取消测试
|
|
|
+ *
|
|
|
+ * 测试订单取消时的解冻功能
|
|
|
+ */
|
|
|
+class MexOrderCancelTest extends TestCase
|
|
|
+{
|
|
|
+ use RefreshDatabase;
|
|
|
+
|
|
|
+ private int $testUserId = 1001;
|
|
|
+ private int $testItemId = 10001;
|
|
|
+
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp();
|
|
|
+
|
|
|
+ // 准备测试数据
|
|
|
+ $this->prepareTestData();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 准备测试数据
|
|
|
+ */
|
|
|
+ private function prepareTestData(): void
|
|
|
+ {
|
|
|
+ // 创建测试用户物品
|
|
|
+ ItemUser::create([
|
|
|
+ 'user_id' => $this->testUserId,
|
|
|
+ 'item_id' => $this->testItemId,
|
|
|
+ 'instance_id' => null,
|
|
|
+ 'quantity' => 100,
|
|
|
+ 'is_frozen' => false,
|
|
|
+ 'expire_at' => now()->addDays(30),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 创建价格配置
|
|
|
+ \App\Module\Mex\Models\MexPriceConfig::create([
|
|
|
+ 'item_id' => $this->testItemId,
|
|
|
+ 'min_price' => 1.0,
|
|
|
+ 'max_price' => 100.0,
|
|
|
+ 'protection_threshold' => 10,
|
|
|
+ 'is_enabled' => true,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试取消卖出订单时解冻物品
|
|
|
+ */
|
|
|
+ public function testCancelSellOrderUnfreezeItems(): void
|
|
|
+ {
|
|
|
+ // 1. 创建卖出订单(会冻结物品)
|
|
|
+ $createResult = \DB::transaction(function () {
|
|
|
+ return \App\Module\Mex\Logic\MexOrderLogic::createSellOrder(
|
|
|
+ $this->testUserId,
|
|
|
+ $this->testItemId,
|
|
|
+ 20,
|
|
|
+ 10.5,
|
|
|
+ FUND_CURRENCY_TYPE::FUND2
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertTrue($createResult['success'], '创建卖出订单失败:' . ($createResult['message'] ?? ''));
|
|
|
+ $orderId = $createResult['order_id'];
|
|
|
+
|
|
|
+ // 2. 验证物品已被冻结
|
|
|
+ $frozenItems = ItemUser::where('user_id', $this->testUserId)
|
|
|
+ ->where('item_id', $this->testItemId)
|
|
|
+ ->where('is_frozen', true)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $this->assertGreaterThan(0, $frozenItems->count(), '物品应该被冻结');
|
|
|
+
|
|
|
+ // 3. 验证冻结记录存在
|
|
|
+ $freezeLog = ItemFreezeLog::where('source_id', $orderId)
|
|
|
+ ->where('source_type', 'mex_sell_order')
|
|
|
+ ->where('action_type', FREEZE_ACTION_TYPE::FREEZE)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $this->assertNotNull($freezeLog, '应该存在冻结记录');
|
|
|
+
|
|
|
+ // 4. 取消订单
|
|
|
+ $cancelResult = MexOrderService::cancelOrder($this->testUserId, $orderId);
|
|
|
+
|
|
|
+ $this->assertTrue($cancelResult['success'], '取消订单失败:' . ($cancelResult['message'] ?? ''));
|
|
|
+
|
|
|
+ // 5. 验证订单状态已更新
|
|
|
+ $order = MexOrder::find($orderId);
|
|
|
+ $this->assertEquals(OrderStatus::CANCELLED, $order->status, '订单状态应该为已取消');
|
|
|
+
|
|
|
+ // 6. 验证物品已解冻
|
|
|
+ $frozenItemsAfterCancel = ItemUser::where('user_id', $this->testUserId)
|
|
|
+ ->where('item_id', $this->testItemId)
|
|
|
+ ->where('is_frozen', true)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $this->assertEquals(0, $frozenItemsAfterCancel->count(), '物品应该已解冻');
|
|
|
+
|
|
|
+ // 7. 验证解冻记录存在
|
|
|
+ $unfreezeLog = ItemFreezeLog::where('source_id', $orderId)
|
|
|
+ ->where('source_type', 'mex_sell_order')
|
|
|
+ ->where('action_type', FREEZE_ACTION_TYPE::UNFREEZE)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $this->assertNotNull($unfreezeLog, '应该存在解冻记录');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试取消买入订单
|
|
|
+ */
|
|
|
+ public function testCancelBuyOrder(): void
|
|
|
+ {
|
|
|
+ // 1. 创建买入订单
|
|
|
+ $createResult = \DB::transaction(function () {
|
|
|
+ return \App\Module\Mex\Logic\MexOrderLogic::createBuyOrder(
|
|
|
+ $this->testUserId,
|
|
|
+ $this->testItemId,
|
|
|
+ 10,
|
|
|
+ 15.0,
|
|
|
+ FUND_CURRENCY_TYPE::FUND2
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertTrue($createResult['success'], '创建买入订单失败:' . ($createResult['message'] ?? ''));
|
|
|
+ $orderId = $createResult['order_id'];
|
|
|
+
|
|
|
+ // 2. 取消订单
|
|
|
+ $cancelResult = MexOrderService::cancelOrder($this->testUserId, $orderId);
|
|
|
+
|
|
|
+ $this->assertTrue($cancelResult['success'], '取消订单失败:' . ($cancelResult['message'] ?? ''));
|
|
|
+
|
|
|
+ // 3. 验证订单状态已更新
|
|
|
+ $order = MexOrder::find($orderId);
|
|
|
+ $this->assertEquals(OrderStatus::CANCELLED, $order->status, '订单状态应该为已取消');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试取消不存在的订单
|
|
|
+ */
|
|
|
+ public function testCancelNonExistentOrder(): void
|
|
|
+ {
|
|
|
+ $cancelResult = MexOrderService::cancelOrder($this->testUserId, 99999);
|
|
|
+
|
|
|
+ $this->assertFalse($cancelResult['success'], '取消不存在的订单应该失败');
|
|
|
+ $this->assertStringContains('订单不存在', $cancelResult['message']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试取消已完成的订单
|
|
|
+ */
|
|
|
+ public function testCancelCompletedOrder(): void
|
|
|
+ {
|
|
|
+ // 1. 创建订单
|
|
|
+ $order = MexOrder::create([
|
|
|
+ 'user_id' => $this->testUserId,
|
|
|
+ 'item_id' => $this->testItemId,
|
|
|
+ 'currency_type' => FUND_CURRENCY_TYPE::FUND2,
|
|
|
+ 'order_type' => OrderType::SELL,
|
|
|
+ 'quantity' => 10,
|
|
|
+ 'price' => 10.0,
|
|
|
+ 'total_amount' => 100.0,
|
|
|
+ 'status' => OrderStatus::COMPLETED, // 已完成状态
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 2. 尝试取消已完成的订单
|
|
|
+ $cancelResult = MexOrderService::cancelOrder($this->testUserId, $order->id);
|
|
|
+
|
|
|
+ $this->assertFalse($cancelResult['success'], '取消已完成的订单应该失败');
|
|
|
+ $this->assertStringContains('只能取消等待中的订单', $cancelResult['message']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试取消其他用户的订单
|
|
|
+ */
|
|
|
+ public function testCancelOtherUserOrder(): void
|
|
|
+ {
|
|
|
+ // 1. 创建其他用户的订单
|
|
|
+ $order = MexOrder::create([
|
|
|
+ 'user_id' => 9999, // 其他用户
|
|
|
+ 'item_id' => $this->testItemId,
|
|
|
+ 'currency_type' => FUND_CURRENCY_TYPE::FUND2,
|
|
|
+ 'order_type' => OrderType::SELL,
|
|
|
+ 'quantity' => 10,
|
|
|
+ 'price' => 10.0,
|
|
|
+ 'total_amount' => 100.0,
|
|
|
+ 'status' => OrderStatus::PENDING,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 2. 尝试取消其他用户的订单
|
|
|
+ $cancelResult = MexOrderService::cancelOrder($this->testUserId, $order->id);
|
|
|
+
|
|
|
+ $this->assertFalse($cancelResult['success'], '取消其他用户的订单应该失败');
|
|
|
+ $this->assertStringContains('订单不存在', $cancelResult['message']);
|
|
|
+ }
|
|
|
+}
|