Files
chatroom/tests/Feature/BaccaratControllerTest.php

201 lines
5.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\BaccaratBet;
use App\Models\BaccaratRound;
use App\Models\GameConfig;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
class BaccaratControllerTest 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,
],
]
);
}
public function test_can_get_current_round()
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
$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)->getJson(route('baccarat.current'));
$response->assertStatus(200);
$response->assertJsonStructure(['round' => ['id', 'status', 'bet_closes_at']]);
$this->assertEquals($round->id, $response->json('round.id'));
}
public function test_can_bet()
{
Event::fake();
/** @var \App\Models\User $user */
$user = User::factory()->create(['jjb' => 200]);
$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->assertStatus(200);
$response->assertJson(['ok' => true]);
$this->assertEquals(100, $user->fresh()->jjb);
$this->assertDatabaseHas('baccarat_bets', [
'round_id' => $round->id,
'user_id' => $user->id,
'bet_type' => 'big',
'amount' => 100,
]);
Event::assertDispatched(\App\Events\BaccaratPoolUpdated::class);
}
public function test_cannot_bet_out_of_range()
{
/** @var \App\Models\User $user */
$user = User::factory()->create(['jjb' => 200]);
$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' => 50, // Less than min_bet
]);
$response->assertStatus(200);
$response->assertJson(['ok' => false]);
}
public function test_cannot_bet_twice_in_same_round()
{
/** @var \App\Models\User $user */
$user = User::factory()->create(['jjb' => 200]);
$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,
]);
BaccaratBet::forceCreate([
'round_id' => $round->id,
'user_id' => $user->id,
'bet_type' => 'big',
'amount' => 100,
'status' => 'pending',
]);
$response = $this->actingAs($user)->postJson(route('baccarat.bet'), [
'round_id' => $round->id,
'bet_type' => 'small',
'amount' => 100,
]);
$response->assertStatus(200);
$response->assertJson(['ok' => false]);
}
public function test_can_get_history()
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
BaccaratRound::forceCreate([
'status' => 'settled',
'bet_opens_at' => now()->subMinutes(2),
'bet_closes_at' => now()->subMinutes(1),
'settled_at' => now(),
'dice1' => 1,
'dice2' => 2,
'dice3' => 3,
'total_points' => 6,
'result' => 'small',
'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)->getJson(route('baccarat.history'));
$response->assertStatus(200);
$response->assertJsonStructure(['history']);
$this->assertCount(1, $response->json('history'));
}
}