优化 红包 页面
This commit is contained in:
@@ -2,17 +2,25 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Events\RedPacketClaimed;
|
||||
use App\Models\RedPacketEnvelope;
|
||||
use App\Models\Sysparam;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 类功能:验证礼包红包控制器的发包、领取与状态查询流程
|
||||
*/
|
||||
class RedPacketControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* 方法功能:初始化红包测试所需的 Redis 与站长等级配置。
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
@@ -20,7 +28,10 @@ class RedPacketControllerTest extends TestCase
|
||||
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
|
||||
}
|
||||
|
||||
public function test_normal_user_cannot_send_red_packet()
|
||||
/**
|
||||
* 方法功能:验证普通用户不能发送礼包。
|
||||
*/
|
||||
public function test_normal_user_cannot_send_red_packet(): void
|
||||
{
|
||||
$user = User::factory()->create(['user_level' => 10]);
|
||||
|
||||
@@ -33,7 +44,10 @@ class RedPacketControllerTest extends TestCase
|
||||
$response->assertJson(['status' => 'error']);
|
||||
}
|
||||
|
||||
public function test_superadmin_can_send_red_packet()
|
||||
/**
|
||||
* 方法功能:验证站长可以成功发出礼包并写入 Redis 拆包结果。
|
||||
*/
|
||||
public function test_superadmin_can_send_red_packet(): void
|
||||
{
|
||||
$admin = User::factory()->create(['user_level' => 100]);
|
||||
|
||||
@@ -57,7 +71,10 @@ class RedPacketControllerTest extends TestCase
|
||||
$this->assertEquals(10, Redis::llen("red_packet:{$envelope->id}:amounts"));
|
||||
}
|
||||
|
||||
public function test_cannot_send_multiple_active_packets_in_same_room()
|
||||
/**
|
||||
* 方法功能:验证同一房间内不可重复发送未结束的礼包。
|
||||
*/
|
||||
public function test_cannot_send_multiple_active_packets_in_same_room(): void
|
||||
{
|
||||
$admin = User::factory()->create(['user_level' => 100]);
|
||||
|
||||
@@ -74,7 +91,10 @@ class RedPacketControllerTest extends TestCase
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
public function test_user_can_claim_red_packet()
|
||||
/**
|
||||
* 方法功能:验证用户可以领取礼包并增加金币余额。
|
||||
*/
|
||||
public function test_user_can_claim_red_packet(): void
|
||||
{
|
||||
$admin = User::factory()->create(['user_level' => 100]);
|
||||
$user = User::factory()->create(['jjb' => 100]);
|
||||
@@ -104,7 +124,46 @@ class RedPacketControllerTest extends TestCase
|
||||
$this->assertGreaterThan(100, $user->fresh()->jjb);
|
||||
}
|
||||
|
||||
public function test_user_cannot_claim_same_packet_twice()
|
||||
/**
|
||||
* 方法功能:验证领取成功后会返回并广播最新剩余份数。
|
||||
*/
|
||||
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();
|
||||
@@ -130,7 +189,10 @@ class RedPacketControllerTest extends TestCase
|
||||
$response->assertJson(['message' => '您已经领过这个礼包了']);
|
||||
}
|
||||
|
||||
public function test_can_check_packet_status()
|
||||
/**
|
||||
* 方法功能:验证可以查询礼包状态并识别当前用户是否已领取。
|
||||
*/
|
||||
public function test_can_check_packet_status(): void
|
||||
{
|
||||
$admin = User::factory()->create(['user_level' => 100]);
|
||||
$user = User::factory()->create();
|
||||
|
||||
Reference in New Issue
Block a user