| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace App\Module\Mex\Tests;
- use App\Module\Mex\Logic\MexMatchLogic;
- use App\Module\Mex\Models\MexOrder;
- use App\Module\Mex\Models\MexWarehouse;
- use App\Module\Mex\Models\MexPriceConfig;
- use App\Module\Mex\Enums\OrderType;
- use App\Module\Mex\Enums\OrderStatus;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Tests\TestCase;
- /**
- * Mex撮合逻辑测试
- *
- * 测试用户买入物品和用户卖出物品的撮合逻辑
- */
- class MexMatchLogicTest extends TestCase
- {
- use RefreshDatabase, WithFaker;
- /**
- * 测试用户买入物品撮合条件检查
- */
- public function test_check_user_buy_item_match_conditions()
- {
- // 创建测试数据
- $itemId = 1001;
-
- // 测试没有价格配置的情况
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('未配置价格信息', $result['message']);
- // 创建价格配置
- MexPriceConfig::create([
- 'item_id' => $itemId,
- 'min_price' => '10.00000',
- 'max_price' => '20.00000',
- 'protection_threshold' => 300,
- 'is_enabled' => true,
- ]);
- // 测试没有仓库库存的情况
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('仓库库存不足', $result['message']);
- // 创建仓库库存
- MexWarehouse::create([
- 'item_id' => $itemId,
- 'quantity' => 1000,
- ]);
- // 测试没有符合条件订单的情况
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('没有符合条件的待撮合用户买入物品订单', $result['message']);
- // 创建符合条件的用户买入物品订单
- MexOrder::create([
- 'user_id' => 1,
- 'item_id' => $itemId,
- 'order_type' => OrderType::BUY,
- 'quantity' => 100,
- 'price' => '20.00000', // 等于最高价
- 'total_amount' => '2000.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 测试条件满足的情况
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertTrue($result['can_match']);
- $this->assertStringContains('撮合条件满足', $result['message']);
- }
- /**
- * 测试用户卖出物品撮合条件检查
- */
- public function test_check_user_sell_item_match_conditions()
- {
- // 创建测试数据
- $itemId = 1002;
-
- // 测试没有价格配置的情况
- $result = MexMatchLogic::checkUserSellItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('未配置价格信息', $result['message']);
- // 创建价格配置
- MexPriceConfig::create([
- 'item_id' => $itemId,
- 'min_price' => '10.00000',
- 'max_price' => '20.00000',
- 'protection_threshold' => 300,
- 'is_enabled' => true,
- ]);
- // 测试没有待撮合订单的情况
- $result = MexMatchLogic::checkUserSellItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('没有待撮合的用户卖出物品订单', $result['message']);
- // 创建用户卖出物品订单
- MexOrder::create([
- 'user_id' => 2,
- 'item_id' => $itemId,
- 'order_type' => OrderType::SELL,
- 'quantity' => 50,
- 'price' => '10.00000', // 等于最低价
- 'total_amount' => '500.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 测试条件满足的情况
- $result = MexMatchLogic::checkUserSellItemMatchConditions($itemId);
- $this->assertTrue($result['can_match']);
- $this->assertStringContains('撮合条件满足', $result['message']);
- }
- /**
- * 测试用户买入物品撮合统计
- */
- public function test_get_user_buy_item_match_stats()
- {
- // 创建测试订单
- MexOrder::create([
- 'user_id' => 1,
- 'item_id' => 1003,
- 'order_type' => OrderType::BUY,
- 'quantity' => 100,
- 'price' => '15.00000',
- 'total_amount' => '1500.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- MexOrder::create([
- 'user_id' => 2,
- 'item_id' => 1004,
- 'order_type' => OrderType::BUY,
- 'quantity' => 200,
- 'price' => '25.00000',
- 'total_amount' => '5000.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- $stats = MexMatchLogic::getUserBuyItemMatchStats();
- $this->assertEquals(2, $stats['pending_orders']);
- $this->assertEquals(2, $stats['pending_items']);
- $this->assertEquals(300, $stats['pending_quantity']);
- $this->assertEquals('6500.00000', $stats['pending_amount']);
- }
- /**
- * 测试用户卖出物品撮合统计
- */
- public function test_get_user_sell_item_match_stats()
- {
- // 创建测试订单
- MexOrder::create([
- 'user_id' => 3,
- 'item_id' => 1005,
- 'order_type' => OrderType::SELL,
- 'quantity' => 150,
- 'price' => '12.00000',
- 'total_amount' => '1800.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- $stats = MexMatchLogic::getUserSellItemMatchStats();
- $this->assertEquals(1, $stats['pending_orders']);
- $this->assertEquals(1, $stats['pending_items']);
- $this->assertEquals(150, $stats['pending_quantity']);
- $this->assertEquals('1800.00000', $stats['pending_amount']);
- }
- /**
- * 测试价格验证逻辑
- */
- public function test_price_validation_logic()
- {
- $itemId = 1006;
-
- // 创建价格配置
- MexPriceConfig::create([
- 'item_id' => $itemId,
- 'min_price' => '10.00000',
- 'max_price' => '20.00000',
- 'protection_threshold' => 300,
- 'is_enabled' => true,
- ]);
- // 创建仓库库存
- MexWarehouse::create([
- 'item_id' => $itemId,
- 'quantity' => 1000,
- ]);
- // 创建价格低于最高价的用户买入物品订单(不应该被撮合)
- MexOrder::create([
- 'user_id' => 1,
- 'item_id' => $itemId,
- 'order_type' => OrderType::BUY,
- 'quantity' => 100,
- 'price' => '15.00000', // 低于最高价20.00000
- 'total_amount' => '1500.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 检查撮合条件
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('没有符合条件的待撮合用户买入物品订单', $result['message']);
- // 创建价格等于最高价的用户买入物品订单(应该被撮合)
- MexOrder::create([
- 'user_id' => 2,
- 'item_id' => $itemId,
- 'order_type' => OrderType::BUY,
- 'quantity' => 100,
- 'price' => '20.00000', // 等于最高价
- 'total_amount' => '2000.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 检查撮合条件
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertTrue($result['can_match']);
- $this->assertEquals(1, $result['pending_orders']); // 只有一个符合条件的订单
- }
- /**
- * 测试数量保护机制
- */
- public function test_quantity_protection_mechanism()
- {
- $itemId = 1007;
-
- // 创建价格配置,保护阈值为300
- MexPriceConfig::create([
- 'item_id' => $itemId,
- 'min_price' => '10.00000',
- 'max_price' => '20.00000',
- 'protection_threshold' => 300,
- 'is_enabled' => true,
- ]);
- // 创建仓库库存
- MexWarehouse::create([
- 'item_id' => $itemId,
- 'quantity' => 1000,
- ]);
- // 创建超过保护阈值的用户买入物品订单(不应该被撮合)
- MexOrder::create([
- 'user_id' => 1,
- 'item_id' => $itemId,
- 'order_type' => OrderType::BUY,
- 'quantity' => 500, // 超过保护阈值300
- 'price' => '20.00000',
- 'total_amount' => '10000.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 检查撮合条件
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertFalse($result['can_match']);
- $this->assertStringContains('没有符合条件的待撮合用户买入物品订单', $result['message']);
- // 创建不超过保护阈值的用户买入物品订单(应该被撮合)
- MexOrder::create([
- 'user_id' => 2,
- 'item_id' => $itemId,
- 'order_type' => OrderType::BUY,
- 'quantity' => 200, // 不超过保护阈值300
- 'price' => '20.00000',
- 'total_amount' => '4000.00000',
- 'status' => OrderStatus::PENDING,
- ]);
- // 检查撮合条件
- $result = MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- $this->assertTrue($result['can_match']);
- $this->assertEquals(1, $result['pending_orders']); // 只有一个符合条件的订单
- }
- }
|