refactor(catalog): Complete catalog refactor to simplify its data model and its querying.
source commits: refactor/catalog
This commit is contained in:
152
tests/Feature/Purchase/PurchaseCatalogItemTest.php
Normal file
152
tests/Feature/Purchase/PurchaseCatalogItemTest.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Purchase;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Purchase\Services\CheckoutService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PurchaseCatalogItemTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_checkout_persists_catalog_identity_without_polymorphism(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
Storage::disk('s3')->buildTemporaryUrlsUsing(
|
||||
fn (string $path): string => "https://snapshots.test/{$path}",
|
||||
);
|
||||
|
||||
$tenant = $this->createTenant();
|
||||
$user = User::factory()->create();
|
||||
$inventory = Inventory::query()->create(['real_stock' => 10]);
|
||||
$catalogItem = CatalogItem::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'inventory_id' => $inventory->id,
|
||||
'slug' => 'checkout-item',
|
||||
'nombre' => 'Checkout item',
|
||||
'descripcion' => 'Original description',
|
||||
'precio' => 25,
|
||||
]);
|
||||
$productImage = $this->createAttachment('checkout-item');
|
||||
Storage::disk('s3')->put($productImage->path, 'original-image');
|
||||
$catalogItem->attachments()->attach($productImage->id, ['orden' => 0]);
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$cart->addItem($catalogItem->id, null, 2);
|
||||
|
||||
$service = app(CheckoutService::class);
|
||||
$purchase = $service->startCheckout($tenant, $user->id, [
|
||||
'cart_id' => $cart->id,
|
||||
'dni' => '12345678',
|
||||
'telefono' => '123456789',
|
||||
'nombre_apellido' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
$service->confirmPurchase($purchase);
|
||||
|
||||
$this->assertTrue(Schema::hasColumns('compra_items', [
|
||||
'source_catalog_item_id',
|
||||
'source_variant_id',
|
||||
'image_attachment_id',
|
||||
'nombre',
|
||||
'descripcion',
|
||||
'slug',
|
||||
'item_nombre',
|
||||
'variant_attributes',
|
||||
]));
|
||||
$this->assertFalse(Schema::hasColumn('compra_items', 'catalog_item_id'));
|
||||
$this->assertFalse(Schema::hasColumn('compra_items', 'variant_id'));
|
||||
$this->assertFalse(Schema::hasColumn('compra_items', 'buyable_type'));
|
||||
$this->assertFalse(Schema::hasColumn('compra_items', 'buyable_id'));
|
||||
$this->assertDatabaseHas('compra_items', [
|
||||
'compra_id' => $purchase->id,
|
||||
'source_catalog_item_id' => $catalogItem->id,
|
||||
'source_variant_id' => null,
|
||||
'image_attachment_id' => $purchase->items()->value('image_attachment_id'),
|
||||
'nombre' => 'Checkout item',
|
||||
'descripcion' => 'Original description',
|
||||
'slug' => 'checkout-item',
|
||||
'item_nombre' => 'Checkout item',
|
||||
'cantidad' => 2,
|
||||
'precio_unitario' => '25.00',
|
||||
'total' => '50.00',
|
||||
]);
|
||||
$purchaseItem = $purchase->items()->with('imageAttachment')->firstOrFail();
|
||||
$this->assertNotNull($purchaseItem->imageAttachment);
|
||||
$this->assertNotSame($productImage->id, $purchaseItem->image_attachment_id);
|
||||
$this->assertSame('original-image', Storage::disk('s3')->get($purchaseItem->imageAttachment->path));
|
||||
$this->assertStringStartsWith(
|
||||
"purchase/{$purchase->id}/",
|
||||
$purchaseItem->imageAttachment->path,
|
||||
);
|
||||
$catalogItem->update([
|
||||
'nombre' => 'Changed catalog item',
|
||||
'descripcion' => 'Changed description',
|
||||
]);
|
||||
$catalogItem->delete();
|
||||
|
||||
$purchaseItem->refresh();
|
||||
$this->assertSame('Checkout item', $purchaseItem->nombre);
|
||||
$this->assertSame('Original description', $purchaseItem->descripcion);
|
||||
$this->assertDatabaseHas('compra_items', ['id' => $purchaseItem->id]);
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->getJson("/api/tenants/{$tenant->codigo}/compras/{$purchase->id}")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.source_catalog_item_id', $catalogItem->id)
|
||||
->assertJsonPath('data.items.0.item_details.nombre', 'Checkout item')
|
||||
->assertJsonPath('data.items.0.item_details.descripcion', 'Original description')
|
||||
->assertJsonMissingPath('data.items.0.product')
|
||||
->assertJsonMissingPath('data.items.0.variant');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $inventory->id,
|
||||
'real_stock' => 8,
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment('purchase-header');
|
||||
$footerLogo = $this->createAttachment('purchase-footer');
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => 'purchase-catalog',
|
||||
'nombre' => 'Purchase Catalog',
|
||||
'dominio' => 'purchase-catalog.local',
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $name): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "test/{$name}.png",
|
||||
'filename' => "{$name}.png",
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
'extension' => 'png',
|
||||
'size' => 14,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,10 @@ use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Category;
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Services\CheckoutService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
@@ -32,27 +33,25 @@ class StorePurchaseTest extends TestCase
|
||||
'nombre' => 'Test Category',
|
||||
]);
|
||||
|
||||
$product = Product::query()->create([
|
||||
'tenant_codigo' => 'sonder',
|
||||
'categoria_id' => $category->id,
|
||||
$inventory = Inventory::query()->create(['real_stock' => 10]);
|
||||
$catalogItem = CatalogItem::query()->create([
|
||||
'tenant_code' => 'sonder',
|
||||
'category_id' => $category->id,
|
||||
'slug' => 'test-product',
|
||||
'nombre' => 'Test Product',
|
||||
'descripcion' => 'Test',
|
||||
'precio' => '50.00',
|
||||
]);
|
||||
|
||||
$variant = ProductVariant::query()->create([
|
||||
'producto_id' => $product->id,
|
||||
'slug' => 'test-variant',
|
||||
'nombre' => 'Test Variant',
|
||||
'stock' => 10,
|
||||
'precio' => '50.00',
|
||||
$variant = Variant::query()->create([
|
||||
'catalog_item_id' => $catalogItem->id,
|
||||
'inventory_id' => $inventory->id,
|
||||
]);
|
||||
|
||||
$cartResponse = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $catalogItem->id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk();
|
||||
@@ -112,14 +111,14 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'cart_id' => $cartId,
|
||||
'buyable_type' => ProductVariant::class,
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $catalogItem->id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 10,
|
||||
'stock_reservado' => 2,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $inventory->id,
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -133,8 +132,8 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$cartId = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk()
|
||||
@@ -187,9 +186,9 @@ class StorePurchaseTest extends TestCase
|
||||
->assertJsonPath('data.items.0.quantity', 2)
|
||||
->assertJsonPath('data.items.0.unit_price', '50.00')
|
||||
->assertJsonPath('data.items.0.line_total', '100.00')
|
||||
->assertJsonPath('data.items.0.product.id', $variant->product->id)
|
||||
->assertJsonPath('data.items.0.product.nombre', $variant->product->nombre)
|
||||
->assertJsonPath('data.items.0.product.slug', $variant->product->slug)
|
||||
->assertJsonPath('data.items.0.product.id', $variant->catalogItem->id)
|
||||
->assertJsonPath('data.items.0.product.nombre', $variant->catalogItem->nombre)
|
||||
->assertJsonPath('data.items.0.product.slug', $variant->catalogItem->slug)
|
||||
->assertJsonPath('data.items.0.product.imagen', null)
|
||||
->assertJsonPath('data.items.0.variant.id', $variant->id)
|
||||
->assertJsonPath('data.items.0.variant.attributes', [])
|
||||
@@ -245,11 +244,11 @@ class StorePurchaseTest extends TestCase
|
||||
$checkoutService->confirmPurchase($purchase);
|
||||
$purchase->refresh()->markAsPaid();
|
||||
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 8,
|
||||
'stock_reservado' => 0,
|
||||
'cantidad_vendida' => 2,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 8,
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 2,
|
||||
]);
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
@@ -265,10 +264,10 @@ class StorePurchaseTest extends TestCase
|
||||
->assertJsonPath('data.items.0.quantity', 2)
|
||||
->assertJsonPath('data.items.0.unit_price', '50.00')
|
||||
->assertJsonPath('data.items.0.line_total', '100.00')
|
||||
->assertJsonPath('data.items.0.product.id', $variant->product->id)
|
||||
->assertJsonPath('data.items.0.product.imagen', null)
|
||||
->assertJsonPath('data.items.0.variant.id', $variant->id)
|
||||
->assertJsonPath('data.items.0.variant.attributes', [])
|
||||
->assertJsonPath('data.items.0.source_catalog_item_id', $variant->catalog_item_id)
|
||||
->assertJsonPath('data.items.0.source_variant_id', $variant->id)
|
||||
->assertJsonPath('data.items.0.item_details.imagen', null)
|
||||
->assertJsonPath('data.items.0.item_details.attributes', [])
|
||||
->assertJsonPath('data.subtotal', '100.00')
|
||||
->assertJsonPath('data.total', '100.00');
|
||||
}
|
||||
@@ -283,8 +282,13 @@ class StorePurchaseTest extends TestCase
|
||||
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
|
||||
|
||||
$purchase->items()->create([
|
||||
'buyable_type' => ProductVariant::class,
|
||||
'buyable_id' => $variant->id,
|
||||
'source_catalog_item_id' => $variant->catalog_item_id,
|
||||
'source_variant_id' => $variant->id,
|
||||
'nombre' => $variant->catalogItem->nombre,
|
||||
'descripcion' => $variant->catalogItem->descripcion,
|
||||
'slug' => $variant->catalogItem->slug,
|
||||
'item_nombre' => $variant->getName(),
|
||||
'variant_attributes' => [],
|
||||
'cantidad' => 1,
|
||||
'precio_unitario' => '50.00',
|
||||
'discount_total' => null,
|
||||
@@ -330,8 +334,8 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$cartId = $this->actingAs($owner, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
@@ -357,8 +361,8 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$cartId = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/globex/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
@@ -438,12 +442,11 @@ class StorePurchaseTest extends TestCase
|
||||
$checkoutService->confirmPurchase($purchase);
|
||||
$checkoutService->confirmPurchase($purchase);
|
||||
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
'stock_real' => 0,
|
||||
'stock_reservado' => 0,
|
||||
'cantidad_vendida' => 25,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 0,
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 25,
|
||||
]);
|
||||
$this->assertDatabaseCount('compra_items', 1);
|
||||
}
|
||||
@@ -454,7 +457,7 @@ class StorePurchaseTest extends TestCase
|
||||
string $price,
|
||||
string $slugPrefix = 'shirt',
|
||||
InventoryPolicy $inventoryPolicy = InventoryPolicy::Tracked,
|
||||
): ProductVariant {
|
||||
): Variant {
|
||||
$tenant = Tenant::query()->where('codigo', $tenantCode)->first();
|
||||
if (! $tenant) {
|
||||
$this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com");
|
||||
@@ -465,30 +468,27 @@ class StorePurchaseTest extends TestCase
|
||||
'nombre' => "{$slugPrefix} category {$tenantCode}",
|
||||
]);
|
||||
|
||||
$product = Product::query()->create([
|
||||
'tenant_codigo' => $tenantCode,
|
||||
'categoria_id' => $category->id,
|
||||
'slug' => "{$slugPrefix}-{$tenantCode}-".Product::query()->count(),
|
||||
$inventory = Inventory::query()->create(['real_stock' => $stock]);
|
||||
$catalogItem = CatalogItem::query()->create([
|
||||
'tenant_code' => $tenantCode,
|
||||
'category_id' => $category->id,
|
||||
'slug' => "{$slugPrefix}-{$tenantCode}-".CatalogItem::query()->count(),
|
||||
'nombre' => ucfirst($slugPrefix)." {$tenantCode}",
|
||||
'descripcion' => 'Test product',
|
||||
'precio' => $price,
|
||||
'inventory_policy' => $inventoryPolicy,
|
||||
]);
|
||||
|
||||
return ProductVariant::query()->create([
|
||||
'producto_id' => $product->id,
|
||||
'inventory_policy' => $inventoryPolicy->value,
|
||||
'slug' => "{$slugPrefix}-variant-".ProductVariant::query()->count(),
|
||||
'nombre' => ucfirst($slugPrefix).' Variant',
|
||||
'stock' => $stock,
|
||||
'descripcion' => 'Test variant',
|
||||
'precio' => $price,
|
||||
])->load('product');
|
||||
return Variant::query()->create([
|
||||
'catalog_item_id' => $catalogItem->id,
|
||||
'inventory_id' => $inventory->id,
|
||||
])->load(['catalogItem', 'inventory']);
|
||||
}
|
||||
|
||||
protected function createCheckoutPurchase(
|
||||
User $user,
|
||||
string $tenantCode,
|
||||
ProductVariant $variant,
|
||||
Variant $variant,
|
||||
int $quantity,
|
||||
): Purchase {
|
||||
$tenant = Tenant::query()->where('codigo', $tenantCode)->firstOrFail();
|
||||
@@ -498,7 +498,7 @@ class StorePurchaseTest extends TestCase
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$cart->addItem(ProductVariant::class, $variant->id, $quantity);
|
||||
$cart->addItem($variant->catalog_item_id, $variant->id, $quantity);
|
||||
|
||||
return app(CheckoutService::class)->startCheckout($tenant, $user->id, [
|
||||
'cart_id' => $cart->id,
|
||||
|
||||
Reference in New Issue
Block a user