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

@@ -4,7 +4,6 @@ namespace Tests\Unit\Catalog;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Enums\CatalogItemType;
use App\Domains\Catalog\Enums\EventProductType;
use App\Domains\Catalog\Enums\FeaturedGroupSource;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\InventoryPolicy;
@@ -21,13 +20,11 @@ use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\ItemAttribute;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Catalog\Models\VariantDefinition;
use App\Domains\Event\Models\Event;
use App\Domains\Event\Models\EventDate;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Models\Ticket;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class CatalogModelsTest extends TestCase
@@ -59,6 +56,18 @@ class CatalogModelsTest extends TestCase
$this->assertInstanceOf(AttributeOption::class, $attribute->options()->getRelated());
}
public function test_event_date_is_an_attribute_type_with_dynamic_options(): void
{
$attribute = new Attribute;
$attribute->type = FieldType::EventDate->value;
$this->assertSame(FieldType::EventDate, $attribute->type);
$this->assertTrue($attribute->type->supportsOptions());
$this->assertTrue($attribute->type->usesDynamicOptions());
$this->assertContains('event_date', FieldType::values());
$this->assertInstanceOf(EventDate::class, $attribute->eventDates()->getRelated());
}
public function test_catalog_item_is_the_catalog_root(): void
{
$item = new CatalogItem;
@@ -66,8 +75,6 @@ class CatalogModelsTest extends TestCase
'category_id' => '10',
'brand_id' => '20',
'inventory_id' => '30',
'event_id' => '40',
'event_product_type' => EventProductType::Entry->value,
'type' => CatalogItemType::Standard->value,
'precio' => '12.50',
'inventory_policy' => InventoryPolicy::Tracked->value,
@@ -79,14 +86,11 @@ class CatalogModelsTest extends TestCase
$this->assertSame(10, $item->category_id);
$this->assertSame(20, $item->brand_id);
$this->assertSame(30, $item->inventory_id);
$this->assertSame(40, $item->event_id);
$this->assertSame(EventProductType::Entry, $item->event_product_type);
$this->assertSame(CatalogItemType::Standard, $item->type);
$this->assertSame('12.50', $item->precio);
$this->assertSame(InventoryPolicy::Tracked, $item->inventory_policy);
$this->assertTrue($item->has_tickets);
$this->assertInstanceOf(Tenant::class, $item->tenant()->getRelated());
$this->assertInstanceOf(Event::class, $item->event()->getRelated());
$this->assertInstanceOf(Category::class, $item->category()->getRelated());
$this->assertInstanceOf(Brand::class, $item->brand()->getRelated());
$this->assertInstanceOf(Inventory::class, $item->inventory()->getRelated());
@@ -160,24 +164,6 @@ class CatalogModelsTest extends TestCase
$this->assertSame('catalog_items_attachments', $variant->attachments()->getTable());
}
public function test_variant_use_dates_override_or_inherit_catalog_item_dates(): void
{
$item = new CatalogItem;
$item->minimum_use_date = Carbon::parse('2026-08-01 09:00:00');
$item->maximum_use_date = Carbon::parse('2026-08-31 18:00:00');
$variant = new Variant;
$variant->maximum_use_date = Carbon::parse('2026-08-15 18:00:00');
$variant->setRelation('catalogItem', $item);
$this->assertTrue(
$variant->getMinimumUseDate()->equalTo($item->minimum_use_date),
);
$this->assertTrue(
$variant->getMaximumUseDate()->equalTo($variant->maximum_use_date),
);
}
public function test_event_date_identifies_a_variant_without_catalog_attributes(): void
{
$item = new CatalogItem;
@@ -189,15 +175,86 @@ class CatalogModelsTest extends TestCase
$eventDate->time_end = '18:00:00';
$variant = new Variant;
$variant->minimum_use_date = Carbon::parse('2026-10-09 08:00:00');
$variant->maximum_use_date = Carbon::parse('2026-10-09 20:00:00');
$variant->setRelation('catalogItem', $item);
$variant->setRelation('eventDate', $eventDate);
$variant->setRelation('definitions', new EloquentCollection);
$this->assertSame('Entrada General', $variant->getName());
$this->assertSame('2026-10-09 09:00:00', $variant->getMinimumUseDate()->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-09 18:00:00', $variant->getMaximumUseDate()->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-09', $variant->eventDate->date->format('Y-m-d'));
}
public function test_variant_exposes_selection_values_with_api_labels(): void
{
$attribute = new Attribute;
$attribute->codigo = 'size';
$attribute->setRelation('options', new EloquentCollection([
new AttributeOption(['value' => 'M', 'label' => 'Medium']),
]));
$itemAttribute = new ItemAttribute;
$itemAttribute->allow_multi_select = false;
$itemAttribute->setRelation('attribute', $attribute);
$definition = new VariantDefinition(['value' => 'M']);
$definition->item_attribute_id = 1;
$definition->setRelation('itemAttribute', $itemAttribute);
$eventDate = new EventDate([
'date' => '2026-10-09',
'time_start' => '09:00:00',
'time_end' => '18:00:00',
]);
$eventDate->id = 20;
$variant = new Variant;
$variant->setRelation('definitions', new EloquentCollection([$definition]));
$variant->setRelation('eventDates', new EloquentCollection([$eventDate]));
$this->assertSame(
['value' => 'M', 'label' => 'Medium'],
$variant->selectionOptions()->get('size'),
);
$this->assertSame(
['value' => '20', 'label' => '09/10/2026'],
$variant->selectionOptions()->get('event_date'),
);
}
public function test_variant_orders_selection_options_by_item_order_and_attribute_label(): void
{
$definitions = collect([
['id' => 1, 'code' => 'zeta', 'label' => 'Zeta', 'sort_order' => 2],
['id' => 2, 'code' => 'priority', 'label' => 'Priority', 'sort_order' => 1],
['id' => 3, 'code' => 'alpha', 'label' => 'Alpha', 'sort_order' => 2],
])->map(function (array $data): VariantDefinition {
$attribute = new Attribute([
'codigo' => $data['code'],
'nombre' => $data['label'],
]);
$attribute->setRelation('options', new EloquentCollection);
$itemAttribute = new ItemAttribute([
'sort_order' => $data['sort_order'],
'allow_multi_select' => false,
]);
$itemAttribute->id = $data['id'];
$itemAttribute->setRelation('attribute', $attribute);
$definition = new VariantDefinition(['value' => $data['code']]);
$definition->item_attribute_id = $itemAttribute->id;
$definition->setRelation('itemAttribute', $itemAttribute);
return $definition;
});
$variant = new Variant;
$variant->setRelation('definitions', new EloquentCollection($definitions));
$variant->setRelation('eventDates', new EloquentCollection);
$this->assertSame(
['priority', 'alpha', 'zeta'],
$variant->selectionOptions()->keys()->all(),
);
}
public function test_inventory_maps_stock_without_a_polymorphic_owner(): void
@@ -226,6 +283,27 @@ class CatalogModelsTest extends TestCase
$this->assertTrue($item->isAvailable());
}
public function test_catalog_item_only_exposes_available_tracked_variants(): void
{
$unavailable = (new Variant)->setRelation('inventory', $this->trackedInventory(3, 3));
$available = (new Variant)->setRelation('inventory', $this->trackedInventory(5, 2));
$unavailable->id = 10;
$available->id = 20;
$item = new CatalogItem;
$item->inventory_policy = InventoryPolicy::Tracked;
$item->setRelation('variants', new EloquentCollection([$unavailable, $available]));
$this->assertSame([$available], $item->visibleVariants()->all());
$this->assertSame(
[$unavailable, $available],
$item->visibleVariants($unavailable->id)->all(),
);
$item->inventory_policy = InventoryPolicy::Unlimited;
$this->assertSame([$unavailable, $available], $item->visibleVariants()->all());
}
public function test_catalog_item_prioritizes_its_inventory_over_variants(): void
{
$item = new CatalogItem;

View File

@@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Event;
use App\Domains\Event\Services\EventDateTextFormatter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class EventDateTextFormatterTest extends TestCase
{
/** @param array<int, string> $dates */
#[DataProvider('dateCases')]
public function test_it_formats_event_dates_in_spanish(array $dates, ?string $expected): void
{
$this->assertSame($expected, (new EventDateTextFormatter)->format($dates));
}
/** @return array<string, array{array<int, string>, string|null}> */
public static function dateCases(): array
{
return [
'no dates' => [[], null],
'one date' => [['2026-10-09'], '9 de Octubre 2026'],
'same month sorted' => [
['2026-10-12', '2026-10-09', '2026-10-11', '2026-10-10'],
'9, 10, 11 y 12 de Octubre 2026',
],
'different months' => [
['2026-11-01', '2026-10-31'],
'31 de Octubre y 1 de Noviembre 2026',
],
'different years' => [
['2027-01-01', '2026-12-31'],
'31 de Diciembre 2026 y 1 de Enero 2027',
],
];
}
}

View File

@@ -2,48 +2,40 @@
namespace Tests\Unit\Event;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Models\Event;
use App\Domains\Event\Models\EventDate;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Models\ValidityTime;
use Tests\TestCase;
class EventModelsTest extends TestCase
{
public function test_event_maps_its_tenant_dates_and_catalog_items(): void
{
$event = new Event;
$this->assertFalse($event->usesTimestamps());
$this->assertInstanceOf(Tenant::class, $event->tenant()->getRelated());
$this->assertInstanceOf(EventDate::class, $event->dates()->getRelated());
$this->assertInstanceOf(CatalogItem::class, $event->catalogItems()->getRelated());
}
public function test_event_date_maps_schedule_and_variants(): void
{
$eventDate = new EventDate;
$eventDate->setRawAttributes([
'event_id' => '10',
'tenant_code' => 'acme',
'validity_time_id' => '12',
'date' => '2026-10-09',
'time_start' => '09:00:00',
'time_end' => '18:30:00',
]);
$this->assertFalse($eventDate->usesTimestamps());
$this->assertSame(10, $eventDate->event_id);
$this->assertSame('acme', $eventDate->tenant_code);
$this->assertSame(12, $eventDate->validity_time_id);
$this->assertSame('2026-10-09 09:00:00', $eventDate->startsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-09 18:30:00', $eventDate->endsAt()->format('Y-m-d H:i:s'));
$this->assertInstanceOf(Event::class, $eventDate->event()->getRelated());
$this->assertInstanceOf(Tenant::class, $eventDate->tenant()->getRelated());
$this->assertInstanceOf(ValidityTime::class, $eventDate->validityTime()->getRelated());
$this->assertInstanceOf(EventDate::class, (new ValidityTime)->eventDate()->getRelated());
$this->assertInstanceOf(Variant::class, $eventDate->variants()->getRelated());
}
public function test_tenant_has_many_events_and_one_active_event(): void
public function test_tenant_has_many_event_dates(): void
{
$tenant = new Tenant;
$this->assertInstanceOf(Event::class, $tenant->events()->getRelated());
$this->assertInstanceOf(Event::class, $tenant->activeEvent()->getRelated());
$this->assertInstanceOf(EventDate::class, $tenant->eventDates()->getRelated());
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit\Sale;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\PurchaseItem;
use App\Domains\Sale\Resources\AdminApp\SaleDetailResource;
use Illuminate\Http\Request;
use Tests\TestCase;
class SaleDetailResourceTest extends TestCase
{
public function test_it_serializes_the_purchased_item_snapshot(): void
{
$purchase = (new Purchase)->forceFill([
'id' => 15,
'total' => '30000.00',
]);
$item = (new PurchaseItem)->forceFill([
'id' => 8,
'item_nombre' => 'Comida',
'variant_attributes' => [
['name' => 'Fecha', 'value' => ['2026-10-12']],
],
'cantidad' => 3,
'precio_unitario' => '10000.00',
'total' => '30000.00',
]);
$purchase->setRelation('items', collect([$item]));
$data = (new SaleDetailResource($purchase))->resolve(Request::create('/'));
$this->assertSame(15, $data['id']);
$this->assertSame('Comida', $data['items'][0]['product']);
$this->assertSame(['2026-10-12'], $data['items'][0]['event_dates']);
$this->assertSame(3, $data['items'][0]['quantity']);
$this->assertSame('10000.00', $data['items'][0]['unit_price']);
$this->assertSame('30000.00', $data['items'][0]['total']);
$this->assertSame('30000.00', $data['total']);
}
}

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'),
));
}
}