setUserId($targetUserId); $request->setPetId($petId); $request->setPlantId($plantId); // 模拟偷菜成功的结果 $stealResult = StealResultDto::success( stealAmount: 5, itemId: 1, pickLogId: 123, stealLogId: 456, stealerPetId: 36, targetPetId: 37 ); // Mock PetStealService $mockService = Mockery::mock('alias:' . PetStealService::class); $mockService->shouldReceive('stealCrop') ->once() ->with($stealerId, $targetUserId, $plantId) ->andReturn($stealResult); // Mock Log facade $mockLog = Mockery::mock('alias:' . Log::class); $mockLog->shouldReceive('info')->times(2); // 创建Handler实例 $response = new Response(); $handler = new VegetealHandler($response); $handler->user_id = $stealerId; // 执行测试 $result = $handler->handle($request); // 验证结果 $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result); } /** * 测试偷菜被防御的情况 */ public function test_steal_crop_defended() { // 准备测试数据 $targetUserId = 39170; $petId = 36; $plantId = 562; $stealerId = 1; // 创建请求对象 $request = new RequestPetVegeteal(); $request->setUserId($targetUserId); $request->setPetId($petId); $request->setPlantId($plantId); // 模拟偷菜被防御的结果 $stealResult = StealResultDto::defended( stealLogId: 456, stealerPetId: 36, targetPetId: 37 ); // Mock PetStealService $this->mock(PetStealService::class, function ($mock) use ($stealResult, $stealerId, $targetUserId, $plantId) { $mock->shouldReceive('stealCrop') ->once() ->with($stealerId, $targetUserId, $plantId) ->andReturn($stealResult); }); // Mock Log facade Log::shouldReceive('info')->times(2); // 创建Handler实例 $response = new Response(); $handler = new VegetealHandler($response); $handler->user_id = $stealerId; // 执行测试 $result = $handler->handle($request); // 验证结果 $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result); } /** * 测试偷菜失败的情况 */ public function test_steal_crop_failed() { // 准备测试数据 $targetUserId = 39170; $petId = 36; $plantId = 562; $stealerId = 1; // 创建请求对象 $request = new RequestPetVegeteal(); $request->setUserId($targetUserId); $request->setPetId($petId); $request->setPlantId($plantId); // 模拟偷菜失败的结果 $stealResult = StealResultDto::failed( reason: '没有可偷的成熟作物', stealLogId: 456, stealerPetId: 36, targetPetId: null ); // Mock PetStealService $this->mock(PetStealService::class, function ($mock) use ($stealResult, $stealerId, $targetUserId, $plantId) { $mock->shouldReceive('stealCrop') ->once() ->with($stealerId, $targetUserId, $plantId) ->andReturn($stealResult); }); // Mock Log facade Log::shouldReceive('info')->once(); Log::shouldReceive('warning')->once(); // 创建Handler实例 $response = new Response(); $handler = new VegetealHandler($response); $handler->user_id = $stealerId; // 执行测试 $result = $handler->handle($request); // 验证结果 $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result); } /** * 测试业务逻辑异常的情况 */ public function test_steal_crop_logic_exception() { // 准备测试数据 $targetUserId = 39170; $petId = 36; $plantId = 562; $stealerId = 1; // 创建请求对象 $request = new RequestPetVegeteal(); $request->setUserId($targetUserId); $request->setPetId($petId); $request->setPlantId($plantId); // Mock PetStealService抛出业务逻辑异常 $this->mock(PetStealService::class, function ($mock) use ($stealerId, $targetUserId, $plantId) { $mock->shouldReceive('stealCrop') ->once() ->with($stealerId, $targetUserId, $plantId) ->andThrow(new \UCore\Exception\LogicException('不能偷自己的菜')); }); // Mock Log facade Log::shouldReceive('info')->once(); Log::shouldReceive('warning')->once(); // 创建Handler实例 $response = new Response(); $handler = new VegetealHandler($response); $handler->user_id = $stealerId; // 期望抛出异常 $this->expectException(\UCore\Exception\LogicException::class); $this->expectExceptionMessage('不能偷自己的菜'); // 执行测试 $handler->handle($request); } /** * 测试一般异常的情况 */ public function test_steal_crop_general_exception() { // 准备测试数据 $targetUserId = 39170; $petId = 36; $plantId = 562; $stealerId = 1; // 创建请求对象 $request = new RequestPetVegeteal(); $request->setUserId($targetUserId); $request->setPetId($petId); $request->setPlantId($plantId); // Mock PetStealService抛出一般异常 $this->mock(PetStealService::class, function ($mock) use ($stealerId, $targetUserId, $plantId) { $mock->shouldReceive('stealCrop') ->once() ->with($stealerId, $targetUserId, $plantId) ->andThrow(new \Exception('数据库连接失败')); }); // Mock Log facade Log::shouldReceive('info')->once(); Log::shouldReceive('error')->once(); // 创建Handler实例 $response = new Response(); $handler = new VegetealHandler($response); $handler->user_id = $stealerId; // 期望抛出异常 $this->expectException(\Exception::class); $this->expectExceptionMessage('数据库连接失败'); // 执行测试 $handler->handle($request); } }