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

@@ -11,7 +11,6 @@ use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Models\Event;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Services\CheckoutService;
use App\Domains\Tenant\Models\Tenant;
@@ -34,12 +33,10 @@ class StorePurchaseTest extends TestCase
public function test_it_creates_an_independent_purchase_snapshot_from_cart(): void
{
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$event = Event::query()->create([
'tenant_code' => $tenant->codigo,
'name' => 'Sonder Fest',
'address' => 'Test address',
$tenant->update([
'event_title' => 'Sonder Fest',
'event_location' => 'Test address',
]);
$tenant->update(['active_event_id' => $event->id]);
$user = User::factory()->create([
'email' => 'buyer@example.com',
]);
@@ -92,7 +89,6 @@ class StorePurchaseTest extends TestCase
$response->assertJsonPath('data.nombre_apellido', null);
$response->assertJsonPath('data.email', null);
$response->assertJsonPath('data.tenant_codigo', 'sonder');
$response->assertJsonPath('data.event_id', $event->id);
$response->assertJsonPath('data.status', Purchase::STATUS_CREATED);
$response->assertJsonPath('data.items_source', 'purchase');
$response->assertJsonCount(1, 'data.items');
@@ -105,7 +101,6 @@ class StorePurchaseTest extends TestCase
'id' => $purchaseId,
'cart_id' => $cartId,
'tenant_codigo' => 'sonder',
'event_id' => $event->id,
'user_id' => $user->id,
'dni' => null,
'telefono' => null,
@@ -190,6 +185,98 @@ class StorePurchaseTest extends TestCase
]);
}
public function test_it_enforces_the_user_purchase_limit_and_releases_it_after_cancellation(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create();
$otherUser = User::factory()->create();
$variant = $this->createVariantForTenant('sonder', 20, '50.00');
$variant->catalogItem->update(['max_units_per_user' => 3]);
$firstPurchaseId = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [
'direct_item' => [
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 2,
],
])
->assertCreated()
->json('data.id');
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [
'direct_item' => [
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 2,
],
])
->assertUnprocessable()
->assertJsonValidationErrors('direct_item.cantidad');
$this->actingAs($otherUser, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [
'direct_item' => [
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 3,
],
])
->assertCreated();
$this->actingAs($user, 'sanctum')
->postJson("/api/tenants/sonder/compras/{$firstPurchaseId}/cancel")
->assertOk();
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [
'direct_item' => [
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 3,
],
])
->assertCreated();
}
public function test_the_user_purchase_limit_is_shared_by_all_item_variants(): void
{
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create();
$firstVariant = $this->createVariantForTenant('sonder', 20, '50.00');
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
$secondInventory = Inventory::query()->create(['real_stock' => 20]);
$secondVariant = Variant::query()->create([
'catalog_item_id' => $firstVariant->catalog_item_id,
'inventory_id' => $secondInventory->id,
]);
$cart = Cart::query()->create([
'tenant_codigo' => $tenant->codigo,
'user_id' => $user->id,
'status' => 'active',
]);
$cart->addItem($firstVariant->catalog_item_id, $firstVariant->id, 2);
$cart->addItem($secondVariant->catalog_item_id, $secondVariant->id, 2);
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [
'cart_id' => $cart->id,
])
->assertUnprocessable()
->assertJsonValidationErrors('cart_id');
$this->assertDatabaseCount('compras', 0);
$this->assertDatabaseHas('inventories', [
'id' => $firstVariant->inventory_id,
'reserved_stock' => 2,
]);
$this->assertDatabaseHas('inventories', [
'id' => $secondInventory->id,
'reserved_stock' => 2,
]);
}
public function test_it_restores_the_source_cart_when_checkout_is_cancelled(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
@@ -382,6 +469,32 @@ class StorePurchaseTest extends TestCase
]);
}
public function test_it_rejects_a_quantity_update_above_the_user_purchase_limit(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create();
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
$variant->catalogItem->update(['max_units_per_user' => 3]);
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
$itemId = $purchase->items->firstOrFail()->id;
$this->actingAs($user, 'sanctum')
->patchJson("/api/tenants/sonder/compras/{$purchase->id}/items/{$itemId}", [
'quantity' => 4,
])
->assertUnprocessable()
->assertJsonValidationErrors('quantity');
$this->assertDatabaseHas('compra_items', [
'id' => $itemId,
'cantidad' => 2,
]);
$this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id,
'reserved_stock' => 2,
]);
}
public function test_it_reopens_a_pending_purchase_before_editing_items(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');