Add tests for ticket validity and event date formatting

- Create TicketValiditySchemaTest to verify database schema for ticket validity.
- Update CatalogModelsTest to include tests for event date attributes and selection options.
- Introduce EventDateTextFormatterTest for formatting event dates in Spanish.
- Refactor EventModelsTest to include validity time relationships.
- Add SaleDetailResourceTest to ensure correct serialization of purchase items.
- Enhance TicketTest with validity time checks and status management.
- Implement ValidityTimeResourceTest to validate resource output for different validity types.
- Add ValidityTimeTest to verify casting and validity checks for validity time types.
This commit is contained in:
2026-08-11 12:41:35 -03:00
parent b294e5c46e
commit 02cf3f3773
166 changed files with 10203 additions and 1849 deletions

View File

@@ -5,9 +5,12 @@ namespace Tests\Unit\Ticket;
use App\Domains\Auth\Models\User;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;
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\TicketValidityGroup;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Carbon;
use Tests\TestCase;
@@ -20,14 +23,12 @@ class TicketTest extends TestCase
parent::tearDown();
}
public function test_it_maps_its_dates_and_relations(): void
public function test_it_maps_its_fields_and_relations(): void
{
$ticket = new Ticket;
$ticket->setRawAttributes([
'source_catalog_item_id' => '20',
'source_variant_id' => '30',
'starts_at' => '2026-07-21 10:00:00',
'expires_at' => '2026-07-22 10:00:00',
'used_at' => null,
'scanner_user_id' => '15',
'user_id' => '10',
@@ -37,8 +38,6 @@ class TicketTest extends TestCase
$this->assertFalse($ticket->usesTimestamps());
$this->assertSame(20, $ticket->source_catalog_item_id);
$this->assertSame(30, $ticket->source_variant_id);
$this->assertInstanceOf(Carbon::class, $ticket->starts_at);
$this->assertInstanceOf(Carbon::class, $ticket->expires_at);
$this->assertNull($ticket->used_at);
$this->assertSame(15, $ticket->scanner_user_id);
$this->assertSame(10, $ticket->user_id);
@@ -47,105 +46,158 @@ class TicketTest extends TestCase
$this->assertInstanceOf(User::class, $ticket->scannerUser()->getRelated());
$this->assertInstanceOf(CatalogItem::class, $ticket->sourceCatalogItem()->getRelated());
$this->assertInstanceOf(Variant::class, $ticket->sourceVariant()->getRelated());
$this->assertInstanceOf(TicketValidityGroup::class, $ticket->validityGroups()->getRelated());
$this->assertInstanceOf(ValidityTime::class, (new TicketValidityGroup)->validityTimes()->getRelated());
}
public function test_unused_ticket_without_date_restrictions_is_valid(): void
public function test_unused_ticket_without_validity_time_is_valid(): void
{
$this->assertTrue((new Ticket)->isValid());
$ticket = new Ticket;
$this->assertTrue($ticket->isValid());
$this->assertSame(Ticket::STATUS_ACTIVE, $ticket->status);
}
public function test_ticket_is_invalid_before_its_start_date(): void
public function test_fixed_window_controls_ticket_validity(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket(['starts_at' => now()->addSecond()]);
$ticket = $this->ticketWithValidityTime(new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subHour(),
'fixed_expires_at' => now()->addHour(),
]));
$this->assertFalse($ticket->isValid());
$this->assertTrue($ticket->isValid());
$this->assertFalse($ticket->is_expired);
}
public function test_ticket_is_valid_when_its_start_date_is_reached(): void
public function test_time_window_is_resolved_for_current_date(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket(['starts_at' => now()]);
Carbon::setTestNow('2026-07-21 12:00:00');
$ticket = $this->ticketWithValidityGroups([[new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '10:00:00',
'end_time' => '14:00:00',
])]]);
$this->assertSame('2026-07-21 10:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-07-21 14:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
$this->assertTrue($ticket->isValid());
}
public function test_ticket_is_invalid_when_it_expires(): void
public function test_ticket_is_invalid_when_validity_time_expires(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket(['expires_at' => now()]);
$ticket = $this->ticketWithValidityTime(new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_expires_at' => now(),
]));
$this->assertFalse($ticket->isValid());
}
public function test_used_ticket_is_invalid(): void
{
$ticket = new Ticket(['used_at' => now()->subSecond()]);
$this->assertFalse($ticket->isValid());
}
public function test_it_appends_computed_status_fields(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket([
'starts_at' => now()->subHour(),
'expires_at' => now()->addHour(),
]);
$attributes = $ticket->toArray();
$this->assertTrue($attributes['is_valid']);
$this->assertFalse($attributes['is_expired']);
$this->assertFalse($attributes['is_used']);
}
public function test_unused_ticket_is_expired_when_its_expiration_date_is_reached(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket(['expires_at' => now()]);
$this->assertFalse($ticket->is_valid);
$this->assertTrue($ticket->is_expired);
$this->assertFalse($ticket->is_used);
$this->assertSame(Ticket::STATUS_EXPIRED, $ticket->status);
}
public function test_used_ticket_is_not_reported_as_expired(): void
public function test_ticket_is_valid_when_any_validity_time_is_active(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = new Ticket([
'expires_at' => now()->subHour(),
'used_at' => now()->subDay(),
$ticket = $this->ticketWithValidityGroups([
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subDays(2),
'fixed_expires_at' => now()->subDay(),
])],
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subHour(),
'fixed_expires_at' => now()->addHour(),
])],
]);
$this->assertTrue($ticket->isValid());
$this->assertFalse($ticket->is_expired);
$this->assertSame('2026-07-19 10:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-07-21 11:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
}
public function test_ticket_is_invalid_but_not_expired_between_validity_times(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = $this->ticketWithValidityGroups([
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subDays(2),
'fixed_expires_at' => now()->subDay(),
])],
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->addDay(),
'fixed_expires_at' => now()->addDays(2),
])],
]);
$this->assertFalse($ticket->isValid());
$this->assertFalse($ticket->is_expired);
$this->assertSame(Ticket::STATUS_ACTIVE, $ticket->status);
}
public function test_used_ticket_is_invalid_and_not_reported_as_expired(): void
{
$ticket = $this->ticketWithValidityTime(new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_expires_at' => now()->subHour(),
]));
$ticket->used_at = now()->subMinute();
$this->assertFalse($ticket->is_valid);
$this->assertFalse($ticket->is_expired);
$this->assertTrue($ticket->is_used);
$this->assertSame(Ticket::STATUS_USED, $ticket->status);
}
public function test_event_date_has_priority_over_ticket_and_variant_dates(): void
public function test_all_validity_times_in_the_same_group_must_be_active(): void
{
Carbon::setTestNow('2026-10-09 19:00:00');
Carbon::setTestNow('2026-08-20 13:00:00');
$ticket = $this->ticketWithValidityGroups([[
new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => '2026-08-20 09:00:00',
'fixed_expires_at' => '2026-08-20 18:00:00',
]),
new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '12:00:00',
'end_time' => '15:00:00',
]),
]]);
$eventDate = new EventDate;
$eventDate->date = '2026-10-09';
$eventDate->time_start = '09:00:00';
$eventDate->time_end = '18:00:00';
$this->assertTrue($ticket->isValid());
$this->assertSame('2026-08-20 12:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-08-20 15:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
$variant = new Variant;
$variant->minimum_use_date = Carbon::parse('2026-10-09 08:00:00');
$variant->maximum_use_date = Carbon::parse('2026-10-09 22:00:00');
$variant->setRelation('eventDate', $eventDate);
Carbon::setTestNow('2026-08-20 16:00:00');
$ticket = new Ticket([
'starts_at' => Carbon::parse('2026-10-09 07:00:00'),
'expires_at' => Carbon::parse('2026-10-10 23:59:59'),
]);
$ticket->setRelation('sourceVariant', $variant);
$this->assertSame('2026-10-09 09:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-09 18:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
$this->assertFalse($ticket->isValid());
$this->assertTrue($ticket->is_expired);
}
private function ticketWithValidityTime(ValidityTime $validityTime): Ticket
{
return $this->ticketWithValidityGroups([[$validityTime]]);
}
/** @param array<int, array<int, ValidityTime>> $validityGroups */
private function ticketWithValidityGroups(array $validityGroups): Ticket
{
$ticket = new Ticket;
$groups = collect($validityGroups)->map(function (array $validityTimes): TicketValidityGroup {
$group = new TicketValidityGroup;
$group->setRelation('validityTimes', new EloquentCollection($validityTimes));
return $group;
});
$ticket->setRelation('validityGroups', new EloquentCollection($groups->all()));
return $ticket;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Tests\Unit\Ticket;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\ValidityTime;
use App\Domains\Ticket\Resources\ValidityTimeResource;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ValidityTimeResourceTest extends TestCase
{
protected function tearDown(): void
{
Carbon::setTestNow();
parent::tearDown();
}
public function test_time_window_only_contains_non_null_time_fields(): void
{
Carbon::setTestNow('2026-08-20 12:00:00');
$resource = ValidityTimeResource::make(new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '11:00:00',
'end_time' => null,
'fixed_starts_at' => '2026-08-20 10:00:00',
]))->resolve();
$this->assertSame('time_window', $resource['type']);
$this->assertTrue($resource['is_valid']);
$this->assertSame('11:00:00', $resource['start_time']);
$this->assertArrayNotHasKey('end_time', $resource);
$this->assertArrayNotHasKey('fixed_starts_at', $resource);
$this->assertArrayNotHasKey('fixed_expires_at', $resource);
}
public function test_fixed_window_only_contains_non_null_datetime_fields(): void
{
Carbon::setTestNow('2026-08-20 11:00:00');
$resource = ValidityTimeResource::make(new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'start_time' => '11:00:00',
'fixed_starts_at' => '2026-08-20 10:00:00',
'fixed_expires_at' => null,
]))->resolve();
$this->assertSame('fixed_window', $resource['type']);
$this->assertTrue($resource['is_valid']);
$this->assertArrayHasKey('fixed_starts_at', $resource);
$this->assertArrayNotHasKey('fixed_expires_at', $resource);
$this->assertArrayNotHasKey('start_time', $resource);
$this->assertArrayNotHasKey('end_time', $resource);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\Ticket;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\TicketValidityGroup;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ValidityTimeTest extends TestCase
{
public function test_it_casts_its_type_dates_and_flags(): void
{
$validityTime = new ValidityTime;
$validityTime->setRawAttributes([
'type' => ValidityTimeType::FixedWindow->value,
'fixed_starts_at' => '2026-08-20 10:00:00',
'fixed_expires_at' => '2026-08-21 02:00:00',
]);
$this->assertSame(ValidityTimeType::FixedWindow, $validityTime->type);
$this->assertInstanceOf(Carbon::class, $validityTime->fixed_starts_at);
$this->assertInstanceOf(Carbon::class, $validityTime->fixed_expires_at);
$this->assertInstanceOf(CatalogItem::class, $validityTime->catalogItems()->getRelated());
$this->assertInstanceOf(AttributeOption::class, $validityTime->attributeOptions()->getRelated());
$this->assertInstanceOf(
TicketValidityGroup::class,
$validityTime->ticketValidityGroups()->getRelated(),
);
}
public function test_it_exposes_supported_type_values(): void
{
$this->assertSame([
'time_window',
'fixed_window',
], ValidityTimeType::values());
}
public function test_fixed_window_is_valid_between_its_limits(): void
{
$validityTime = new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => '2026-08-20 10:00:00',
'fixed_expires_at' => '2026-08-20 12:00:00',
]);
$this->assertTrue($validityTime->isValid(at: Carbon::parse('2026-08-20 10:00:00')));
$this->assertTrue($validityTime->isValid(at: Carbon::parse('2026-08-20 11:00:00')));
$this->assertFalse($validityTime->isValid(at: Carbon::parse('2026-08-20 12:00:00')));
}
public function test_time_window_is_valid_between_its_limits(): void
{
$validityTime = new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '11:00:00',
'end_time' => '14:00:00',
]);
$this->assertTrue($validityTime->isValid(
Carbon::parse('2026-08-20 12:00:00'),
));
$this->assertFalse($validityTime->isValid(
Carbon::parse('2026-08-20 14:00:00'),
));
}
}