| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace Tests\Feature;
- use App\Models\SignInRewardConfig;
- use Tests\TestCase;
- /**
- * 签到奖励配置控制器功能测试
- *
- * 测试 SignInRewardController 的 HTTP 端点
- */
- class SignInRewardControllerTest extends TestCase
- {
- /**
- * 设置测试环境
- */
- public function setUp(): void
- {
- parent::setUp();
- // 清理测试数据
- SignInRewardConfig::query()->delete();
- }
- /**
- * 测试获取列表页面
- *
- * @return void
- */
- public function test_list_view_returns_success()
- {
- // 创建测试数据
- SignInRewardConfig::create([
- 'DayNumber' => 1,
- 'RewardScore' => 100,
- ]);
- $response = $this->get('/admin/sign-in-reward/list');
- $response->assertStatus(200);
- $response->assertViewHas('list');
- }
- /**
- * 测试获取添加页面
- *
- * @return void
- */
- public function test_add_view_returns_success()
- {
- $response = $this->get('/admin/sign-in-reward/add');
- $response->assertStatus(200);
- }
- /**
- * 测试成功添加奖励配置
- *
- * @return void
- */
- public function test_add_valid_config_returns_success()
- {
- $data = [
- 'DayNumber' => 1,
- 'RewardScore' => 100,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 200, 'msg' => '添加成功']);
- $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', $data);
- }
- /**
- * 测试添加时缺少必要字段
- *
- * @return void
- */
- public function test_add_missing_day_number_returns_error()
- {
- $data = [
- 'RewardScore' => 100,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- $response->assertJsonFragment(['msg' => '签到天数不能为空']);
- }
- /**
- * 测试添加时缺少奖励积分
- *
- * @return void
- */
- public function test_add_missing_reward_score_returns_error()
- {
- $data = [
- 'DayNumber' => 1,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- $response->assertJsonFragment(['msg' => '奖励积分不能为空']);
- }
- /**
- * 测试添加时使用无效天数
- *
- * @return void
- */
- public function test_add_invalid_day_number_returns_error()
- {
- $data = [
- 'DayNumber' => -1,
- 'RewardScore' => 100,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- }
- /**
- * 测试添加时使用负数奖励积分
- *
- * @return void
- */
- public function test_add_negative_reward_score_returns_error()
- {
- $data = [
- 'DayNumber' => 1,
- 'RewardScore' => -100,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- }
- /**
- * 测试添加重复的天数配置
- *
- * @return void
- */
- public function test_add_duplicate_day_number_returns_error()
- {
- // 创建第一条记录
- SignInRewardConfig::create([
- 'DayNumber' => 1,
- 'RewardScore' => 100,
- ]);
- // 尝试添加相同的天数
- $data = [
- 'DayNumber' => 1,
- 'RewardScore' => 200,
- ];
- $response = $this->post('/admin/sign-in-reward/add', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- $response->assertJsonFragment(['msg' => '已存在']);
- }
- /**
- * 测试获取更新页面
- *
- * @return void
- */
- public function test_update_view_returns_success()
- {
- // 创建测试数据
- SignInRewardConfig::create([
- 'DayNumber' => 1,
- 'RewardScore' => 100,
- ]);
- $response = $this->get('/admin/sign-in-reward/update/1');
- $response->assertStatus(200);
- $response->assertViewHas('config');
- }
- /**
- * 测试更新奖励配置
- *
- * @return void
- */
- public function test_update_valid_config_returns_success()
- {
- // 创建初始记录
- SignInRewardConfig::create([
- 'DayNumber' => 2,
- 'RewardScore' => 200,
- ]);
- $data = [
- 'RewardScore' => 300,
- ];
- $response = $this->post('/admin/sign-in-reward/update/2', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 200, 'msg' => '修改成功']);
- $this->assertDatabaseHas('QPAccountsDB.dbo.SignInRewardConfig', [
- 'DayNumber' => 2,
- 'RewardScore' => 300,
- ]);
- }
- /**
- * 测试更新时使用无效奖励积分
- *
- * @return void
- */
- public function test_update_invalid_reward_score_returns_error()
- {
- // 创建初始记录
- SignInRewardConfig::create([
- 'DayNumber' => 2,
- 'RewardScore' => 200,
- ]);
- $data = [
- 'RewardScore' => -100,
- ];
- $response = $this->post('/admin/sign-in-reward/update/2', $data);
- $response->assertStatus(200);
- $response->assertJson(['code' => 400]);
- }
- /**
- * 测试更新不存在的记录
- *
- * @return void
- */
- public function test_update_nonexistent_record_returns_error()
- {
- $data = [
- 'RewardScore' => 300,
- ];
- $response = $this->post('/admin/sign-in-reward/update/999', $data);
- $response->assertStatus(404);
- }
- /**
- * 测试删除奖励配置
- *
- * @return void
- */
- public function test_delete_config_returns_success()
- {
- // 创建测试数据
- SignInRewardConfig::create([
- 'DayNumber' => 3,
- 'RewardScore' => 300,
- ]);
- $response = $this->post('/admin/sign-in-reward/delete/3');
- $response->assertStatus(200);
- $response->assertJson(['code' => 200, 'msg' => '删除成功']);
- $this->assertDatabaseMissing('QPAccountsDB.dbo.SignInRewardConfig', [
- 'DayNumber' => 3,
- ]);
- }
- /**
- * 测试删除不存在的记录
- *
- * @return void
- */
- public function test_delete_nonexistent_record_returns_error()
- {
- $response = $this->post('/admin/sign-in-reward/delete/999');
- $response->assertStatus(404);
- }
- /**
- * 清理测试数据
- */
- public function tearDown(): void
- {
- SignInRewardConfig::query()->delete();
- parent::tearDown();
- }
- }
|