info('开始测试奖励扣除收集器功能...'); // 测试手动添加数据 $this->testManualCollection(); // 测试事件触发自动收集 $this->testEventCollection(); // 测试数据查询 $this->testDataQuery(); // 测试数据清理 $this->testDataClear(); $this->info('奖励扣除收集器功能测试完成!'); return 0; } /** * 测试手动收集功能 */ protected function testManualCollection(): void { $this->info('测试手动收集功能...'); // 手动添加奖励数据 RewardCollectorService::addItemReward(1001, 0, 10); RewardCollectorService::addCoinReward(1, 100); // 手动添加扣除数据 DeductCollectorService::addItemDeduct(2001, 0, 5); DeductCollectorService::addCoinDeduct(2, 50); $this->line('✓ 手动添加奖励和扣除数据完成'); } /** * 测试事件触发自动收集 */ protected function testEventCollection(): void { $this->info('测试事件触发自动收集...'); // 触发物品数量增加事件(奖励) $itemRewardEvent = new ItemQuantityChanged( userId: 1, itemId: 1002, instanceId: 0, oldQuantity: 0, newQuantity: 20, userItemId: 1, options: [] ); Event::dispatch($itemRewardEvent); // 触发物品数量减少事件(扣除) $itemDeductEvent = new ItemQuantityChanged( userId: 1, itemId: 2002, instanceId: 0, oldQuantity: 30, newQuantity: 25, userItemId: 2, options: [] ); Event::dispatch($itemDeductEvent); // 触发资金增加事件(奖励) $fundRewardEvent = new FundChangedEvent( userId: 1, fundId: 1, amount: 200, beforeBalance: 1000, afterBalance: 1200, operateType: 1, operateId: 1, remark: '测试奖励' ); Event::dispatch($fundRewardEvent); // 触发资金减少事件(扣除) $fundDeductEvent = new FundChangedEvent( userId: 1, fundId: 2, amount: -150, beforeBalance: 500, afterBalance: 350, operateType: 2, operateId: 2, remark: '测试扣除' ); Event::dispatch($fundDeductEvent); $this->line('✓ 事件触发自动收集完成'); } /** * 测试数据查询功能 */ protected function testDataQuery(): void { $this->info('测试数据查询功能...'); // 查询奖励数据 if (RewardCollectorService::hasRewards()) { $rewards = RewardCollectorService::getRewards(); $this->line('奖励数据:'); $this->line(' 物品奖励数量:' . count($rewards['items'])); $this->line(' 代币奖励数量:' . count($rewards['coins'])); foreach ($rewards['items'] as $item) { $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}"); } foreach ($rewards['coins'] as $coin) { $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}"); } } else { $this->line('没有奖励数据'); } // 查询扣除数据 if (DeductCollectorService::hasDeducts()) { $deducts = DeductCollectorService::getDeducts(); $this->line('扣除数据:'); $this->line(' 物品扣除数量:' . count($deducts['items'])); $this->line(' 代币扣除数量:' . count($deducts['coins'])); foreach ($deducts['items'] as $item) { $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}"); } foreach ($deducts['coins'] as $coin) { $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}"); } } else { $this->line('没有扣除数据'); } $this->line('✓ 数据查询完成'); } /** * 测试数据清理功能 */ protected function testDataClear(): void { $this->info('测试数据清理功能...'); // 清理数据 RewardCollectorService::clearRewards(); DeductCollectorService::clearDeducts(); // 验证清理结果 $hasRewards = RewardCollectorService::hasRewards(); $hasDeducts = DeductCollectorService::hasDeducts(); if (!$hasRewards && !$hasDeducts) { $this->line('✓ 数据清理成功'); } else { $this->error('✗ 数据清理失败'); } } }