SignInRewardLogicTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Http\logic\admin\SignInRewardLogic;
  4. use App\Models\SignInRewardConfig;
  5. use Tests\TestCase;
  6. /**
  7. * 签到奖励配置业务逻辑单元测试
  8. *
  9. * 测试 SignInRewardLogic 的 CRUD 操作
  10. */
  11. class SignInRewardLogicTest extends TestCase
  12. {
  13. /**
  14. * @var SignInRewardLogic
  15. */
  16. protected $logic;
  17. /**
  18. * 设置测试环境
  19. */
  20. public function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->logic = new SignInRewardLogic();
  24. // 清理测试数据
  25. SignInRewardConfig::query()->delete();
  26. }
  27. /**
  28. * 测试创建有效的签到奖励配置
  29. *
  30. * @return void
  31. */
  32. public function test_create_valid_reward_config()
  33. {
  34. $data = [
  35. 'DayNumber' => 1,
  36. 'RewardScore' => 100,
  37. ];
  38. $result = $this->logic->create($data);
  39. $this->assertTrue($result);
  40. $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', $data);
  41. }
  42. /**
  43. * 测试创建配置时缺少必要字段
  44. *
  45. * @return void
  46. */
  47. public function test_create_missing_day_number()
  48. {
  49. $data = [
  50. 'RewardScore' => 100,
  51. ];
  52. $result = $this->logic->create($data);
  53. $this->assertFalse($result);
  54. $this->assertStringContainsString('签到天数', $this->logic->getError());
  55. }
  56. /**
  57. * 测试创建配置时奖励积分缺失
  58. *
  59. * @return void
  60. */
  61. public function test_create_missing_reward_score()
  62. {
  63. $data = [
  64. 'DayNumber' => 1,
  65. ];
  66. $result = $this->logic->create($data);
  67. $this->assertFalse($result);
  68. $this->assertStringContainsString('奖励积分', $this->logic->getError());
  69. }
  70. /**
  71. * 测试创建配置时使用无效的天数(非正整数)
  72. *
  73. * @return void
  74. */
  75. public function test_create_invalid_day_number()
  76. {
  77. $data = [
  78. 'DayNumber' => -1,
  79. 'RewardScore' => 100,
  80. ];
  81. $result = $this->logic->create($data);
  82. $this->assertFalse($result);
  83. $this->assertStringContainsString('正整数', $this->logic->getError());
  84. }
  85. /**
  86. * 测试创建配置时使用负数的奖励积分
  87. *
  88. * @return void
  89. */
  90. public function test_create_negative_reward_score()
  91. {
  92. $data = [
  93. 'DayNumber' => 1,
  94. 'RewardScore' => -100,
  95. ];
  96. $result = $this->logic->create($data);
  97. $this->assertFalse($result);
  98. $this->assertStringContainsString('非负整数', $this->logic->getError());
  99. }
  100. /**
  101. * 测试创建重复的天数配置
  102. *
  103. * @return void
  104. */
  105. public function test_create_duplicate_day_number()
  106. {
  107. // 创建第一条记录
  108. $data1 = [
  109. 'DayNumber' => 1,
  110. 'RewardScore' => 100,
  111. ];
  112. $this->logic->create($data1);
  113. // 尝试创建相同的天数
  114. $result = $this->logic->create($data1);
  115. $this->assertFalse($result);
  116. $this->assertStringContainsString('已存在', $this->logic->getError());
  117. }
  118. /**
  119. * 测试获取列表
  120. *
  121. * @return void
  122. */
  123. public function test_get_list()
  124. {
  125. // 创建多条记录
  126. for ($i = 1; $i <= 3; $i++) {
  127. $data = [
  128. 'DayNumber' => $i,
  129. 'RewardScore' => $i * 100,
  130. ];
  131. $this->logic->create($data);
  132. }
  133. $list = $this->logic->getList(10);
  134. $this->assertCount(3, $list);
  135. }
  136. /**
  137. * 测试按天数查找奖励配置
  138. *
  139. * @return void
  140. */
  141. public function test_find_by_day()
  142. {
  143. $data = [
  144. 'DayNumber' => 5,
  145. 'RewardScore' => 500,
  146. ];
  147. $this->logic->create($data);
  148. $config = $this->logic->getByDay(5);
  149. $this->assertNotNull($config);
  150. $this->assertEquals(5, $config->DayNumber);
  151. $this->assertEquals(500, $config->RewardScore);
  152. }
  153. /**
  154. * 测试查找不存在的天数
  155. *
  156. * @return void
  157. */
  158. public function test_find_by_day_not_found()
  159. {
  160. $config = $this->logic->getByDay(999);
  161. $this->assertNull($config);
  162. }
  163. /**
  164. * 测试更新奖励配置
  165. *
  166. * @return void
  167. */
  168. public function test_update_reward_config()
  169. {
  170. // 创建初始记录
  171. $data = [
  172. 'DayNumber' => 2,
  173. 'RewardScore' => 200,
  174. ];
  175. $this->logic->create($data);
  176. // 更新记录
  177. $updateData = [
  178. 'RewardScore' => 300,
  179. ];
  180. $result = $this->logic->update(2, $updateData);
  181. $this->assertTrue($result);
  182. $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', [
  183. 'DayNumber' => 2,
  184. 'RewardScore' => 300,
  185. ]);
  186. }
  187. /**
  188. * 测试更新时使用无效的奖励积分
  189. *
  190. * @return void
  191. */
  192. public function test_update_invalid_reward_score()
  193. {
  194. // 创建初始记录
  195. $data = [
  196. 'DayNumber' => 2,
  197. 'RewardScore' => 200,
  198. ];
  199. $this->logic->create($data);
  200. // 尝试使用无效的奖励积分更新
  201. $updateData = [
  202. 'RewardScore' => 'invalid',
  203. ];
  204. $result = $this->logic->update(2, $updateData);
  205. $this->assertFalse($result);
  206. }
  207. /**
  208. * 测试更新不存在的记录
  209. *
  210. * @return void
  211. */
  212. public function test_update_nonexistent_record()
  213. {
  214. $updateData = [
  215. 'RewardScore' => 300,
  216. ];
  217. $this->expectException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);
  218. $this->logic->update(999, $updateData);
  219. }
  220. /**
  221. * 测试删除奖励配置
  222. *
  223. * @return void
  224. */
  225. public function test_delete_reward_config()
  226. {
  227. // 创建初始记录
  228. $data = [
  229. 'DayNumber' => 3,
  230. 'RewardScore' => 300,
  231. ];
  232. $this->logic->create($data);
  233. // 删除记录
  234. $result = $this->logic->delete(3);
  235. $this->assertTrue($result);
  236. $this->assertDatabaseMissing('QPAccountsDB.dbo.SignInRewardConfig', [
  237. 'DayNumber' => 3,
  238. ]);
  239. }
  240. /**
  241. * 测试删除不存在的记录
  242. *
  243. * @return void
  244. */
  245. public function test_delete_nonexistent_record()
  246. {
  247. $this->expectException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);
  248. $this->logic->delete(999);
  249. }
  250. /**
  251. * 清理测试数据
  252. */
  253. public function tearDown(): void
  254. {
  255. SignInRewardConfig::query()->delete();
  256. parent::tearDown();
  257. }
  258. }