升级节日福利年度调度与批次领取
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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user