feat: enhance checkout process with cart integration and stock confirmation

This commit is contained in:
2026-07-06 14:06:25 -03:00
parent 6bda3bf013
commit dc32eb9bee
11 changed files with 403 additions and 64 deletions

View File

@@ -3,6 +3,7 @@
namespace Tests\Feature\Purchase;
use App\Domains\Auth\Models\User;
use App\Domains\Cart\Models\Cart;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Tenant\Models\Tenant;
@@ -13,17 +14,12 @@ class StorePurchaseTest extends TestCase
{
use RefreshDatabase;
public function test_it_creates_a_purchase_with_customer_details_and_clears_the_cart(): void
public function test_it_creates_a_purchase_from_cart_id_without_persisting_items_yet(): void
{
// 1. Setup Tenant
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
// 2. Setup User
$user = User::factory()->create([
'email' => 'buyer@example.com'
'email' => 'buyer@example.com',
]);
// 3. Setup Product & Variant
$category = \App\Domains\Catalog\Models\Category::query()->create([
'tenant_code' => 'sonder',
'nombre' => 'Test Category',
@@ -46,61 +42,206 @@ class StorePurchaseTest extends TestCase
'precio' => '50.00',
]);
// 4. Add item to user's cart
$this->actingAs($user, 'sanctum')
$cartResponse = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/cart/items', [
'product_variant_id' => $variant->id,
'cantidad' => 2,
])
->assertOk();
// Check cart exists in DB
$cartId = $cartResponse->json('data.id');
$this->assertDatabaseHas('carritos', [
'id' => $cartId,
'tenant_codigo' => 'sonder',
'user_id' => $user->id,
'status' => 'active',
]);
// 5. Submit Purchase with payment_method at root
$response = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras', [
'cart_id' => $cartId,
'dni' => '987654321',
'telefono' => '+54 9 341 555-4321',
'nombre_apellido' => 'Juan Perez',
'email' => 'juan.perez@example.com',
'payment_method' => 'transferencia',
'items' => [
[
'producto_variante_id' => $variant->id,
'cantidad' => 2,
]
]
]);
// 6. Assertions
$response->assertCreated();
$response->assertJsonPath('data.cart_id', $cartId);
$response->assertJsonPath('data.dni', '987654321');
$response->assertJsonPath('data.telefono', '+54 9 341 555-4321');
$response->assertJsonPath('data.nombre_apellido', 'Juan Perez');
$response->assertJsonPath('data.email', 'juan.perez@example.com');
$response->assertJsonPath('data.payment_method', 'transferencia');
$response->assertJsonPath('data.tenant_codigo', 'sonder');
$response->assertJsonPath('data.items', []);
$response->assertJsonPath('data.total', '0.00');
$purchaseId = $response->json('data.id');
// Assert Purchase saved in DB
$this->assertDatabaseHas('compras', [
'id' => $purchaseId,
'cart_id' => $cartId,
'tenant_codigo' => 'sonder',
'user_id' => $user->id,
'dni' => '987654321',
'telefono' => '+54 9 341 555-4321',
'nombre_apellido' => 'Juan Perez',
'email' => 'juan.perez@example.com',
'payment_method' => 'transferencia',
]);
// Assert Cart was cleared (deleted)
$this->assertDatabaseMissing('carritos', [
'tenant_codigo' => 'sonder',
$this->assertDatabaseMissing('compra_items', [
'compra_id' => $purchaseId,
]);
$this->assertDatabaseHas('carritos', [
'id' => $cartId,
'status' => 'active',
'user_id' => $user->id,
]);
$this->assertDatabaseMissing('carritos', [
'id' => $cartId,
'deleted_at' => null,
]);
$this->assertDatabaseHas('carrito_items', [
'cart_id' => $cartId,
'producto_variante_id' => $variant->id,
'cantidad' => 2,
]);
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'stock_real' => 10,
'stock_reservado' => 2,
]);
}
public function test_it_rejects_a_cart_from_another_user(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$owner = User::factory()->create();
$attacker = User::factory()->create();
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
$cartId = $this->actingAs($owner, 'sanctum')
->postJson('/api/tenants/sonder/cart/items', [
'product_variant_id' => $variant->id,
'cantidad' => 1,
])
->assertOk()
->json('data.id');
$this->actingAs($attacker, 'sanctum')
->postJson('/api/tenants/sonder/compras', [
'cart_id' => $cartId,
'dni' => '12345678',
'telefono' => '+54 9 341 555-1111',
'nombre_apellido' => 'Intruso',
'email' => 'intruso@example.com',
])
->assertNotFound();
}
public function test_it_rejects_a_cart_from_another_tenant(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$this->createTenant('globex', 'Globex', 'globex.com.ar');
$user = User::factory()->create();
$variant = $this->createVariantForTenant('globex', 10, '50.00');
$cartId = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/globex/cart/items', [
'product_variant_id' => $variant->id,
'cantidad' => 1,
])
->assertOk()
->json('data.id');
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras', [
'cart_id' => $cartId,
'dni' => '12345678',
'telefono' => '+54 9 341 555-1111',
'nombre_apellido' => 'Juan Perez',
'email' => 'juan.perez@example.com',
])
->assertNotFound();
}
public function test_it_rejects_an_empty_cart(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create();
$cart = Cart::query()->create([
'tenant_codigo' => 'sonder',
'user_id' => $user->id,
'status' => 'active',
]);
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras', [
'cart_id' => $cart->id,
'dni' => '12345678',
'telefono' => '+54 9 341 555-1111',
'nombre_apellido' => 'Juan Perez',
'email' => 'juan.perez@example.com',
])
->assertUnprocessable()
->assertJsonValidationErrors(['cart_id']);
}
public function test_it_rejects_a_converted_cart(): void
{
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create();
$cart = Cart::query()->create([
'tenant_codigo' => 'sonder',
'user_id' => $user->id,
'status' => 'converted',
]);
$this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras', [
'cart_id' => $cart->id,
'dni' => '12345678',
'telefono' => '+54 9 341 555-1111',
'nombre_apellido' => 'Juan Perez',
'email' => 'juan.perez@example.com',
])
->assertUnprocessable()
->assertJsonValidationErrors(['cart_id']);
}
protected function createVariantForTenant(
string $tenantCode,
int $stock,
string $price,
string $slugPrefix = 'shirt',
): ProductVariant {
$tenant = Tenant::query()->where('codigo', $tenantCode)->first();
if (! $tenant) {
$this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com");
}
$category = \App\Domains\Catalog\Models\Category::query()->create([
'tenant_code' => $tenantCode,
'nombre' => "{$slugPrefix} category {$tenantCode}",
]);
$product = Product::query()->create([
'tenant_codigo' => $tenantCode,
'categoria_id' => $category->id,
'slug' => "{$slugPrefix}-{$tenantCode}-".Product::query()->count(),
'nombre' => ucfirst($slugPrefix)." {$tenantCode}",
'descripcion' => 'Test product',
'precio' => $price,
]);
return ProductVariant::query()->create([
'producto_id' => $product->id,
'slug' => "{$slugPrefix}-variant-".ProductVariant::query()->count(),
'nombre' => ucfirst($slugPrefix).' Variant',
'stock' => $stock,
'descripcion' => 'Test variant',
'precio' => $price,
])->load('product');
}
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant