升级节日福利年度调度与批次领取
This commit is contained in:
@@ -1,139 +1,194 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:节日福利前台领取功能测试
|
||||
*
|
||||
* 覆盖按批次查询状态、领取成功、名单缺失与过期拒绝等核心路径,
|
||||
* 确保 run_id 改造后前台接口仍能正确工作。
|
||||
*/
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\HolidayClaim;
|
||||
use App\Models\HolidayEvent;
|
||||
use App\Models\HolidayEventRun;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 类功能:验证节日福利前台领取接口在批次模式下的行为。
|
||||
*/
|
||||
class HolidayControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_can_check_holiday_status()
|
||||
/**
|
||||
* 方法功能:验证用户可以查询指定福利批次的待领取状态。
|
||||
*/
|
||||
public function test_can_check_holiday_run_status(): void
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = User::factory()->create();
|
||||
$event = HolidayEvent::create([
|
||||
'name' => 'Test Holiday',
|
||||
'status' => 'active',
|
||||
'expires_at' => now()->addDays(1),
|
||||
'max_claimants' => 10,
|
||||
'claimed_amount' => 0,
|
||||
'total_amount' => 5000,
|
||||
'distribute_type' => 'fixed',
|
||||
'send_at' => now(),
|
||||
'repeat_type' => 'once',
|
||||
'target_type' => 'all',
|
||||
]);
|
||||
[$event, $run] = $this->createActiveRun();
|
||||
|
||||
HolidayClaim::create([
|
||||
'event_id' => $event->id,
|
||||
'run_id' => $run->id,
|
||||
'user_id' => $user->id,
|
||||
'amount' => 500,
|
||||
'claimed_at' => now(),
|
||||
'claimed_at' => null,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('holiday.status', ['event' => $event->id]));
|
||||
$response = $this->actingAs($user)->getJson(route('holiday.status', ['run' => $run->id]));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'claimable' => true,
|
||||
'claimed' => false,
|
||||
'amount' => 500,
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_can_claim_holiday_bonus()
|
||||
/**
|
||||
* 方法功能:验证用户领取批次福利后会入账并写回领取状态。
|
||||
*/
|
||||
public function test_can_claim_holiday_bonus_from_run(): void
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = User::factory()->create(['jjb' => 100]);
|
||||
$event = HolidayEvent::create([
|
||||
'name' => 'Test Holiday',
|
||||
'status' => 'active',
|
||||
'expires_at' => now()->addDays(1),
|
||||
'max_claimants' => 10,
|
||||
'claimed_amount' => 0,
|
||||
'total_amount' => 5000,
|
||||
'distribute_type' => 'fixed',
|
||||
'send_at' => now(),
|
||||
'repeat_type' => 'once',
|
||||
'target_type' => 'all',
|
||||
[$event, $run] = $this->createActiveRun();
|
||||
|
||||
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' => true,
|
||||
'amount' => 500,
|
||||
]);
|
||||
|
||||
$this->assertSame(600, (int) $user->fresh()->jjb);
|
||||
|
||||
$this->assertDatabaseHas('holiday_claims', [
|
||||
'event_id' => $event->id,
|
||||
'run_id' => $run->id,
|
||||
'user_id' => $user->id,
|
||||
'amount' => 500,
|
||||
]);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证不在批次名单中的用户不能领取福利。
|
||||
*/
|
||||
public function test_cannot_claim_if_not_in_run_list(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
[, $run] = $this->createActiveRun();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['run' => $run->id]));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'ok' => false,
|
||||
'message' => '您不在本次福利名单内,或活动已结束。',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证已过期批次会拒绝领取。
|
||||
*/
|
||||
public function test_cannot_claim_if_run_is_expired(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
[$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' => now(),
|
||||
'claimed_at' => null,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['event' => $event->id]));
|
||||
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['run' => $run->id]));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['ok' => true]);
|
||||
|
||||
// Verify currency incremented
|
||||
$this->assertEquals(600, $user->fresh()->jjb);
|
||||
|
||||
// Verify claim is deleted
|
||||
$this->assertDatabaseMissing('holiday_claims', [
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
$response->assertOk();
|
||||
$response->assertJson([
|
||||
'ok' => false,
|
||||
'message' => '活动已结束或已过期。',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_cannot_claim_if_not_in_list()
|
||||
/**
|
||||
* 方法功能:创建一个默认可领取的节日福利模板与发放批次。
|
||||
*
|
||||
* @param array<string, mixed> $runOverrides
|
||||
* @return array{0: HolidayEvent, 1: HolidayEventRun}
|
||||
*/
|
||||
private function createActiveRun(array $runOverrides = []): array
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = User::factory()->create();
|
||||
$event = HolidayEvent::create([
|
||||
'name' => 'Test Holiday',
|
||||
'name' => '元旦快乐',
|
||||
'description' => '测试节日福利模板',
|
||||
'status' => 'pending',
|
||||
'enabled' => true,
|
||||
'expires_at' => null,
|
||||
'max_claimants' => 10,
|
||||
'claimed_amount' => 0,
|
||||
'claimed_count' => 0,
|
||||
'total_amount' => 5000,
|
||||
'min_amount' => 100,
|
||||
'max_amount' => 1000,
|
||||
'fixed_amount' => 500,
|
||||
'distribute_type' => 'fixed',
|
||||
'expire_minutes' => 30,
|
||||
'send_at' => now(),
|
||||
'repeat_type' => 'once',
|
||||
'target_type' => 'all',
|
||||
]);
|
||||
|
||||
$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',
|
||||
'expires_at' => now()->addDays(1),
|
||||
'max_claimants' => 10,
|
||||
'audience_count' => 0,
|
||||
'claimed_count' => 0,
|
||||
'claimed_amount' => 0,
|
||||
'total_amount' => 5000,
|
||||
'distribute_type' => 'fixed',
|
||||
'send_at' => now(),
|
||||
'repeat_type' => 'once',
|
||||
'target_type' => 'all',
|
||||
]);
|
||||
], $runOverrides));
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['event' => $event->id]));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['ok' => false, 'message' => '您不在本次福利名单内,或活动已结束。']);
|
||||
}
|
||||
|
||||
public function test_cannot_claim_if_expired()
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = User::factory()->create();
|
||||
$event = HolidayEvent::create([
|
||||
'name' => 'Test Holiday',
|
||||
'status' => 'completed',
|
||||
'expires_at' => now()->subDays(1),
|
||||
'max_claimants' => 10,
|
||||
'claimed_amount' => 0,
|
||||
'total_amount' => 5000,
|
||||
'distribute_type' => 'fixed',
|
||||
'send_at' => now(),
|
||||
'repeat_type' => 'once',
|
||||
'target_type' => 'all',
|
||||
]);
|
||||
|
||||
HolidayClaim::create([
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
'amount' => 500,
|
||||
'claimed_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('holiday.claim', ['event' => $event->id]));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['ok' => false, 'message' => '活动已结束或已过期。']);
|
||||
return [$event, $run];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:节日福利调度与触发任务测试
|
||||
*
|
||||
* 覆盖年度节日多天/多轮推导、手动立即触发与每轮重新计算名单等关键行为,
|
||||
* 确保模板调度与发放批次生成逻辑稳定。
|
||||
*/
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Events\HolidayEventStarted;
|
||||
use App\Events\MessageSent;
|
||||
use App\Jobs\SaveMessageJob;
|
||||
use App\Jobs\TriggerHolidayEventJob;
|
||||
use App\Models\HolidayEvent;
|
||||
use App\Models\HolidayEventRun;
|
||||
use App\Models\User;
|
||||
use App\Services\ChatStateService;
|
||||
use App\Services\HolidayEventScheduleService;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 类功能:验证节日福利模板的调度推导和批次生成逻辑。
|
||||
*/
|
||||
class HolidayEventSchedulingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* 方法功能:验证年度节日可跨连续多天推进,并在最后一天后滚到下一年。
|
||||
*/
|
||||
public function test_yearly_schedule_advances_across_days_then_rolls_to_next_year(): void
|
||||
{
|
||||
$service = app(HolidayEventScheduleService::class);
|
||||
|
||||
$event = $this->createYearlyEvent([
|
||||
'schedule_month' => 1,
|
||||
'schedule_day' => 1,
|
||||
'schedule_time' => '09:00',
|
||||
'duration_days' => 3,
|
||||
'daily_occurrences' => 1,
|
||||
'occurrence_interval_minutes' => null,
|
||||
'send_at' => '2026-01-01 09:00:00',
|
||||
]);
|
||||
|
||||
$nextDay = $service->advanceAfterTrigger($event);
|
||||
$this->assertSame('2026-01-02 09:00:00', $nextDay?->format('Y-m-d H:i:s'));
|
||||
|
||||
$event->forceFill(['send_at' => CarbonImmutable::parse('2026-01-03 09:00:00')]);
|
||||
$nextYear = $service->advanceAfterTrigger($event);
|
||||
|
||||
$this->assertSame('2027-01-01 09:00:00', $nextYear?->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证年度节日支持同一天多轮发送并在最后一轮后切到下一年。
|
||||
*/
|
||||
public function test_yearly_schedule_advances_across_same_day_multiple_occurrences(): void
|
||||
{
|
||||
$service = app(HolidayEventScheduleService::class);
|
||||
|
||||
$event = $this->createYearlyEvent([
|
||||
'schedule_month' => 8,
|
||||
'schedule_day' => 1,
|
||||
'schedule_time' => '09:00',
|
||||
'duration_days' => 1,
|
||||
'daily_occurrences' => 3,
|
||||
'occurrence_interval_minutes' => 180,
|
||||
'send_at' => '2026-08-01 09:00:00',
|
||||
]);
|
||||
|
||||
$secondRound = $service->advanceAfterTrigger($event);
|
||||
$this->assertSame('2026-08-01 12:00:00', $secondRound?->format('Y-m-d H:i:s'));
|
||||
|
||||
$event->forceFill(['send_at' => CarbonImmutable::parse('2026-08-01 12:00:00')]);
|
||||
$thirdRound = $service->advanceAfterTrigger($event);
|
||||
$this->assertSame('2026-08-01 15:00:00', $thirdRound?->format('Y-m-d H:i:s'));
|
||||
|
||||
$event->forceFill(['send_at' => CarbonImmutable::parse('2026-08-01 15:00:00')]);
|
||||
$nextYear = $service->advanceAfterTrigger($event);
|
||||
|
||||
$this->assertSame('2027-08-01 09:00:00', $nextYear?->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证手动立即触发的每一轮都会重新按当时在线用户生成名单。
|
||||
*/
|
||||
public function test_manual_trigger_recomputes_audience_for_each_run(): void
|
||||
{
|
||||
Event::fake([HolidayEventStarted::class, MessageSent::class]);
|
||||
Queue::fake([SaveMessageJob::class]);
|
||||
|
||||
$firstUser = User::factory()->create(['username' => 'holiday-a']);
|
||||
$secondUser = User::factory()->create(['username' => 'holiday-b']);
|
||||
|
||||
Redis::shouldReceive('hgetall')
|
||||
->twice()
|
||||
->andReturn(
|
||||
['holiday-a' => json_encode(['user_id' => $firstUser->id], JSON_UNESCAPED_UNICODE)],
|
||||
['holiday-b' => json_encode(['user_id' => $secondUser->id], JSON_UNESCAPED_UNICODE)],
|
||||
);
|
||||
|
||||
$chatState = $this->createMock(ChatStateService::class);
|
||||
$chatState->expects($this->exactly(2))->method('nextMessageId')->with(1)->willReturnOnConsecutiveCalls(1, 2);
|
||||
$chatState->expects($this->exactly(2))->method('pushMessage');
|
||||
|
||||
$event = $this->createYearlyEvent([
|
||||
'send_at' => now()->addDay()->toDateTimeString(),
|
||||
]);
|
||||
|
||||
(new TriggerHolidayEventJob($event, true))->handle($chatState, app(HolidayEventScheduleService::class));
|
||||
(new TriggerHolidayEventJob($event, true))->handle($chatState, app(HolidayEventScheduleService::class));
|
||||
|
||||
$runs = HolidayEventRun::query()->orderBy('id')->get();
|
||||
|
||||
$this->assertCount(2, $runs);
|
||||
$this->assertDatabaseHas('holiday_claims', ['run_id' => $runs[0]->id, 'user_id' => $firstUser->id]);
|
||||
$this->assertDatabaseHas('holiday_claims', ['run_id' => $runs[1]->id, 'user_id' => $secondUser->id]);
|
||||
$this->assertDatabaseMissing('holiday_claims', ['run_id' => $runs[0]->id, 'user_id' => $secondUser->id]);
|
||||
$this->assertDatabaseMissing('holiday_claims', ['run_id' => $runs[1]->id, 'user_id' => $firstUser->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证手动立即触发不会破坏年度模板原本的 send_at 锚点。
|
||||
*/
|
||||
public function test_manual_trigger_does_not_change_yearly_anchor_send_at(): void
|
||||
{
|
||||
Event::fake([HolidayEventStarted::class, MessageSent::class]);
|
||||
Queue::fake([SaveMessageJob::class]);
|
||||
Redis::shouldReceive('hgetall')->once()->andReturn([]);
|
||||
|
||||
$chatState = $this->createMock(ChatStateService::class);
|
||||
$event = $this->createYearlyEvent([
|
||||
'send_at' => '2026-08-01 09:00:00',
|
||||
]);
|
||||
|
||||
(new TriggerHolidayEventJob($event, true))->handle($chatState, app(HolidayEventScheduleService::class));
|
||||
|
||||
$this->assertSame('2026-08-01 09:00:00', $event->fresh()?->send_at?->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:验证旧的 daily 重复模式仍按原逻辑继续推进。
|
||||
*/
|
||||
public function test_daily_repeat_mode_still_advances_normally(): void
|
||||
{
|
||||
$service = app(HolidayEventScheduleService::class);
|
||||
|
||||
$event = HolidayEvent::create([
|
||||
'name' => '普通重复福利',
|
||||
'description' => 'daily 兼容测试',
|
||||
'total_amount' => 5000,
|
||||
'max_claimants' => 20,
|
||||
'distribute_type' => 'fixed',
|
||||
'min_amount' => 100,
|
||||
'max_amount' => null,
|
||||
'fixed_amount' => 500,
|
||||
'send_at' => CarbonImmutable::parse('2026-05-01 09:00:00'),
|
||||
'expire_minutes' => 30,
|
||||
'repeat_type' => 'daily',
|
||||
'target_type' => 'all',
|
||||
'status' => 'pending',
|
||||
'enabled' => true,
|
||||
'claimed_count' => 0,
|
||||
'claimed_amount' => 0,
|
||||
'duration_days' => 1,
|
||||
'daily_occurrences' => 1,
|
||||
]);
|
||||
|
||||
$nextSendAt = $service->advanceAfterTrigger($event);
|
||||
|
||||
$this->assertSame('2026-05-02 09:00:00', $nextSendAt?->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:创建一个年度节日模板。
|
||||
*
|
||||
* @param array<string, mixed> $overrides
|
||||
*/
|
||||
private function createYearlyEvent(array $overrides = []): HolidayEvent
|
||||
{
|
||||
return HolidayEvent::create(array_merge([
|
||||
'name' => '建军节福利',
|
||||
'description' => '年度节日调度测试',
|
||||
'total_amount' => 9000,
|
||||
'max_claimants' => 30,
|
||||
'distribute_type' => 'fixed',
|
||||
'min_amount' => 100,
|
||||
'max_amount' => null,
|
||||
'fixed_amount' => 300,
|
||||
'send_at' => '2026-08-01 09:00:00',
|
||||
'expire_minutes' => 30,
|
||||
'repeat_type' => 'yearly',
|
||||
'schedule_month' => 8,
|
||||
'schedule_day' => 1,
|
||||
'schedule_time' => '09:00',
|
||||
'duration_days' => 1,
|
||||
'daily_occurrences' => 1,
|
||||
'occurrence_interval_minutes' => null,
|
||||
'target_type' => 'all',
|
||||
'target_value' => null,
|
||||
'status' => 'pending',
|
||||
'enabled' => true,
|
||||
'claimed_count' => 0,
|
||||
'claimed_amount' => 0,
|
||||
], $overrides));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user