GodActivationTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Module\Farm\Enums\BUFF_TYPE;
  4. use App\Module\Farm\Services\BuffService;
  5. use App\Module\Farm\Validations\GodActivationValidation;
  6. use App\Module\GameItems\Services\ItemService;
  7. use Tests\TestCase;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. /**
  10. * 神像激活功能测试
  11. */
  12. class GodActivationTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. /**
  16. * 测试神像时间属性
  17. */
  18. public function testGodDurationAttribute()
  19. {
  20. // 创建一个测试物品ID
  21. $itemId = 3001; // 假设这是丰收之神物品
  22. // 测试获取神像时间属性
  23. $godDuration = ItemService::getItemNumericAttribute($itemId, 'god_duration_seconds');
  24. // 如果物品不存在,应该返回默认值0
  25. $this->assertIsInt($godDuration);
  26. $this->assertGreaterThanOrEqual(0, $godDuration);
  27. }
  28. /**
  29. * 测试神像激活验证
  30. */
  31. public function testGodActivationValidation()
  32. {
  33. $userId = 1;
  34. $godId = BUFF_TYPE::HARVEST_GOD->value;
  35. $itemId = 3001;
  36. // 创建验证对象
  37. $validation = new GodActivationValidation([
  38. 'user_id' => $userId,
  39. 'god_id' => $godId,
  40. 'item_id' => $itemId
  41. ]);
  42. // 验证规则是否正确设置
  43. $rules = $validation->rules();
  44. $this->assertIsArray($rules);
  45. $this->assertNotEmpty($rules);
  46. }
  47. /**
  48. * 测试神像类型枚举
  49. */
  50. public function testBuffTypeEnum()
  51. {
  52. // 测试所有神像类型
  53. $this->assertEquals(1, BUFF_TYPE::HARVEST_GOD->value);
  54. $this->assertEquals(2, BUFF_TYPE::RAIN_GOD->value);
  55. $this->assertEquals(3, BUFF_TYPE::WEED_KILLER_GOD->value);
  56. $this->assertEquals(4, BUFF_TYPE::PEST_CLEANER_GOD->value);
  57. // 测试名称获取
  58. $this->assertEquals('丰收之神', BUFF_TYPE::getName(1));
  59. $this->assertEquals('雨露之神', BUFF_TYPE::getName(2));
  60. $this->assertEquals('屠草之神', BUFF_TYPE::getName(3));
  61. $this->assertEquals('拭虫之神', BUFF_TYPE::getName(4));
  62. // 测试效果描述
  63. $this->assertEquals('确保收获时获得最高产量', BUFF_TYPE::getDescription(1));
  64. $this->assertEquals('防止干旱灾害', BUFF_TYPE::getDescription(2));
  65. $this->assertEquals('防止杂草灾害', BUFF_TYPE::getDescription(3));
  66. $this->assertEquals('防止虫害灾害', BUFF_TYPE::getDescription(4));
  67. }
  68. /**
  69. * 测试神像物品ID映射
  70. */
  71. public function testGodItemIdMapping()
  72. {
  73. // 测试神像物品ID映射关系
  74. $godTypes = [
  75. BUFF_TYPE::HARVEST_GOD->value => 3001,
  76. BUFF_TYPE::RAIN_GOD->value => 3002,
  77. BUFF_TYPE::WEED_KILLER_GOD->value => 3003,
  78. BUFF_TYPE::PEST_CLEANER_GOD->value => 3004,
  79. ];
  80. foreach ($godTypes as $godId => $expectedItemId) {
  81. $calculatedItemId = 3000 + $godId;
  82. $this->assertEquals($expectedItemId, $calculatedItemId, "神像ID {$godId} 对应的物品ID应该是 {$expectedItemId}");
  83. }
  84. }
  85. /**
  86. * 测试时间转换逻辑
  87. */
  88. public function testTimeConversion()
  89. {
  90. // 测试秒转小时的逻辑
  91. $testCases = [
  92. 3600 => 1, // 1小时
  93. 7200 => 2, // 2小时
  94. 86400 => 24, // 24小时
  95. 90000 => 25, // 25小时(向上取整)
  96. 3599 => 1, // 不足1小时,向上取整为1小时
  97. 0 => 24, // 0秒,使用默认24小时
  98. ];
  99. foreach ($testCases as $seconds => $expectedHours) {
  100. $actualHours = $seconds > 0 ? ceil($seconds / 3600) : 24;
  101. $this->assertEquals($expectedHours, $actualHours, "秒数 {$seconds} 应该转换为 {$expectedHours} 小时");
  102. }
  103. }
  104. /**
  105. * 测试数值属性Cast类
  106. */
  107. public function testNumericAttributesCast()
  108. {
  109. // 创建NumericAttributesCast实例
  110. $cast = new \App\Module\GameItems\Casts\NumericAttributesCast();
  111. // 验证新增的神像时间属性
  112. $this->assertObjectHasProperty('god_duration_seconds', $cast);
  113. $this->assertEquals(0, $cast->god_duration_seconds);
  114. }
  115. /**
  116. * 测试验证器错误消息
  117. */
  118. public function testValidatorErrorMessages()
  119. {
  120. // 测试无效的神像类型
  121. $invalidGodIds = [0, -1, 5, 999];
  122. foreach ($invalidGodIds as $invalidGodId) {
  123. $validation = new GodActivationValidation([
  124. 'user_id' => 1,
  125. 'god_id' => $invalidGodId,
  126. 'item_id' => 3001
  127. ]);
  128. // 验证应该失败
  129. try {
  130. $validation->validated();
  131. $this->fail("神像ID {$invalidGodId} 应该验证失败");
  132. } catch (\Exception $e) {
  133. $this->assertStringContainsString('验证失败', $e->getMessage());
  134. }
  135. }
  136. }
  137. /**
  138. * 测试物品查找逻辑
  139. */
  140. public function testFindGodItemLogic()
  141. {
  142. // 这个测试需要实际的数据库数据,这里只测试逻辑结构
  143. $this->assertTrue(method_exists(\App\Module\GameItems\Services\ItemService::class, 'getUserItems'));
  144. $this->assertTrue(method_exists(\App\Module\GameItems\Services\ItemService::class, 'getItemNumericAttribute'));
  145. }
  146. }