SignInRewardControllerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\SignInRewardConfig;
  4. use Tests\TestCase;
  5. /**
  6. * 签到奖励配置控制器功能测试
  7. *
  8. * 测试 SignInRewardController 的 HTTP 端点
  9. */
  10. class SignInRewardControllerTest extends TestCase
  11. {
  12. /**
  13. * 设置测试环境
  14. */
  15. public function setUp(): void
  16. {
  17. parent::setUp();
  18. // 清理测试数据
  19. SignInRewardConfig::query()->delete();
  20. }
  21. /**
  22. * 测试获取列表页面
  23. *
  24. * @return void
  25. */
  26. public function test_list_view_returns_success()
  27. {
  28. // 创建测试数据
  29. SignInRewardConfig::create([
  30. 'DayNumber' => 1,
  31. 'RewardScore' => 100,
  32. ]);
  33. $response = $this->get('/admin/sign-in-reward/list');
  34. $response->assertStatus(200);
  35. $response->assertViewHas('list');
  36. }
  37. /**
  38. * 测试获取添加页面
  39. *
  40. * @return void
  41. */
  42. public function test_add_view_returns_success()
  43. {
  44. $response = $this->get('/admin/sign-in-reward/add');
  45. $response->assertStatus(200);
  46. }
  47. /**
  48. * 测试成功添加奖励配置
  49. *
  50. * @return void
  51. */
  52. public function test_add_valid_config_returns_success()
  53. {
  54. $data = [
  55. 'DayNumber' => 1,
  56. 'RewardScore' => 100,
  57. ];
  58. $response = $this->post('/admin/sign-in-reward/add', $data);
  59. $response->assertStatus(200);
  60. $response->assertJson(['code' => 200, 'msg' => '添加成功']);
  61. $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', $data);
  62. }
  63. /**
  64. * 测试添加时缺少必要字段
  65. *
  66. * @return void
  67. */
  68. public function test_add_missing_day_number_returns_error()
  69. {
  70. $data = [
  71. 'RewardScore' => 100,
  72. ];
  73. $response = $this->post('/admin/sign-in-reward/add', $data);
  74. $response->assertStatus(200);
  75. $response->assertJson(['code' => 400]);
  76. $response->assertJsonFragment(['msg' => '签到天数不能为空']);
  77. }
  78. /**
  79. * 测试添加时缺少奖励积分
  80. *
  81. * @return void
  82. */
  83. public function test_add_missing_reward_score_returns_error()
  84. {
  85. $data = [
  86. 'DayNumber' => 1,
  87. ];
  88. $response = $this->post('/admin/sign-in-reward/add', $data);
  89. $response->assertStatus(200);
  90. $response->assertJson(['code' => 400]);
  91. $response->assertJsonFragment(['msg' => '奖励积分不能为空']);
  92. }
  93. /**
  94. * 测试添加时使用无效天数
  95. *
  96. * @return void
  97. */
  98. public function test_add_invalid_day_number_returns_error()
  99. {
  100. $data = [
  101. 'DayNumber' => -1,
  102. 'RewardScore' => 100,
  103. ];
  104. $response = $this->post('/admin/sign-in-reward/add', $data);
  105. $response->assertStatus(200);
  106. $response->assertJson(['code' => 400]);
  107. }
  108. /**
  109. * 测试添加时使用负数奖励积分
  110. *
  111. * @return void
  112. */
  113. public function test_add_negative_reward_score_returns_error()
  114. {
  115. $data = [
  116. 'DayNumber' => 1,
  117. 'RewardScore' => -100,
  118. ];
  119. $response = $this->post('/admin/sign-in-reward/add', $data);
  120. $response->assertStatus(200);
  121. $response->assertJson(['code' => 400]);
  122. }
  123. /**
  124. * 测试添加重复的天数配置
  125. *
  126. * @return void
  127. */
  128. public function test_add_duplicate_day_number_returns_error()
  129. {
  130. // 创建第一条记录
  131. SignInRewardConfig::create([
  132. 'DayNumber' => 1,
  133. 'RewardScore' => 100,
  134. ]);
  135. // 尝试添加相同的天数
  136. $data = [
  137. 'DayNumber' => 1,
  138. 'RewardScore' => 200,
  139. ];
  140. $response = $this->post('/admin/sign-in-reward/add', $data);
  141. $response->assertStatus(200);
  142. $response->assertJson(['code' => 400]);
  143. $response->assertJsonFragment(['msg' => '已存在']);
  144. }
  145. /**
  146. * 测试获取更新页面
  147. *
  148. * @return void
  149. */
  150. public function test_update_view_returns_success()
  151. {
  152. // 创建测试数据
  153. SignInRewardConfig::create([
  154. 'DayNumber' => 1,
  155. 'RewardScore' => 100,
  156. ]);
  157. $response = $this->get('/admin/sign-in-reward/update/1');
  158. $response->assertStatus(200);
  159. $response->assertViewHas('config');
  160. }
  161. /**
  162. * 测试更新奖励配置
  163. *
  164. * @return void
  165. */
  166. public function test_update_valid_config_returns_success()
  167. {
  168. // 创建初始记录
  169. SignInRewardConfig::create([
  170. 'DayNumber' => 2,
  171. 'RewardScore' => 200,
  172. ]);
  173. $data = [
  174. 'RewardScore' => 300,
  175. ];
  176. $response = $this->post('/admin/sign-in-reward/update/2', $data);
  177. $response->assertStatus(200);
  178. $response->assertJson(['code' => 200, 'msg' => '修改成功']);
  179. $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', [
  180. 'DayNumber' => 2,
  181. 'RewardScore' => 300,
  182. ]);
  183. }
  184. /**
  185. * 测试更新时使用无效奖励积分
  186. *
  187. * @return void
  188. */
  189. public function test_update_invalid_reward_score_returns_error()
  190. {
  191. // 创建初始记录
  192. SignInRewardConfig::create([
  193. 'DayNumber' => 2,
  194. 'RewardScore' => 200,
  195. ]);
  196. $data = [
  197. 'RewardScore' => -100,
  198. ];
  199. $response = $this->post('/admin/sign-in-reward/update/2', $data);
  200. $response->assertStatus(200);
  201. $response->assertJson(['code' => 400]);
  202. }
  203. /**
  204. * 测试更新不存在的记录
  205. *
  206. * @return void
  207. */
  208. public function test_update_nonexistent_record_returns_error()
  209. {
  210. $data = [
  211. 'RewardScore' => 300,
  212. ];
  213. $response = $this->post('/admin/sign-in-reward/update/999', $data);
  214. $response->assertStatus(404);
  215. }
  216. /**
  217. * 测试删除奖励配置
  218. *
  219. * @return void
  220. */
  221. public function test_delete_config_returns_success()
  222. {
  223. // 创建测试数据
  224. SignInRewardConfig::create([
  225. 'DayNumber' => 3,
  226. 'RewardScore' => 300,
  227. ]);
  228. $response = $this->post('/admin/sign-in-reward/delete/3');
  229. $response->assertStatus(200);
  230. $response->assertJson(['code' => 200, 'msg' => '删除成功']);
  231. $this->assertDatabaseMissing('QPAccountsDB.dbo.SignInRewardConfig', [
  232. 'DayNumber' => 3,
  233. ]);
  234. }
  235. /**
  236. * 测试删除不存在的记录
  237. *
  238. * @return void
  239. */
  240. public function test_delete_nonexistent_record_returns_error()
  241. {
  242. $response = $this->post('/admin/sign-in-reward/delete/999');
  243. $response->assertStatus(404);
  244. }
  245. /**
  246. * 清理测试数据
  247. */
  248. public function tearDown(): void
  249. {
  250. SignInRewardConfig::query()->delete();
  251. parent::tearDown();
  252. }
  253. }