2026-04-03 13:55:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 文件功能:节日福利前台领取功能测试
|
|
|
|
|
*
|
|
|
|
|
* 覆盖按批次查询状态、领取成功、名单缺失与过期拒绝等核心路径,
|
|
|
|
|
* 确保 run_id 改造后前台接口仍能正确工作。
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-03 13:55:36 +08:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use App\Models\HolidayClaim;
|
|
|
|
|
use App\Models\HolidayEvent;
|
2026-04-21 17:53:11 +08:00
|
|
|
use App\Models\HolidayEventRun;
|
2026-04-03 13:55:36 +08:00
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 类功能:验证节日福利前台领取接口在批次模式下的行为。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
class HolidayControllerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证用户可以查询指定福利批次的待领取状态。
|
|
|
|
|
*/
|
|
|
|
|
public function test_can_check_holiday_run_status(): void
|
2026-04-03 13:55:36 +08:00
|
|
|
{
|
|
|
|
|
$user = User::factory()->create();
|
2026-04-21 17:53:11 +08:00
|
|
|
[$event, $run] = $this->createActiveRun();
|
2026-04-03 13:55:36 +08:00
|
|
|
|
|
|
|
|
HolidayClaim::create([
|
|
|
|
|
'event_id' => $event->id,
|
2026-04-21 17:53:11 +08:00
|
|
|
'run_id' => $run->id,
|
2026-04-03 13:55:36 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'amount' => 500,
|
2026-04-21 17:53:11 +08:00
|
|
|
'claimed_at' => null,
|
2026-04-03 13:55:36 +08:00
|
|
|
]);
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response = $this->actingAs($user)->getJson(route('holiday.status', ['run' => $run->id]));
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response->assertOk();
|
2026-04-03 13:55:36 +08:00
|
|
|
$response->assertJson([
|
|
|
|
|
'claimable' => true,
|
2026-04-21 17:53:11 +08:00
|
|
|
'claimed' => false,
|
2026-04-03 13:55:36 +08:00
|
|
|
'amount' => 500,
|
2026-04-21 17:53:11 +08:00
|
|
|
'status' => 'active',
|
2026-04-03 13:55:36 +08:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证用户领取批次福利后会入账并写回领取状态。
|
|
|
|
|
*/
|
|
|
|
|
public function test_can_claim_holiday_bonus_from_run(): void
|
2026-04-03 13:55:36 +08:00
|
|
|
{
|
|
|
|
|
$user = User::factory()->create(['jjb' => 100]);
|
2026-04-21 17:53:11 +08:00
|
|
|
[$event, $run] = $this->createActiveRun();
|
2026-04-03 13:55:36 +08:00
|
|
|
|
|
|
|
|
HolidayClaim::create([
|
|
|
|
|
'event_id' => $event->id,
|
2026-04-21 17:53:11 +08:00
|
|
|
'run_id' => $run->id,
|
2026-04-03 13:55:36 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'amount' => 500,
|
2026-04-21 17:53:11 +08:00
|
|
|
'claimed_at' => null,
|
2026-04-03 13:55:36 +08:00
|
|
|
]);
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['run' => $run->id]));
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response->assertOk();
|
|
|
|
|
$response->assertJson([
|
|
|
|
|
'ok' => true,
|
|
|
|
|
'amount' => 500,
|
|
|
|
|
]);
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$this->assertSame(600, (int) $user->fresh()->jjb);
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$this->assertDatabaseHas('holiday_claims', [
|
2026-04-03 13:55:36 +08:00
|
|
|
'event_id' => $event->id,
|
2026-04-21 17:53:11 +08:00
|
|
|
'run_id' => $run->id,
|
2026-04-03 13:55:36 +08:00
|
|
|
'user_id' => $user->id,
|
2026-04-21 17:53:11 +08:00
|
|
|
'amount' => 500,
|
2026-04-03 13:55:36 +08:00
|
|
|
]);
|
2026-04-21 17:53:11 +08:00
|
|
|
|
|
|
|
|
$claimedRow = HolidayClaim::query()
|
|
|
|
|
->where('run_id', $run->id)
|
|
|
|
|
->where('user_id', $user->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull($claimedRow?->claimed_at);
|
|
|
|
|
$this->assertSame('completed', $run->fresh()?->status);
|
|
|
|
|
$this->assertSame(1, (int) $run->fresh()?->claimed_count);
|
|
|
|
|
$this->assertSame(500, (int) $run->fresh()?->claimed_amount);
|
2026-04-03 13:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证不在批次名单中的用户不能领取福利。
|
|
|
|
|
*/
|
|
|
|
|
public function test_cannot_claim_if_not_in_run_list(): void
|
2026-04-03 13:55:36 +08:00
|
|
|
{
|
|
|
|
|
$user = User::factory()->create();
|
2026-04-21 17:53:11 +08:00
|
|
|
[, $run] = $this->createActiveRun();
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['run' => $run->id]));
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$response->assertOk();
|
|
|
|
|
$response->assertJson([
|
|
|
|
|
'ok' => false,
|
|
|
|
|
'message' => '您不在本次福利名单内,或活动已结束。',
|
|
|
|
|
]);
|
2026-04-03 13:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证已过期批次会拒绝领取。
|
|
|
|
|
*/
|
|
|
|
|
public function test_cannot_claim_if_run_is_expired(): void
|
2026-04-03 13:55:36 +08:00
|
|
|
{
|
|
|
|
|
$user = User::factory()->create();
|
2026-04-21 17:53:11 +08:00
|
|
|
[$event, $run] = $this->createActiveRun([
|
|
|
|
|
'status' => 'expired',
|
|
|
|
|
'expires_at' => now()->subDay(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HolidayClaim::create([
|
|
|
|
|
'event_id' => $event->id,
|
|
|
|
|
'run_id' => $run->id,
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'amount' => 500,
|
|
|
|
|
'claimed_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['run' => $run->id]));
|
|
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
|
$response->assertJson([
|
|
|
|
|
'ok' => false,
|
|
|
|
|
'message' => '活动已结束或已过期。',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 方法功能:创建一个默认可领取的节日福利模板与发放批次。
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, mixed> $runOverrides
|
|
|
|
|
* @return array{0: HolidayEvent, 1: HolidayEventRun}
|
|
|
|
|
*/
|
|
|
|
|
private function createActiveRun(array $runOverrides = []): array
|
|
|
|
|
{
|
2026-04-03 13:55:36 +08:00
|
|
|
$event = HolidayEvent::create([
|
2026-04-21 17:53:11 +08:00
|
|
|
'name' => '元旦快乐',
|
|
|
|
|
'description' => '测试节日福利模板',
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
'enabled' => true,
|
|
|
|
|
'expires_at' => null,
|
2026-04-03 13:55:36 +08:00
|
|
|
'max_claimants' => 10,
|
|
|
|
|
'claimed_amount' => 0,
|
2026-04-21 17:53:11 +08:00
|
|
|
'claimed_count' => 0,
|
2026-04-03 13:55:36 +08:00
|
|
|
'total_amount' => 5000,
|
2026-04-21 17:53:11 +08:00
|
|
|
'min_amount' => 100,
|
|
|
|
|
'max_amount' => 1000,
|
|
|
|
|
'fixed_amount' => 500,
|
2026-04-03 13:55:36 +08:00
|
|
|
'distribute_type' => 'fixed',
|
2026-04-21 17:53:11 +08:00
|
|
|
'expire_minutes' => 30,
|
2026-04-03 13:55:36 +08:00
|
|
|
'send_at' => now(),
|
|
|
|
|
'repeat_type' => 'once',
|
|
|
|
|
'target_type' => 'all',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
$run = HolidayEventRun::create(array_merge([
|
|
|
|
|
'holiday_event_id' => $event->id,
|
|
|
|
|
'event_name' => $event->name,
|
|
|
|
|
'event_description' => $event->description,
|
|
|
|
|
'total_amount' => $event->total_amount,
|
|
|
|
|
'max_claimants' => $event->max_claimants,
|
|
|
|
|
'distribute_type' => $event->distribute_type,
|
|
|
|
|
'min_amount' => $event->min_amount,
|
|
|
|
|
'max_amount' => $event->max_amount,
|
|
|
|
|
'fixed_amount' => $event->fixed_amount,
|
|
|
|
|
'target_type' => $event->target_type,
|
|
|
|
|
'target_value' => null,
|
|
|
|
|
'repeat_type' => $event->repeat_type,
|
|
|
|
|
'scheduled_for' => now(),
|
|
|
|
|
'triggered_at' => now(),
|
|
|
|
|
'expires_at' => now()->addDay(),
|
|
|
|
|
'status' => 'active',
|
|
|
|
|
'audience_count' => 0,
|
|
|
|
|
'claimed_count' => 0,
|
|
|
|
|
'claimed_amount' => 0,
|
|
|
|
|
], $runOverrides));
|
2026-04-03 13:55:36 +08:00
|
|
|
|
2026-04-21 17:53:11 +08:00
|
|
|
return [$event, $run];
|
2026-04-03 13:55:36 +08:00
|
|
|
}
|
|
|
|
|
}
|