Merge branch 'feature/event_scoped_admin' into homo
This commit is contained in:
206
tests/Feature/Migrations/GeneralizeTicketReservationsTest.php
Normal file
206
tests/Feature/Migrations/GeneralizeTicketReservationsTest.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Migrations;
|
||||
|
||||
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
|
||||
use App\Domains\Ticketing\Ticket\Models\ReservationBatch;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/** Tests the upgrade on the guarded SQLite :memory: connection. */
|
||||
class GeneralizeTicketReservationsTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Schema::create('tenants', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('codigo');
|
||||
});
|
||||
Schema::create('users', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->softDeletes();
|
||||
});
|
||||
Schema::create('catalog_items', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('tenant_code');
|
||||
$table->softDeletes();
|
||||
});
|
||||
Schema::create('variantes', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('catalog_item_id');
|
||||
$table->softDeletes();
|
||||
});
|
||||
Schema::create('tickets', fn (Blueprint $table) => $table->id());
|
||||
Schema::create('inventories', fn (Blueprint $table) => $table->id());
|
||||
(require database_path('migrations/2026_09_23_010000_create_desfile_entry_reservations_table.php'))->up();
|
||||
(require database_path('migrations/2026_09_24_000000_add_administrative_entry_reservation_stock.php'))->up();
|
||||
(require database_path('migrations/2026_09_24_010000_add_soft_deletes_to_desfile_entry_reservations_table.php'))->up();
|
||||
DB::table('users')->insert(['id' => 1]);
|
||||
DB::table('tenants')->insert([
|
||||
['codigo' => 'desfile_pura_tendencia'], ['codigo' => 'other'],
|
||||
]);
|
||||
DB::table('catalog_items')->insert([
|
||||
['id' => 1, 'tenant_code' => 'desfile_pura_tendencia'],
|
||||
['id' => 2, 'tenant_code' => 'other'],
|
||||
]);
|
||||
DB::table('variantes')->insert([
|
||||
['id' => 1, 'catalog_item_id' => 1, 'deleted_at' => null],
|
||||
['id' => 2, 'catalog_item_id' => 2, 'deleted_at' => now()],
|
||||
]);
|
||||
DB::table('tickets')->insert(['id' => 10]);
|
||||
DB::table('inventories')->insert(['id' => 20, 'entry_reserved_stock' => 1]);
|
||||
DB::table('desfile_reservation_batches')->insert([
|
||||
'id' => 30, 'user_id' => 1, 'tenant_code' => 'desfile_pura_tendencia',
|
||||
'idempotency_key' => 'aab7c678-7f4d-486a-87ec-7f613ad6f58d',
|
||||
'request_hash' => str_repeat('a', 64),
|
||||
'created_at' => now(), 'updated_at' => now(),
|
||||
]);
|
||||
DB::table('desfile_entry_reservations')->insert([
|
||||
['id' => 40, 'variant_id' => 1, 'ticket_id' => 10, 'inventory_id' => 20,
|
||||
'batch_id' => 30, 'fecha_reserva' => now(), 'importe' => 250,
|
||||
'tipo_pago' => 'otro_metodo', 'deleted_at' => null],
|
||||
['id' => 41, 'variant_id' => 2, 'ticket_id' => null, 'inventory_id' => null,
|
||||
'batch_id' => null, 'fecha_reserva' => now(), 'importe' => 0,
|
||||
'tipo_pago' => 'sin_cargo', 'deleted_at' => now()],
|
||||
]);
|
||||
}
|
||||
|
||||
private function migration(): Migration
|
||||
{
|
||||
return require database_path('migrations/2026_10_02_000000_generalize_ticket_reservations.php');
|
||||
}
|
||||
|
||||
public function test_upgrade_preserves_reservations_batches_stock_and_deleted_history(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$this->assertFalse(Schema::hasTable('desfile_entry_reservations'));
|
||||
$this->assertFalse(Schema::hasTable('desfile_reservation_batches'));
|
||||
$this->assertDatabaseCount('entry_reservations', 2);
|
||||
$this->assertDatabaseCount('reservation_batches', 1);
|
||||
$reservation = EntryReservation::findOrFail(40);
|
||||
$this->assertSame(1, $reservation->catalog_item_id);
|
||||
$this->assertSame(1, $reservation->catalogItem->id);
|
||||
$this->assertSame(30, $reservation->batch->id);
|
||||
$this->assertSame(40, $reservation->batch->reservations->sole()->id);
|
||||
$this->assertSame(10, $reservation->ticket->id);
|
||||
$this->assertSame('250.00', $reservation->importe);
|
||||
$this->assertSame(str_repeat('a', 64), $reservation->batch->request_hash);
|
||||
$this->assertSame(1, $reservation->inventory->entry_reserved_stock);
|
||||
$historical = EntryReservation::withTrashed()->findOrFail(41);
|
||||
$this->assertTrue($historical->trashed());
|
||||
$this->assertTrue($historical->variant->trashed());
|
||||
$this->assertSame(2, $historical->catalog_item_id);
|
||||
}
|
||||
|
||||
public function test_reservations_can_belong_to_other_tenants_and_have_no_variant(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$batch = ReservationBatch::create([
|
||||
'user_id' => 1, 'tenant_code' => 'other',
|
||||
'idempotency_key' => '9495eb8e-a424-4f1b-8911-e05c51e1caf7',
|
||||
'request_hash' => str_repeat('b', 64),
|
||||
]);
|
||||
$reservation = $batch->reservations()->create([
|
||||
'catalog_item_id' => 2, 'fecha_reserva' => now(),
|
||||
'tipo_pago' => 'sin_cargo', 'importe' => 0,
|
||||
]);
|
||||
$this->assertNull($reservation->variant_id);
|
||||
$this->assertSame('other', $reservation->catalogItem->tenant_code);
|
||||
$this->assertSame('other', $reservation->batch->tenant->codigo);
|
||||
$this->assertSame(1, $reservation->batch->user->id);
|
||||
}
|
||||
|
||||
public function test_variant_based_callers_infer_the_catalog_item(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$reservation = EntryReservation::create([
|
||||
'variant_id' => 1, 'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
|
||||
]);
|
||||
$this->assertSame(1, $reservation->fresh()->catalog_item_id);
|
||||
}
|
||||
|
||||
public function test_a_variant_from_another_catalog_item_is_rejected(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$this->expectException(ValidationException::class);
|
||||
EntryReservation::create([
|
||||
'catalog_item_id' => 2, 'variant_id' => 1,
|
||||
'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_the_catalog_item_is_required_at_the_database_level(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$this->expectException(QueryException::class);
|
||||
DB::table('entry_reservations')->insert([
|
||||
'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_upgrade_preserves_foreign_keys_and_batch_idempotency(): void
|
||||
{
|
||||
$this->migration()->up();
|
||||
$this->assertSame([], DB::select('PRAGMA foreign_key_check'));
|
||||
foreach (['catalog_items' => 1, 'variantes' => 1, 'tickets' => 10, 'inventories' => 20, 'reservation_batches' => 30] as $table => $id) {
|
||||
try {
|
||||
DB::table($table)->where('id', $id)->delete();
|
||||
$this->fail("Expected {$table} to remain referenced");
|
||||
} catch (QueryException) {
|
||||
$this->assertDatabaseHas($table, ['id' => $id]);
|
||||
}
|
||||
}
|
||||
$batch = ReservationBatch::findOrFail(30);
|
||||
$this->expectException(QueryException::class);
|
||||
ReservationBatch::create($batch->only(['user_id', 'tenant_code', 'idempotency_key', 'request_hash']));
|
||||
}
|
||||
|
||||
public function test_rollback_restores_the_original_schema_and_keeps_data(): void
|
||||
{
|
||||
$migration = $this->migration();
|
||||
$migration->up();
|
||||
$migration->down();
|
||||
$this->assertFalse(Schema::hasTable('entry_reservations'));
|
||||
$this->assertFalse(Schema::hasTable('reservation_batches'));
|
||||
$this->assertDatabaseCount('desfile_entry_reservations', 2);
|
||||
$this->assertDatabaseCount('desfile_reservation_batches', 1);
|
||||
$this->assertFalse(Schema::hasColumn('desfile_entry_reservations', 'catalog_item_id'));
|
||||
$this->assertSame([], DB::select('PRAGMA foreign_key_check'));
|
||||
$migration->up();
|
||||
$this->assertSame(1, EntryReservation::findOrFail(40)->catalog_item_id);
|
||||
}
|
||||
|
||||
public function test_rollback_does_not_discard_reservations_without_variants(): void
|
||||
{
|
||||
$migration = $this->migration();
|
||||
$migration->up();
|
||||
EntryReservation::create([
|
||||
'catalog_item_id' => 2, 'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
|
||||
]);
|
||||
try {
|
||||
$migration->down();
|
||||
$this->fail('Expected incompatible rollback to be rejected');
|
||||
} catch (\LogicException) {
|
||||
$this->assertDatabaseCount('entry_reservations', 3);
|
||||
$this->assertTrue(Schema::hasTable('reservation_batches'));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_an_orphaned_catalog_is_rejected_before_renaming_tables(): void
|
||||
{
|
||||
DB::table('variantes')->where('id', 2)->update(['catalog_item_id' => 999]);
|
||||
try {
|
||||
$this->migration()->up();
|
||||
$this->fail('Expected orphaned catalog to be rejected');
|
||||
} catch (\LogicException) {
|
||||
$this->assertDatabaseCount('desfile_entry_reservations', 2);
|
||||
$this->assertFalse(Schema::hasTable('entry_reservations'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user