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:
@@ -4,10 +4,13 @@ namespace Tests\Feature\Cart;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -99,6 +102,114 @@ class CartControllerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_authenticated_cart_respects_previous_purchases_and_repeated_additions(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
$item = $this->createDirectItem($tenant, 20, '10.00');
|
||||
$item->update(['max_units_per_user' => 4]);
|
||||
$this->createPurchaseItem($tenant, $user, $item, 1);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.cantidad', 2);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('cantidad');
|
||||
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_authenticated_cart_shares_the_purchase_limit_between_variants(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
[$item, $firstVariant] = $this->createVariantItem($tenant, 20, '10.00');
|
||||
$item->update(['max_units_per_user' => 3]);
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 20]);
|
||||
$secondVariant = $item->variants()->create(['inventory_id' => $secondInventory->id]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $firstVariant->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $secondVariant->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('cantidad');
|
||||
|
||||
$this->assertDatabaseCount('carrito_items', 1);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $secondInventory->id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_authenticated_cart_rejects_quantity_updates_above_the_purchase_limit(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
$item = $this->createDirectItem($tenant, 20, '10.00');
|
||||
$item->update(['max_units_per_user' => 3]);
|
||||
|
||||
$response = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->patchJson('/api/tenants/acme/cart/items/'.$response->json('data.items.0.id'), [
|
||||
'cantidad' => 4,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('cantidad');
|
||||
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'id' => $response->json('data.items.0.id'),
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_guest_cart_does_not_apply_a_user_purchase_limit(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$item = $this->createDirectItem($tenant, 20, '10.00');
|
||||
$item->update(['max_units_per_user' => 1]);
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.cantidad', 2);
|
||||
}
|
||||
|
||||
public function test_it_updates_and_removes_an_item_using_its_selected_inventory(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
@@ -145,6 +256,110 @@ class CartControllerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_changes_an_item_variant_and_moves_the_stock_reservation(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item, $firstVariant] = $this->createVariantItem($tenant, 10, '15.00');
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 2]);
|
||||
$secondVariant = $item->variants()->create([
|
||||
'inventory_id' => $secondInventory->id,
|
||||
'precio' => '20.00',
|
||||
]);
|
||||
$unavailableInventory = Inventory::query()->create(['real_stock' => 0]);
|
||||
$unavailableVariant = $item->variants()->create([
|
||||
'inventory_id' => $unavailableInventory->id,
|
||||
'precio' => '25.00',
|
||||
]);
|
||||
$createResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $firstVariant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
$guestToken = $createResponse->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $createResponse->json('data.items.0.id');
|
||||
|
||||
$this->call(
|
||||
'PATCH',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode(['cantidad' => 2, 'variant_id' => $secondVariant->id]),
|
||||
)
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.variant_id', $secondVariant->id)
|
||||
->assertJsonPath('data.items.0.precio_unitario', '20.00')
|
||||
->assertJsonCount(2, 'data.items.0.product.variants')
|
||||
->assertJsonPath('data.items.0.product.variants.0.id', $firstVariant->id)
|
||||
->assertJsonPath('data.items.0.product.variants.1.id', $secondVariant->id)
|
||||
->assertJsonPath('data.items.0.product.variants.1.stock_tecnico', 0)
|
||||
->assertJsonMissing(['id' => $unavailableVariant->id, 'stock_tecnico' => 0]);
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstVariant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $secondInventory->id,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_changing_to_a_variant_already_in_the_cart_merges_both_rows(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item, $firstVariant] = $this->createVariantItem($tenant, 10, '15.00');
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 10]);
|
||||
$secondVariant = $item->variants()->create(['inventory_id' => $secondInventory->id]);
|
||||
|
||||
$firstResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $firstVariant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
$guestToken = $firstResponse->getCookie('guest_token', false)?->getValue();
|
||||
$firstCartItemId = $firstResponse->json('data.items.0.id');
|
||||
|
||||
$this->call(
|
||||
'POST',
|
||||
'/api/tenants/acme/cart/items',
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $secondVariant->id,
|
||||
'cantidad' => 3,
|
||||
]),
|
||||
)->assertOk();
|
||||
|
||||
$this->call(
|
||||
'PATCH',
|
||||
"/api/tenants/acme/cart/items/{$firstCartItemId}",
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode(['cantidad' => 2, 'variant_id' => $secondVariant->id]),
|
||||
)
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data.items')
|
||||
->assertJsonPath('data.items.0.variant_id', $secondVariant->id)
|
||||
->assertJsonPath('data.items.0.cantidad', 5);
|
||||
|
||||
$this->assertDatabaseCount('carrito_items', 1);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstVariant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $secondInventory->id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_requires_a_variant_when_the_item_has_variant_inventory(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
@@ -245,6 +460,33 @@ class CartControllerTest extends TestCase
|
||||
return [$item, $variant];
|
||||
}
|
||||
|
||||
private function createPurchaseItem(
|
||||
Tenant $tenant,
|
||||
User $user,
|
||||
CatalogItem $item,
|
||||
int $quantity,
|
||||
): PurchaseItem {
|
||||
$purchase = Purchase::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => Purchase::STATUS_PAID,
|
||||
'total' => (float) $item->precio * $quantity,
|
||||
]);
|
||||
|
||||
return $purchase->items()->create([
|
||||
'source_catalog_item_id' => $item->id,
|
||||
'source_variant_id' => null,
|
||||
'nombre' => $item->nombre,
|
||||
'slug' => $item->slug,
|
||||
'item_nombre' => $item->nombre,
|
||||
'variant_attributes' => [],
|
||||
'cantidad' => $quantity,
|
||||
'precio_unitario' => $item->precio,
|
||||
'total' => (float) $item->precio * $quantity,
|
||||
'reservation_status' => PurchaseItem::RESERVATION_COMMITTED,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header");
|
||||
|
||||
Reference in New Issue
Block a user