Files
chatroom/tests/Feature/RedPacketControllerTest.php

166 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature;
use App\Models\RedPacketEnvelope;
use App\Models\Sysparam;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Redis;
use Tests\TestCase;
class RedPacketControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Redis::flushall();
Sysparam::updateOrCreate(['alias' => 'superlevel'], ['body' => '100']);
}
public function test_normal_user_cannot_send_red_packet()
{
$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']);
}
public function test_superadmin_can_send_red_packet()
{
$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"));
}
public function test_cannot_send_multiple_active_packets_in_same_room()
{
$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);
}
public function test_user_can_claim_red_packet()
{
$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);
}
public function test_user_cannot_claim_same_packet_twice()
{
$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' => '您已经领过这个礼包了']);
}
public function test_can_check_packet_status()
{
$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,
]);
}
}