NumericAttributesCastTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Tests\Unit\GameItems;
  3. use App\Module\GameItems\Casts\NumericAttributesCast;
  4. use Tests\TestCase;
  5. /**
  6. * 物品数值属性Cast类测试
  7. */
  8. class NumericAttributesCastTest extends TestCase
  9. {
  10. /**
  11. * 测试神像种类属性
  12. */
  13. public function testGodTypeAttribute()
  14. {
  15. $cast = new NumericAttributesCast();
  16. // 测试默认值
  17. $this->assertEquals(0, $cast->god_type);
  18. // 测试设置值
  19. $cast->god_type = 1; // 丰收之神
  20. $this->assertEquals(1, $cast->god_type);
  21. $cast->god_type = 2; // 雨露之神
  22. $this->assertEquals(2, $cast->god_type);
  23. $cast->god_type = 3; // 屠草之神
  24. $this->assertEquals(3, $cast->god_type);
  25. $cast->god_type = 4; // 拭虫之神
  26. $this->assertEquals(4, $cast->god_type);
  27. }
  28. /**
  29. * 测试神像时间属性
  30. */
  31. public function testGodDurationAttribute()
  32. {
  33. $cast = new NumericAttributesCast();
  34. // 测试默认值
  35. $this->assertEquals(0, $cast->god_duration_seconds);
  36. // 测试设置值
  37. $cast->god_duration_seconds = 3600; // 1小时
  38. $this->assertEquals(3600, $cast->god_duration_seconds);
  39. $cast->god_duration_seconds = 86400; // 24小时
  40. $this->assertEquals(86400, $cast->god_duration_seconds);
  41. }
  42. /**
  43. * 测试神像物品识别逻辑
  44. */
  45. public function testGodItemIdentification()
  46. {
  47. $cast = new NumericAttributesCast();
  48. // 非神像物品
  49. $cast->god_duration_seconds = 0;
  50. $cast->god_type = 0;
  51. $this->assertFalse($this->isGodItem($cast));
  52. // 只有时间没有类型
  53. $cast->god_duration_seconds = 3600;
  54. $cast->god_type = 0;
  55. $this->assertFalse($this->isGodItem($cast));
  56. // 只有类型没有时间
  57. $cast->god_duration_seconds = 0;
  58. $cast->god_type = 1;
  59. $this->assertFalse($this->isGodItem($cast));
  60. // 完整的神像物品
  61. $cast->god_duration_seconds = 3600;
  62. $cast->god_type = 1;
  63. $this->assertTrue($this->isGodItem($cast));
  64. }
  65. /**
  66. * 测试JSON序列化和反序列化
  67. */
  68. public function testJsonSerialization()
  69. {
  70. $cast = new NumericAttributesCast();
  71. $cast->god_duration_seconds = 86400;
  72. $cast->god_type = 2;
  73. $cast->pet_power = 50;
  74. // 转换为数组
  75. $array = $cast->toArray();
  76. $this->assertEquals(86400, $array['god_duration_seconds']);
  77. $this->assertEquals(2, $array['god_type']);
  78. $this->assertEquals(50, $array['pet_power']);
  79. // 从数组创建新实例
  80. $newCast = new NumericAttributesCast();
  81. $newCast->fromArray($array);
  82. $this->assertEquals(86400, $newCast->god_duration_seconds);
  83. $this->assertEquals(2, $newCast->god_type);
  84. $this->assertEquals(50, $newCast->pet_power);
  85. }
  86. /**
  87. * 辅助方法:判断是否为神像物品
  88. */
  89. private function isGodItem(NumericAttributesCast $cast): bool
  90. {
  91. return $cast->god_duration_seconds > 0 && $cast->god_type > 0;
  92. }
  93. }