Add baccarat loss cover activity
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:百家乐买单活动功能测试
|
||||
*
|
||||
* 覆盖活动创建、下注关联、补偿领取与历史记录接口,
|
||||
* 确保活动统计与金币流水都能正确落库。
|
||||
*/
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\CurrencySource;
|
||||
use App\Models\BaccaratLossCoverEvent;
|
||||
use App\Models\BaccaratLossCoverRecord;
|
||||
use App\Models\BaccaratRound;
|
||||
use App\Models\GameConfig;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BaccaratLossCoverControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* 初始化百家乐基础配置。
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
GameConfig::updateOrCreate(
|
||||
['game_key' => 'baccarat'],
|
||||
[
|
||||
'name' => 'Baccarat',
|
||||
'icon' => 'baccarat',
|
||||
'description' => 'Baccarat Game',
|
||||
'enabled' => true,
|
||||
'params' => [
|
||||
'min_bet' => 100,
|
||||
'max_bet' => 50000,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 superlevel 管理员可以创建百家乐买单活动。
|
||||
*/
|
||||
public function test_superlevel_admin_can_create_baccarat_loss_cover_event(): void
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$admin = User::factory()->create(['user_level' => 100]);
|
||||
|
||||
$response = $this->actingAs($admin)->postJson(route('command.baccarat_loss_cover.store'), [
|
||||
'title' => '你玩游戏我买单',
|
||||
'description' => '测试活动',
|
||||
'starts_at' => now()->addMinutes(5)->toDateTimeString(),
|
||||
'ends_at' => now()->addMinutes(35)->toDateTimeString(),
|
||||
'claim_deadline_at' => now()->addDay()->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
|
||||
$this->assertDatabaseHas('baccarat_loss_cover_events', [
|
||||
'title' => '你玩游戏我买单',
|
||||
'created_by_user_id' => $admin->id,
|
||||
'status' => 'scheduled',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证活动进行中下注会挂到活动并写入用户聚合记录。
|
||||
*/
|
||||
public function test_bet_during_active_event_is_tracked_in_loss_cover_record(): void
|
||||
{
|
||||
Event::fake();
|
||||
|
||||
$user = User::factory()->create(['jjb' => 500]);
|
||||
$event = BaccaratLossCoverEvent::factory()->create([
|
||||
'status' => 'active',
|
||||
'starts_at' => now()->subMinutes(2),
|
||||
'ends_at' => now()->addMinutes(20),
|
||||
]);
|
||||
|
||||
$round = BaccaratRound::forceCreate([
|
||||
'status' => 'betting',
|
||||
'bet_opens_at' => now(),
|
||||
'bet_closes_at' => now()->addMinutes(1),
|
||||
'total_bet_big' => 0,
|
||||
'total_bet_small' => 0,
|
||||
'total_bet_triple' => 0,
|
||||
'bet_count' => 0,
|
||||
'bet_count_big' => 0,
|
||||
'bet_count_small' => 0,
|
||||
'bet_count_triple' => 0,
|
||||
'total_payout' => 0,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('baccarat.bet'), [
|
||||
'round_id' => $round->id,
|
||||
'bet_type' => 'big',
|
||||
'amount' => 100,
|
||||
]);
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
|
||||
$this->assertDatabaseHas('baccarat_bets', [
|
||||
'round_id' => $round->id,
|
||||
'user_id' => $user->id,
|
||||
'loss_cover_event_id' => $event->id,
|
||||
'amount' => 100,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('baccarat_loss_cover_records', [
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
'total_bet_amount' => 100,
|
||||
'claim_status' => 'not_eligible',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('baccarat_loss_cover_events', [
|
||||
'id' => $event->id,
|
||||
'participant_count' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证用户领取补偿后会增加金币并写入金币流水。
|
||||
*/
|
||||
public function test_user_can_claim_baccarat_loss_cover_and_currency_log_is_written(): void
|
||||
{
|
||||
$user = User::factory()->create(['jjb' => 200]);
|
||||
$event = BaccaratLossCoverEvent::factory()->create([
|
||||
'status' => 'claimable',
|
||||
'claim_deadline_at' => now()->addHours(12),
|
||||
'total_loss_amount' => 300,
|
||||
]);
|
||||
|
||||
BaccaratLossCoverRecord::factory()->create([
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
'total_bet_amount' => 600,
|
||||
'total_loss_amount' => 300,
|
||||
'compensation_amount' => 300,
|
||||
'claim_status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('baccarat-loss-cover.claim', $event));
|
||||
|
||||
$response->assertOk()->assertJson([
|
||||
'ok' => true,
|
||||
'amount' => 300,
|
||||
]);
|
||||
|
||||
$this->assertSame(500, (int) $user->fresh()->jjb);
|
||||
|
||||
$this->assertDatabaseHas('baccarat_loss_cover_records', [
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
'claim_status' => 'claimed',
|
||||
'claimed_amount' => 300,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('user_currency_logs', [
|
||||
'user_id' => $user->id,
|
||||
'currency' => 'gold',
|
||||
'amount' => 300,
|
||||
'source' => CurrencySource::BACCARAT_LOSS_COVER_CLAIM->value,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证历史接口会返回当前用户在活动中的领取状态。
|
||||
*/
|
||||
public function test_history_endpoint_contains_my_claim_status(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$event = BaccaratLossCoverEvent::factory()->create([
|
||||
'status' => 'completed',
|
||||
'total_claimed_amount' => 400,
|
||||
]);
|
||||
|
||||
BaccaratLossCoverRecord::factory()->create([
|
||||
'event_id' => $event->id,
|
||||
'user_id' => $user->id,
|
||||
'claim_status' => 'claimed',
|
||||
'claimed_amount' => 400,
|
||||
'compensation_amount' => 400,
|
||||
'claimed_at' => now(),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('baccarat-loss-cover.history'));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('events.0.id', $event->id);
|
||||
$response->assertJsonPath('events.0.my_record.claim_status', 'claimed');
|
||||
$response->assertJsonPath('events.0.my_record.claimed_amount', 400);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user