| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace Tests\Unit\Pet;
- use App\Module\AppGame\Handler\Pet\VegetealHandler;
- use App\Module\Pet\Services\PetStealService;
- use App\Module\Pet\Dtos\StealResultDto;
- use Illuminate\Support\Facades\Log;
- use PHPUnit\Framework\TestCase;
- use Uraus\Kku\Request\RequestPetVegeteal;
- use Uraus\Kku\Response;
- use Mockery;
- /**
- * VegetealHandler单元测试
- */
- class VegetealHandlerTest extends TestCase
- {
- protected function setUp(): void
- {
- parent::setUp();
- }
- protected function tearDown(): void
- {
- Mockery::close();
- parent::tearDown();
- }
- /**
- * 测试偷菜成功的情况
- */
- public function test_steal_crop_success()
- {
- // 准备测试数据
- $targetUserId = 39170;
- $petId = 36;
- $plantId = 562;
- $stealerId = 1;
- // 创建请求对象
- $request = new RequestPetVegeteal();
- $request->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);
- }
- }
|