slot-booking/tests/Feature/CancelHoldTest.php
Dmitriy 16f3f851b1 feat: slot booking API with hot cache and oversell protection
GET /slots/availability, POST /slots/{id}/hold, POST /holds/{id}/confirm
and DELETE /holds/{id} on Laravel 12 and MySQL 8.

- availability cached for 10s behind a single-flight lock with a stale
  copy, invalidated after commit on confirm and cancel
- oversell prevented by a conditional decrement, verified by the number
  of affected rows rather than a preceding select
- idempotency enforced by a unique index on holds.idempotency_key
- active holds counted with an expires_at filter, so no background
  cleanup is required for correctness
- 27 feature tests against MySQL, service documentation, Postman collection

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 14:47:18 +03:00

75 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Enums\HoldStatus;
use App\Models\Hold;
use App\Models\Slot;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
final class CancelHoldTest extends TestCase
{
use RefreshDatabase;
public function test_cancelling_a_held_hold_releases_it_without_touching_remaining(): void
{
$slot = Slot::factory()->withCapacity(1)->create();
$hold = Hold::factory()->for($slot)->create();
$this->deleteJson("/holds/{$hold->id}")
->assertOk()
->assertJsonPath('status', HoldStatus::Cancelled->value)
->assertJsonPath('cancelled_at', fn (?string $value): bool => $value !== null);
// Место не списывалось, remaining меняться не должен.
$this->assertSame(1, $slot->refresh()->remaining);
// Освободившееся место снова можно забронировать.
$this->getJson('/slots/availability')->assertJsonPath('0.remaining', 1);
$this->postJson("/slots/{$slot->id}/hold", [], ['Idempotency-Key' => (string) Str::uuid()])
->assertCreated();
}
public function test_cancelling_a_confirmed_hold_gives_the_seat_back(): void
{
$slot = Slot::factory()->withCapacity(1)->create();
$holdId = $this->postJson("/slots/{$slot->id}/hold", [], ['Idempotency-Key' => (string) Str::uuid()])
->assertCreated()
->json('hold_id');
$this->postJson("/holds/{$holdId}/confirm")->assertOk();
$this->assertSame(0, $slot->refresh()->remaining);
$this->getJson('/slots/availability')->assertJsonPath('0.remaining', 0);
$this->deleteJson("/holds/{$holdId}")->assertOk();
$this->assertSame(1, $slot->refresh()->remaining);
$this->getJson('/slots/availability')->assertJsonPath('0.remaining', 1);
}
public function test_cancelling_twice_is_idempotent(): void
{
$slot = Slot::factory()->withCapacity(2)->create();
$holdId = $this->postJson("/slots/{$slot->id}/hold", [], ['Idempotency-Key' => (string) Str::uuid()])
->assertCreated()
->json('hold_id');
$this->postJson("/holds/{$holdId}/confirm")->assertOk();
$this->assertSame(1, $slot->refresh()->remaining);
$this->deleteJson("/holds/{$holdId}")->assertOk();
$this->deleteJson("/holds/{$holdId}")->assertOk();
// Место возвращается ровно один раз.
$this->assertSame(2, $slot->refresh()->remaining);
}
public function test_it_returns_404_for_an_unknown_hold(): void
{
$this->deleteJson('/holds/999')->assertNotFound();
}
}