Refactor ticket validity handling and improve tests

- Updated tests for Accommodation, Entry, Food, Merchandise, and Sale controllers to use soft deletes for variants and ensure proper inventory counts.
- Enhanced ticket generation logic to resolve validity from soft-deleted catalog sources.
- Introduced a new TicketValidityResolver service to manage ticket validity based on event dates and variant definitions.
- Removed unnecessary database assertions and improved the clarity of validity checks in tests.
- Added comprehensive tests for the new TicketValidityResolver service, ensuring correct handling of event dates and multi-select options.
- Cleaned up unused code and assertions in existing tests for better maintainability.
This commit is contained in:
2026-08-14 09:00:51 -03:00
parent bfdaf1c38b
commit 573d4fe5e6
59 changed files with 1185 additions and 679 deletions

View File

@@ -5,10 +5,11 @@ namespace Tests\Feature\Ticket;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Event\Models\EventDate;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\Ticket;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
@@ -23,35 +24,52 @@ class TicketControllerTest extends TestCase
$user = User::factory()->create();
$olderTicket = $this->createTicket($tenant, $user, 'Older ticket');
$newerTicket = $this->createTicket($tenant, $user, 'Newer ticket');
$validityTimes = collect([
ValidityTime::query()->create([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subHour(),
'fixed_expires_at' => now()->addHour(),
$catalogItem = CatalogItem::query()->create([
'tenant_code' => $tenant->codigo,
'slug' => 'dated-ticket',
'nombre' => 'Dated ticket',
'descripcion' => 'Dated ticket',
'precio' => 10,
'has_tickets' => true,
]);
$eventDates = collect([
EventDate::query()->create([
'tenant_code' => $tenant->codigo,
'date' => now()->format('Y-m-d'),
'time_start' => now()->subHour()->format('H:i:s'),
'time_end' => now()->addHour()->format('H:i:s'),
]),
ValidityTime::query()->create([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->addDay(),
'fixed_expires_at' => now()->addDays(2),
EventDate::query()->create([
'tenant_code' => $tenant->codigo,
'date' => now()->addDay()->format('Y-m-d'),
'time_start' => '00:00:00',
'time_end' => '23:59:59',
]),
]);
$validityTimes->each(function (ValidityTime $validityTime) use ($newerTicket): void {
$group = $newerTicket->validityGroups()->create();
$group->validityTimes()->attach($validityTime);
});
$variant = $catalogItem->variants()->create([
'inventory_id' => Inventory::query()->create()->id,
]);
$variant->eventDates()->sync($eventDates->pluck('id'));
$newerTicket->update([
'source_catalog_item_id' => $catalogItem->id,
'source_variant_id' => $variant->id,
]);
$expectedName = 'Dated ticket ('.$eventDates
->pluck('date')
->map(fn ($date): string => $date->format('d/m/Y'))
->implode(', ').')';
$this->actingAs($user, 'sanctum')
->getJson("/api/tenants/{$tenant->codigo}/tickets")
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $newerTicket->id)
->assertJsonPath('data.0.name', 'Newer ticket')
->assertJsonPath('data.0.name', $expectedName)
->assertJsonPath('data.0.is_valid', true)
->assertJsonPath('data.0.is_expired', false)
->assertJsonPath('data.0.is_used', false)
->assertJsonCount(2, 'data.0.validity_times')
->assertJsonCount(2, 'data.0.validity_groups')
->assertJsonCount(1, 'data.0.validity_groups.0.validity_times')
->assertJsonMissingPath('data.0.validity_times')
->assertJsonMissingPath('data.0.validity_groups')
->assertJsonMissingPath('data.0.validity_time')
->assertJsonPath('data.1.id', $olderTicket->id)
->assertJsonMissingPath('meta')
@@ -122,11 +140,19 @@ class TicketControllerTest extends TestCase
private function createTicket(Tenant $tenant, User $user, string $name): Ticket
{
$catalogItem = CatalogItem::query()->create([
'tenant_code' => $tenant->codigo,
'slug' => Str::slug($name).'-'.Str::lower(Str::random(8)),
'nombre' => $name,
'descripcion' => "Description for {$name}",
'precio' => 10,
'has_tickets' => true,
]);
return Ticket::query()->create([
'tenant_code' => $tenant->codigo,
'ticket' => (string) Str::uuid(),
'name' => $name,
'description' => "Description for {$name}",
'source_catalog_item_id' => $catalogItem->id,
'user_id' => $user->id,
]);
}