VegetealHandlerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Tests\Unit\Pet;
  3. use App\Module\AppGame\Handler\Pet\VegetealHandler;
  4. use App\Module\Pet\Services\PetStealService;
  5. use App\Module\Pet\Dtos\StealResultDto;
  6. use Illuminate\Support\Facades\Log;
  7. use PHPUnit\Framework\TestCase;
  8. use Uraus\Kku\Request\RequestPetVegeteal;
  9. use Uraus\Kku\Response;
  10. use Mockery;
  11. /**
  12. * VegetealHandler单元测试
  13. */
  14. class VegetealHandlerTest extends TestCase
  15. {
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. }
  20. protected function tearDown(): void
  21. {
  22. Mockery::close();
  23. parent::tearDown();
  24. }
  25. /**
  26. * 测试偷菜成功的情况
  27. */
  28. public function test_steal_crop_success()
  29. {
  30. // 准备测试数据
  31. $targetUserId = 39170;
  32. $petId = 36;
  33. $plantId = 562;
  34. $stealerId = 1;
  35. // 创建请求对象
  36. $request = new RequestPetVegeteal();
  37. $request->setUserId($targetUserId);
  38. $request->setPetId($petId);
  39. $request->setPlantId($plantId);
  40. // 模拟偷菜成功的结果
  41. $stealResult = StealResultDto::success(
  42. stealAmount: 5,
  43. itemId: 1,
  44. pickLogId: 123,
  45. stealLogId: 456,
  46. stealerPetId: 36,
  47. targetPetId: 37
  48. );
  49. // Mock PetStealService
  50. $mockService = Mockery::mock('alias:' . PetStealService::class);
  51. $mockService->shouldReceive('stealCrop')
  52. ->once()
  53. ->with($stealerId, $targetUserId, $plantId)
  54. ->andReturn($stealResult);
  55. // Mock Log facade
  56. $mockLog = Mockery::mock('alias:' . Log::class);
  57. $mockLog->shouldReceive('info')->times(2);
  58. // 创建Handler实例
  59. $response = new Response();
  60. $handler = new VegetealHandler($response);
  61. $handler->user_id = $stealerId;
  62. // 执行测试
  63. $result = $handler->handle($request);
  64. // 验证结果
  65. $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result);
  66. }
  67. /**
  68. * 测试偷菜被防御的情况
  69. */
  70. public function test_steal_crop_defended()
  71. {
  72. // 准备测试数据
  73. $targetUserId = 39170;
  74. $petId = 36;
  75. $plantId = 562;
  76. $stealerId = 1;
  77. // 创建请求对象
  78. $request = new RequestPetVegeteal();
  79. $request->setUserId($targetUserId);
  80. $request->setPetId($petId);
  81. $request->setPlantId($plantId);
  82. // 模拟偷菜被防御的结果
  83. $stealResult = StealResultDto::defended(
  84. stealLogId: 456,
  85. stealerPetId: 36,
  86. targetPetId: 37
  87. );
  88. // Mock PetStealService
  89. $this->mock(PetStealService::class, function ($mock) use ($stealResult, $stealerId, $targetUserId, $plantId) {
  90. $mock->shouldReceive('stealCrop')
  91. ->once()
  92. ->with($stealerId, $targetUserId, $plantId)
  93. ->andReturn($stealResult);
  94. });
  95. // Mock Log facade
  96. Log::shouldReceive('info')->times(2);
  97. // 创建Handler实例
  98. $response = new Response();
  99. $handler = new VegetealHandler($response);
  100. $handler->user_id = $stealerId;
  101. // 执行测试
  102. $result = $handler->handle($request);
  103. // 验证结果
  104. $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result);
  105. }
  106. /**
  107. * 测试偷菜失败的情况
  108. */
  109. public function test_steal_crop_failed()
  110. {
  111. // 准备测试数据
  112. $targetUserId = 39170;
  113. $petId = 36;
  114. $plantId = 562;
  115. $stealerId = 1;
  116. // 创建请求对象
  117. $request = new RequestPetVegeteal();
  118. $request->setUserId($targetUserId);
  119. $request->setPetId($petId);
  120. $request->setPlantId($plantId);
  121. // 模拟偷菜失败的结果
  122. $stealResult = StealResultDto::failed(
  123. reason: '没有可偷的成熟作物',
  124. stealLogId: 456,
  125. stealerPetId: 36,
  126. targetPetId: null
  127. );
  128. // Mock PetStealService
  129. $this->mock(PetStealService::class, function ($mock) use ($stealResult, $stealerId, $targetUserId, $plantId) {
  130. $mock->shouldReceive('stealCrop')
  131. ->once()
  132. ->with($stealerId, $targetUserId, $plantId)
  133. ->andReturn($stealResult);
  134. });
  135. // Mock Log facade
  136. Log::shouldReceive('info')->once();
  137. Log::shouldReceive('warning')->once();
  138. // 创建Handler实例
  139. $response = new Response();
  140. $handler = new VegetealHandler($response);
  141. $handler->user_id = $stealerId;
  142. // 执行测试
  143. $result = $handler->handle($request);
  144. // 验证结果
  145. $this->assertInstanceOf(\Uraus\Kku\Response\ResponsePetVegeteal::class, $result);
  146. }
  147. /**
  148. * 测试业务逻辑异常的情况
  149. */
  150. public function test_steal_crop_logic_exception()
  151. {
  152. // 准备测试数据
  153. $targetUserId = 39170;
  154. $petId = 36;
  155. $plantId = 562;
  156. $stealerId = 1;
  157. // 创建请求对象
  158. $request = new RequestPetVegeteal();
  159. $request->setUserId($targetUserId);
  160. $request->setPetId($petId);
  161. $request->setPlantId($plantId);
  162. // Mock PetStealService抛出业务逻辑异常
  163. $this->mock(PetStealService::class, function ($mock) use ($stealerId, $targetUserId, $plantId) {
  164. $mock->shouldReceive('stealCrop')
  165. ->once()
  166. ->with($stealerId, $targetUserId, $plantId)
  167. ->andThrow(new \UCore\Exception\LogicException('不能偷自己的菜'));
  168. });
  169. // Mock Log facade
  170. Log::shouldReceive('info')->once();
  171. Log::shouldReceive('warning')->once();
  172. // 创建Handler实例
  173. $response = new Response();
  174. $handler = new VegetealHandler($response);
  175. $handler->user_id = $stealerId;
  176. // 期望抛出异常
  177. $this->expectException(\UCore\Exception\LogicException::class);
  178. $this->expectExceptionMessage('不能偷自己的菜');
  179. // 执行测试
  180. $handler->handle($request);
  181. }
  182. /**
  183. * 测试一般异常的情况
  184. */
  185. public function test_steal_crop_general_exception()
  186. {
  187. // 准备测试数据
  188. $targetUserId = 39170;
  189. $petId = 36;
  190. $plantId = 562;
  191. $stealerId = 1;
  192. // 创建请求对象
  193. $request = new RequestPetVegeteal();
  194. $request->setUserId($targetUserId);
  195. $request->setPetId($petId);
  196. $request->setPlantId($plantId);
  197. // Mock PetStealService抛出一般异常
  198. $this->mock(PetStealService::class, function ($mock) use ($stealerId, $targetUserId, $plantId) {
  199. $mock->shouldReceive('stealCrop')
  200. ->once()
  201. ->with($stealerId, $targetUserId, $plantId)
  202. ->andThrow(new \Exception('数据库连接失败'));
  203. });
  204. // Mock Log facade
  205. Log::shouldReceive('info')->once();
  206. Log::shouldReceive('error')->once();
  207. // 创建Handler实例
  208. $response = new Response();
  209. $handler = new VegetealHandler($response);
  210. $handler->user_id = $stealerId;
  211. // 期望抛出异常
  212. $this->expectException(\Exception::class);
  213. $this->expectExceptionMessage('数据库连接失败');
  214. // 执行测试
  215. $handler->handle($request);
  216. }
  217. }