79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Tests\TestCase;
|
|
|
|
class EarnControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Redis::flushall(); // Clear redis keys so they don't leak between tests
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_can_claim_video_reward()
|
|
{
|
|
$user = User::factory()->create([
|
|
'jjb' => 0,
|
|
'exp_num' => 0,
|
|
]);
|
|
|
|
$response = $this->actingAs($user)->postJson(route('earn.video_reward'), [
|
|
'room_id' => 1,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => true,
|
|
]);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'id' => $user->id,
|
|
'jjb' => 5000, // Reward is hardcoded to 5000
|
|
'exp_num' => 500, // Exp is hardcoded to 500
|
|
]);
|
|
|
|
// Cooldown should be set
|
|
$this->assertTrue((bool) Redis::exists("earn_video:cooldown:{$user->id}"));
|
|
}
|
|
|
|
public function test_cannot_claim_during_cooldown()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
// First claim
|
|
$this->actingAs($user)->postJson(route('earn.video_reward'));
|
|
|
|
// Second claim immediately should fail due to cooldown
|
|
$response = $this->actingAs($user)->postJson(route('earn.video_reward'));
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => false,
|
|
'message' => '操作过快,请稍后再试。',
|
|
]);
|
|
}
|
|
|
|
public function test_cannot_exceed_daily_limit()
|
|
{
|
|
$user = User::factory()->create();
|
|
$dateKey = now()->format('Y-m-d');
|
|
|
|
// Manually set daily count to max limit (3)
|
|
Redis::set("earn_video:count:{$user->id}:{$dateKey}", 3);
|
|
|
|
$response = $this->actingAs($user)->postJson(route('earn.video_reward'));
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment([
|
|
'success' => false,
|
|
]);
|
|
}
|
|
}
|