2026-04-03 13:55:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 文件功能:赛马竞猜功能测试
|
|
|
|
|
*
|
|
|
|
|
* 覆盖当前场次查询、下注约束、历史记录以及结算广播数据,
|
|
|
|
|
* 用于保证赛马玩法的接口行为与奖金展示链路稳定。
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-03 13:55:36 +08:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
use App\Events\HorseRaceSettled;
|
2026-04-11 16:11:00 +08:00
|
|
|
use App\Jobs\CloseHorseRaceJob;
|
2026-04-03 13:55:36 +08:00
|
|
|
use App\Models\GameConfig;
|
|
|
|
|
use App\Models\HorseBet;
|
|
|
|
|
use App\Models\HorseRace;
|
|
|
|
|
use App\Models\User;
|
2026-04-11 16:11:00 +08:00
|
|
|
use App\Services\ChatStateService;
|
2026-04-03 13:55:36 +08:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
2026-04-11 16:11:00 +08:00
|
|
|
use Illuminate\Support\Facades\Queue;
|
2026-04-03 13:55:36 +08:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 类功能:赛马竞猜控制器与结算流程测试类
|
|
|
|
|
*
|
|
|
|
|
* 验证前台接口、结算任务以及广播载荷,防止下注和中奖金额
|
|
|
|
|
* 相关逻辑出现回归。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
class HorseRaceControllerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:初始化赛马默认配置。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
GameConfig::updateOrCreate(
|
|
|
|
|
['game_key' => 'horse_racing'],
|
|
|
|
|
[
|
|
|
|
|
'name' => 'Horse Racing',
|
|
|
|
|
'icon' => 'horse',
|
|
|
|
|
'description' => 'Horse Racing Game',
|
|
|
|
|
'enabled' => true,
|
|
|
|
|
'params' => [
|
|
|
|
|
'min_bet' => 100,
|
|
|
|
|
'max_bet' => 100000,
|
|
|
|
|
'house_take_percent' => 5,
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证可获取当前进行中的场次。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
public function test_can_get_current_race()
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User $user */
|
2026-04-12 22:31:35 +08:00
|
|
|
$user = User::factory()->create(['jjb' => 4567]);
|
2026-04-03 13:55:36 +08:00
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'betting',
|
|
|
|
|
'bet_opens_at' => now(),
|
|
|
|
|
'bet_closes_at' => now()->addMinutes(1),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 0,
|
2026-04-11 16:27:04 +08:00
|
|
|
'total_pool' => 10000,
|
2026-04-03 13:55:36 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->getJson(route('horse-race.current'));
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJsonStructure(['race' => ['id', 'status', 'bet_closes_at', 'horses']]);
|
|
|
|
|
$this->assertEquals($race->id, $response->json('race.id'));
|
2026-04-12 22:31:35 +08:00
|
|
|
$this->assertSame(4567, $response->json('jjb'));
|
2026-04-03 13:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证当前场次总注池会包含种子池金额。
|
|
|
|
|
*/
|
2026-04-11 16:27:04 +08:00
|
|
|
public function test_current_race_total_pool_includes_seed_pool(): void
|
|
|
|
|
{
|
|
|
|
|
GameConfig::updateOrCreate(
|
|
|
|
|
['game_key' => 'horse_racing'],
|
|
|
|
|
[
|
|
|
|
|
'name' => 'Horse Racing',
|
|
|
|
|
'icon' => 'horse',
|
|
|
|
|
'description' => 'Horse Racing Game',
|
|
|
|
|
'enabled' => true,
|
|
|
|
|
'params' => [
|
|
|
|
|
'min_bet' => 100,
|
|
|
|
|
'max_bet' => 100000,
|
|
|
|
|
'house_take_percent' => 5,
|
|
|
|
|
'seed_pool' => 10000,
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'betting',
|
|
|
|
|
'bet_opens_at' => now(),
|
|
|
|
|
'bet_closes_at' => now()->addMinutes(1),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 0,
|
|
|
|
|
'total_pool' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->getJson(route('horse-race.current'));
|
|
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
|
$this->assertSame($race->id, $response->json('race.id'));
|
|
|
|
|
$this->assertSame(10000, $response->json('race.total_pool'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证跑马阶段的总注池不会重复叠加下注金额。
|
|
|
|
|
*/
|
|
|
|
|
public function test_current_race_running_total_pool_is_not_double_counted(): void
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'running',
|
|
|
|
|
'bet_opens_at' => now()->subMinutes(2),
|
|
|
|
|
'bet_closes_at' => now()->subMinute(),
|
|
|
|
|
'race_starts_at' => now()->subSeconds(30),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 2,
|
|
|
|
|
'total_pool' => 10000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::create([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => User::factory()->create()->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 5000,
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::create([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => User::factory()->create()->id,
|
|
|
|
|
'horse_id' => 2,
|
|
|
|
|
'amount' => 5000,
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->getJson(route('horse-race.current'));
|
|
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
|
$this->assertSame(10000, $response->json('race.total_pool'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 方法功能:验证用户可以成功下注。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
public function test_can_bet()
|
|
|
|
|
{
|
|
|
|
|
Event::fake();
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create(['jjb' => 500]);
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'betting',
|
|
|
|
|
'bet_opens_at' => now(),
|
|
|
|
|
'bet_closes_at' => now()->addMinutes(1),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 0,
|
|
|
|
|
'total_pool' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->postJson(route('horse-race.bet'), [
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 100,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJson(['ok' => true]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $user->fresh()->jjb);
|
|
|
|
|
$this->assertDatabaseHas('horse_bets', [
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 100,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证超出配置范围的下注会被拦截。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
public function test_cannot_bet_out_of_range()
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create(['jjb' => 500]);
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'betting',
|
|
|
|
|
'bet_opens_at' => now(),
|
|
|
|
|
'bet_closes_at' => now()->addMinutes(1),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 0,
|
|
|
|
|
'total_pool' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->postJson(route('horse-race.bet'), [
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 50, // Less than min_bet
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJson(['ok' => false]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证同一用户同一场只能下注一次。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
public function test_cannot_bet_twice_in_same_race()
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create(['jjb' => 500]);
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'betting',
|
|
|
|
|
'bet_opens_at' => now(),
|
|
|
|
|
'bet_closes_at' => now()->addMinutes(1),
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 0,
|
|
|
|
|
'total_pool' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::forceCreate([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 100,
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->postJson(route('horse-race.bet'), [
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'horse_id' => 2,
|
|
|
|
|
'amount' => 100,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJson(['ok' => false]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证可读取最近的赛马历史记录。
|
|
|
|
|
*/
|
2026-04-03 13:55:36 +08:00
|
|
|
public function test_can_get_history()
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
|
|
HorseRace::create([
|
|
|
|
|
'status' => 'settled',
|
|
|
|
|
'bet_opens_at' => now()->subMinutes(2),
|
|
|
|
|
'bet_closes_at' => now()->subMinutes(1),
|
|
|
|
|
'settled_at' => now(),
|
|
|
|
|
'winner_horse_id' => 1,
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_bets' => 1,
|
|
|
|
|
'total_pool' => 100,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->getJson(route('horse-race.history'));
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJsonStructure(['history']);
|
|
|
|
|
$this->assertCount(1, $response->json('history'));
|
|
|
|
|
}
|
2026-04-11 16:11:00 +08:00
|
|
|
|
2026-04-12 11:09:15 +08:00
|
|
|
/**
|
|
|
|
|
* 方法功能:验证单个赢家至少拿回本金,并可获得种子池补贴。
|
|
|
|
|
*/
|
2026-04-11 16:11:00 +08:00
|
|
|
public function test_single_winner_receives_seed_pool_and_does_not_lose_principal(): void
|
|
|
|
|
{
|
|
|
|
|
Event::fake();
|
|
|
|
|
Queue::fake();
|
|
|
|
|
|
|
|
|
|
GameConfig::updateOrCreate(
|
|
|
|
|
['game_key' => 'horse_racing'],
|
|
|
|
|
[
|
|
|
|
|
'name' => 'Horse Racing',
|
|
|
|
|
'icon' => 'horse',
|
|
|
|
|
'description' => 'Horse Racing Game',
|
|
|
|
|
'enabled' => true,
|
|
|
|
|
'params' => [
|
|
|
|
|
'min_bet' => 100,
|
|
|
|
|
'max_bet' => 100000,
|
|
|
|
|
'house_take_percent' => 5,
|
|
|
|
|
'seed_pool' => 10,
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
|
$user = User::factory()->create(['jjb' => 500]);
|
|
|
|
|
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'running',
|
|
|
|
|
'bet_opens_at' => now()->subMinutes(2),
|
|
|
|
|
'bet_closes_at' => now()->subMinute(),
|
|
|
|
|
'race_starts_at' => now()->subSeconds(30),
|
|
|
|
|
'winner_horse_id' => 1,
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::create([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 100,
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$user->decrement('jjb', 100);
|
|
|
|
|
|
|
|
|
|
$chatState = $this->createMock(ChatStateService::class);
|
|
|
|
|
$chatState->method('nextMessageId')->willReturn(1);
|
|
|
|
|
$chatState->method('pushMessage');
|
|
|
|
|
|
|
|
|
|
app()->instance(ChatStateService::class, $chatState);
|
|
|
|
|
|
|
|
|
|
(new CloseHorseRaceJob($race))->handle(app('App\Services\UserCurrencyService'), $chatState);
|
|
|
|
|
|
|
|
|
|
$bet = HorseBet::query()->first();
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull($bet);
|
|
|
|
|
$this->assertSame('won', $bet->status);
|
|
|
|
|
$this->assertSame(105, $bet->payout);
|
|
|
|
|
$this->assertSame(505, $user->fresh()->jjb);
|
|
|
|
|
}
|
2026-04-12 11:09:15 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 方法功能:验证结算广播会携带前端展示奖金所需的奖池参数。
|
|
|
|
|
*/
|
|
|
|
|
public function test_settled_broadcast_contains_pool_data_for_payout_display(): void
|
|
|
|
|
{
|
|
|
|
|
$race = HorseRace::create([
|
|
|
|
|
'status' => 'settled',
|
|
|
|
|
'bet_opens_at' => now()->subMinutes(2),
|
|
|
|
|
'bet_closes_at' => now()->subMinute(),
|
|
|
|
|
'race_starts_at' => now()->subSeconds(30),
|
|
|
|
|
'race_ends_at' => now()->subSecond(),
|
|
|
|
|
'settled_at' => now(),
|
|
|
|
|
'winner_horse_id' => 1,
|
|
|
|
|
'horses' => [
|
|
|
|
|
['id' => 1, 'name' => 'Horse A', 'emoji' => '🐎'],
|
|
|
|
|
['id' => 2, 'name' => 'Horse B', 'emoji' => '🏇'],
|
|
|
|
|
],
|
|
|
|
|
'total_pool' => 10000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::create([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => User::factory()->create()->id,
|
|
|
|
|
'horse_id' => 1,
|
|
|
|
|
'amount' => 5000,
|
|
|
|
|
'status' => 'won',
|
|
|
|
|
'payout' => 9500,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
HorseBet::create([
|
|
|
|
|
'race_id' => $race->id,
|
|
|
|
|
'user_id' => User::factory()->create()->id,
|
|
|
|
|
'horse_id' => 2,
|
|
|
|
|
'amount' => 5000,
|
|
|
|
|
'status' => 'lost',
|
|
|
|
|
'payout' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$payload = (new HorseRaceSettled($race))->broadcastWith();
|
|
|
|
|
|
|
|
|
|
$this->assertSame(10000, $payload['total_pool']);
|
|
|
|
|
$this->assertSame(5000, $payload['winner_pool']);
|
|
|
|
|
$this->assertSame(9500, $payload['distributable_pool']);
|
|
|
|
|
}
|
2026-04-03 13:55:36 +08:00
|
|
|
}
|