| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace Tests\Unit\GameItems;
- use App\Module\GameItems\Enums\FREEZE_REASON_TYPE;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\GameItems\Models\ItemUser;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\DB;
- use Tests\TestCase;
- /**
- * 物品消耗冻结功能测试
- *
- * 测试物品消耗时是否正确处理include_frozen参数
- */
- class ItemConsumeFrozenTest extends TestCase
- {
- use RefreshDatabase;
- private int $testUserId = 99999;
- private int $testItemId = 1001;
- protected function setUp(): void
- {
- parent::setUp();
-
- // 创建测试用户物品
- DB::transaction(function () {
- // 添加100个未冻结物品
- ItemUser::create([
- 'user_id' => $this->testUserId,
- 'item_id' => $this->testItemId,
- 'instance_id' => null,
- 'quantity' => 100,
- 'is_frozen' => false,
- 'frozen_log_id' => null,
- ]);
- // 添加50个冻结物品
- ItemUser::create([
- 'user_id' => $this->testUserId,
- 'item_id' => $this->testItemId,
- 'instance_id' => null,
- 'quantity' => 50,
- 'is_frozen' => true,
- 'frozen_log_id' => 1, // 假设的冻结日志ID
- ]);
- });
- }
- /**
- * 测试不包含冻结物品的消耗(默认行为)
- */
- public function testConsumeItemWithoutFrozen()
- {
- DB::transaction(function () {
- // 消耗80个物品,不包含冻结物品
- $result = ItemService::consumeItem(
- $this->testUserId,
- $this->testItemId,
- null,
- 80,
- ['source_type' => 'test', 'source_id' => 1]
- );
- $this->assertTrue($result['success']);
- $this->assertEquals($this->testItemId, $result['item_id']);
- $this->assertEquals(80, $result['quantity']);
- // 验证剩余数量:应该还有20个未冻结物品,50个冻结物品不变
- $remainingItems = ItemUser::where('user_id', $this->testUserId)
- ->where('item_id', $this->testItemId)
- ->get();
- $unfrozenQuantity = $remainingItems->where('is_frozen', false)->sum('quantity');
- $frozenQuantity = $remainingItems->where('is_frozen', true)->sum('quantity');
- $this->assertEquals(20, $unfrozenQuantity); // 100 - 80 = 20
- $this->assertEquals(50, $frozenQuantity); // 冻结物品不变
- });
- }
- /**
- * 测试包含冻结物品的消耗
- */
- public function testConsumeItemWithFrozen()
- {
- DB::transaction(function () {
- // 消耗120个物品,包含冻结物品
- $result = ItemService::consumeItem(
- $this->testUserId,
- $this->testItemId,
- null,
- 120,
- [
- 'source_type' => 'test',
- 'source_id' => 1,
- 'include_frozen' => true // 包含冻结物品
- ]
- );
- $this->assertTrue($result['success']);
- $this->assertEquals($this->testItemId, $result['item_id']);
- $this->assertEquals(120, $result['quantity']);
- // 验证剩余数量:应该还有30个物品(100+50-120=30)
- $remainingQuantity = ItemUser::where('user_id', $this->testUserId)
- ->where('item_id', $this->testItemId)
- ->sum('quantity');
- $this->assertEquals(30, $remainingQuantity);
- });
- }
- /**
- * 测试不包含冻结物品时数量不足的情况
- */
- public function testConsumeItemInsufficientWithoutFrozen()
- {
- $this->expectException(\Exception::class);
- $this->expectExceptionMessage('数量不足');
- DB::transaction(function () {
- // 尝试消耗120个物品,但只有100个未冻结物品
- ItemService::consumeItem(
- $this->testUserId,
- $this->testItemId,
- null,
- 120,
- ['source_type' => 'test', 'source_id' => 1]
- );
- });
- }
- /**
- * 测试包含冻结物品时数量足够的情况
- */
- public function testConsumeItemSufficientWithFrozen()
- {
- DB::transaction(function () {
- // 消耗150个物品,总共有150个物品(100未冻结+50冻结)
- $result = ItemService::consumeItem(
- $this->testUserId,
- $this->testItemId,
- null,
- 150,
- [
- 'source_type' => 'test',
- 'source_id' => 1,
- 'include_frozen' => true
- ]
- );
- $this->assertTrue($result['success']);
- // 验证所有物品都被消耗
- $remainingQuantity = ItemUser::where('user_id', $this->testUserId)
- ->where('item_id', $this->testItemId)
- ->sum('quantity');
- $this->assertEquals(0, $remainingQuantity);
- });
- }
- /**
- * 测试优先消耗冻结物品的逻辑
- */
- public function testConsumeFrozenItemsFirst()
- {
- DB::transaction(function () {
- // 消耗50个物品,应该优先消耗冻结的50个物品
- $result = ItemService::consumeItem(
- $this->testUserId,
- $this->testItemId,
- null,
- 50,
- [
- 'source_type' => 'test',
- 'source_id' => 1,
- 'include_frozen' => true
- ]
- );
- $this->assertTrue($result['success']);
- $this->assertEquals($this->testItemId, $result['item_id']);
- $this->assertEquals(50, $result['quantity']);
- // 验证剩余物品:应该还有100个未冻结物品,0个冻结物品
- $remainingUnfrozen = ItemUser::where('user_id', $this->testUserId)
- ->where('item_id', $this->testItemId)
- ->where('is_frozen', false)
- ->sum('quantity');
- $remainingFrozen = ItemUser::where('user_id', $this->testUserId)
- ->where('item_id', $this->testItemId)
- ->where('is_frozen', true)
- ->sum('quantity');
- $this->assertEquals(100, $remainingUnfrozen, '应该还有100个未冻结物品');
- $this->assertEquals(0, $remainingFrozen, '冻结物品应该被优先消耗完');
- });
- }
- }
|