Files
chatroom/tests/Feature/RedPacketControllerTest.php
T

228 lines
7.0 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature;
2026-04-21 15:10:41 +08:00
use App\Events\RedPacketClaimed;
use App\Models\RedPacketEnvelope;
use App\Models\Sysparam;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
2026-04-21 15:10:41 +08:00
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;
use Tests\TestCase;
2026-04-21 15:10:41 +08:00
/**
* 类功能:验证礼包红包控制器的发包、领取与状态查询流程
*/
class RedPacketControllerTest extends TestCase
{
use RefreshDatabase;
2026-04-21 15:10:41 +08:00
/**
* 方法功能:初始化红包测试所需的 Redis 与站长等级配置。
*/
protected function setUp(): void
{
parent::setUp();
Redis::flushall();
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证普通用户不能发送礼包。
*/
public function test_normal_user_cannot_send_red_packet(): void
{
$user = User::factory()->create(['user_level' => 10]);
$response = $this->actingAs($user)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response->assertStatus(403);
$response->assertJson(['status' => 'error']);
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证站长可以成功发出礼包并写入 Redis 拆包结果。
*/
public function test_superadmin_can_send_red_packet(): void
{
$admin = User::factory()->create(['user_level' => 100]);
$response = $this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response->assertStatus(200);
$response->assertJson(['status' => 'success']);
$this->assertDatabaseHas('red_packet_envelopes', [
'sender_id' => $admin->id,
'room_id' => 1,
'type' => 'gold',
'status' => 'active',
]);
$envelope = RedPacketEnvelope::first();
// Check Redis for parts
$this->assertEquals(10, Redis::llen("red_packet:{$envelope->id}:amounts"));
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证同一房间内不可重复发送未结束的礼包。
*/
public function test_cannot_send_multiple_active_packets_in_same_room(): void
{
$admin = User::factory()->create(['user_level' => 100]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response = $this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$response->assertStatus(422);
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证用户可以领取礼包并增加金币余额。
*/
public function test_user_can_claim_red_packet(): void
{
$admin = User::factory()->create(['user_level' => 100]);
$user = User::factory()->create(['jjb' => 100]);
// Send packet
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$envelope = RedPacketEnvelope::first();
// Claim packet
$response = $this->actingAs($user)->postJson(route('red_packet.claim', ['envelopeId' => $envelope->id]), [
'room_id' => 1,
]);
$response->assertStatus(200);
$response->assertJson(['status' => 'success']);
$this->assertDatabaseHas('red_packet_claims', [
'envelope_id' => $envelope->id,
'user_id' => $user->id,
]);
// Verify currency incremented
$this->assertGreaterThan(100, $user->fresh()->jjb);
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证领取成功后会返回并广播最新剩余份数。
*/
public function test_claim_red_packet_returns_and_broadcasts_remaining_count(): void
{
Event::fake([RedPacketClaimed::class]);
$admin = User::factory()->create(['user_level' => 100]);
$user = User::factory()->create(['jjb' => 100]);
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$envelope = RedPacketEnvelope::query()->firstOrFail();
$response = $this->actingAs($user)->postJson(route('red_packet.claim', ['envelopeId' => $envelope->id]), [
'room_id' => 1,
]);
$response->assertOk();
$response->assertJson([
'status' => 'success',
'remaining_count' => 9,
]);
Event::assertDispatched(RedPacketClaimed::class, function (RedPacketClaimed $event) use ($user, $envelope): bool {
return $event->claimer->is($user)
&& $event->envelopeId === $envelope->id
&& $event->roomId === 1
&& $event->remainingCount === 9
&& $event->type === 'gold';
});
}
/**
* 方法功能:验证同一用户不能重复领取同一个礼包。
*/
public function test_user_cannot_claim_same_packet_twice(): void
{
$admin = User::factory()->create(['user_level' => 100]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$envelope = RedPacketEnvelope::first();
// First claim
$this->actingAs($user)->postJson(route('red_packet.claim', ['envelopeId' => $envelope->id]), [
'room_id' => 1,
]);
// Second claim
$response = $this->actingAs($user)->postJson(route('red_packet.claim', ['envelopeId' => $envelope->id]), [
'room_id' => 1,
]);
$response->assertStatus(422);
$response->assertJson(['message' => '您已经领过这个礼包了']);
}
2026-04-21 15:10:41 +08:00
/**
* 方法功能:验证可以查询礼包状态并识别当前用户是否已领取。
*/
public function test_can_check_packet_status(): void
{
$admin = User::factory()->create(['user_level' => 100]);
$user = User::factory()->create();
$this->actingAs($admin)->postJson(route('command.red_packet.send'), [
'room_id' => 1,
'type' => 'gold',
]);
$envelope = RedPacketEnvelope::first();
$response = $this->actingAs($user)->getJson(route('red_packet.status', ['envelopeId' => $envelope->id]));
$response->assertStatus(200);
$response->assertJson([
'status' => 'success',
'has_claimed' => false,
'is_expired' => false,
]);
// Claim it
$this->actingAs($user)->postJson(route('red_packet.claim', ['envelopeId' => $envelope->id]), [
'room_id' => 1,
]);
$response2 = $this->actingAs($user)->getJson(route('red_packet.status', ['envelopeId' => $envelope->id]));
$response2->assertJson([
'status' => 'success',
'has_claimed' => true,
]);
}
}