feat: implement product creation workflow with variants, attribute definitions, and validation logic
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace Tests\Feature\Cart;
|
||||
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use App\Domains\Catalog\Models\ProductAttribute;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Catalog\Models\ProductVariantDefinition;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
@@ -33,7 +33,7 @@ class CartControllerTest extends TestCase
|
||||
public function test_it_creates_a_guest_cart_and_returns_the_cart_snapshot(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '49.90');
|
||||
$attribute = ProductAttribute::query()->create([
|
||||
$attribute = Attribute::query()->create([
|
||||
'tenant_codigo' => 'acme',
|
||||
'codigo' => 'color',
|
||||
'nombre' => 'Color',
|
||||
|
||||
@@ -6,7 +6,7 @@ use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProductAttributeControllerTest extends TestCase
|
||||
class AttributeControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -14,7 +14,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
{
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/product-attributes', [
|
||||
$response = $this->postJson('/api/tenants/acme/attributes', [
|
||||
'codigo' => 'color',
|
||||
'nombre' => 'Color',
|
||||
'is_required' => true,
|
||||
@@ -47,7 +47,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
->assertJsonPath('data.options.0.label', 'Red')
|
||||
->assertJsonPath('data.options.1.metadata.hex', '#0000ff');
|
||||
|
||||
$this->assertDatabaseHas('productos_attributes', [
|
||||
$this->assertDatabaseHas('attribute', [
|
||||
'tenant_codigo' => 'acme',
|
||||
'codigo' => 'color',
|
||||
'type' => 'select',
|
||||
@@ -64,7 +64,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
{
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/product-attributes', [
|
||||
$response = $this->postJson('/api/tenants/acme/attributes', [
|
||||
'codigo' => 'material',
|
||||
'nombre' => 'Material',
|
||||
'type' => 'string',
|
||||
388
tests/Feature/Catalog/ProductControllerTest.php
Normal file
388
tests/Feature/Catalog/ProductControllerTest.php
Normal file
@@ -0,0 +1,388 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Catalog;
|
||||
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProductControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Tenant $tenant;
|
||||
private Attribute $sizeAttr;
|
||||
private Attribute $colorAttr;
|
||||
private Attribute $extraAttr;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->tenant = $this->createTenant('acme', 'Acme Inc.', 'acme.com');
|
||||
|
||||
// Create attributes for variants
|
||||
$this->sizeAttr = Attribute::create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'codigo' => 'talle',
|
||||
'nombre' => 'Talle',
|
||||
'is_required' => true,
|
||||
'type' => 'select',
|
||||
]);
|
||||
|
||||
$this->colorAttr = Attribute::create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'codigo' => 'color',
|
||||
'nombre' => 'Color',
|
||||
'is_required' => true,
|
||||
'type' => 'select',
|
||||
]);
|
||||
|
||||
$this->extraAttr = Attribute::create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'codigo' => 'extra',
|
||||
'nombre' => 'Extra Attribute',
|
||||
'is_required' => false,
|
||||
'type' => 'string',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_creates_product_with_variants_and_definitions_and_syncs_attributes(): void
|
||||
{
|
||||
$payload = [
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'remera-sport',
|
||||
'nombre' => 'Remera Sport',
|
||||
'descripcion' => 'Remera para hacer deportes',
|
||||
'precio' => 15000.00,
|
||||
'attribute_ids' => [
|
||||
$this->extraAttr->id,
|
||||
],
|
||||
'variants' => [
|
||||
[
|
||||
'slug' => 'remera-sport-s-azul',
|
||||
'nombre' => 'Remera Sport S Azul',
|
||||
'stock' => 10,
|
||||
'precio' => 15000.00,
|
||||
'definitions' => [
|
||||
[
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => 'S',
|
||||
],
|
||||
[
|
||||
'attribute_id' => $this->colorAttr->id,
|
||||
'value' => 'Azul',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'slug' => 'remera-sport-m-azul',
|
||||
'nombre' => 'Remera Sport M Azul',
|
||||
'stock' => 5,
|
||||
'precio' => 16000.00,
|
||||
'definitions' => [
|
||||
[
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => 'M',
|
||||
],
|
||||
[
|
||||
'attribute_id' => $this->colorAttr->id,
|
||||
'value' => 'Azul',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->postJson("/api/tenants/{$this->tenant->codigo}/productos", $payload);
|
||||
|
||||
$response->assertCreated();
|
||||
|
||||
// Assert JSON structure
|
||||
$response->assertJsonPath('data.nombre', 'Remera Sport');
|
||||
$response->assertJsonCount(2, 'data.variants');
|
||||
$response->assertJsonPath('data.variants.0.slug', 'remera-sport-s-azul');
|
||||
$response->assertJsonPath('data.variants.0.definitions.0.value', 'S');
|
||||
$response->assertJsonPath('data.variants.1.precio', '16000.00');
|
||||
|
||||
// Assert Database
|
||||
$this->assertDatabaseHas('productos', [
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'slug' => 'remera-sport',
|
||||
]);
|
||||
|
||||
$product = Product::where('slug', 'remera-sport')->firstOrFail();
|
||||
|
||||
// Assert that products_attributes has both explicit extraAttr and those from variants (sizeAttr, colorAttr)
|
||||
$this->assertDatabaseHas('products_attributes', [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $this->extraAttr->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('products_attributes', [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('products_attributes', [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $this->colorAttr->id,
|
||||
]);
|
||||
|
||||
$this->assertCount(2, $product->variants);
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'producto_id' => $product->id,
|
||||
'slug' => 'remera-sport-s-azul',
|
||||
'stock' => 10,
|
||||
'precio' => 15000.00,
|
||||
]);
|
||||
|
||||
$variantS = ProductVariant::where('slug', 'remera-sport-s-azul')->firstOrFail();
|
||||
$this->assertDatabaseHas('productos_variantes_values', [
|
||||
'producto_variante_id' => $variantS->id,
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => 'S',
|
||||
]);
|
||||
$this->assertDatabaseHas('productos_variantes_values', [
|
||||
'producto_variante_id' => $variantS->id,
|
||||
'attribute_id' => $this->colorAttr->id,
|
||||
'value' => 'Azul',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_updates_product_and_syncs_variants_and_attributes(): void
|
||||
{
|
||||
// 1. Create a product with 2 variants initially
|
||||
$product = Product::create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'pantalon-cargo',
|
||||
'nombre' => 'Pantalon Cargo',
|
||||
'precio' => 20000.00,
|
||||
]);
|
||||
|
||||
$product->attributes()->sync([$this->extraAttr->id]);
|
||||
|
||||
$v1 = $product->variants()->create([
|
||||
'slug' => 'pantalon-cargo-38',
|
||||
'nombre' => 'Pantalon Cargo 38',
|
||||
'stock' => 4,
|
||||
'precio' => 20000.00,
|
||||
]);
|
||||
$v1->definitions()->create([
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => '38',
|
||||
]);
|
||||
|
||||
$v2 = $product->variants()->create([
|
||||
'slug' => 'pantalon-cargo-40',
|
||||
'nombre' => 'Pantalon Cargo 40',
|
||||
'stock' => 8,
|
||||
'precio' => 20000.00,
|
||||
]);
|
||||
$v2->definitions()->create([
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => '40',
|
||||
]);
|
||||
|
||||
// 2. Perform update payload
|
||||
// We will:
|
||||
// - Update v1 (change stock/precio, keep id)
|
||||
// - Delete v2 (by omitting it)
|
||||
// - Create a new variant v3
|
||||
// - Update attribute_ids (remove extraAttr, add sizeAttr and colorAttr indirectly through variants)
|
||||
$payload = [
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'pantalon-cargo-new-slug',
|
||||
'nombre' => 'Pantalon Cargo V2',
|
||||
'precio' => 22000.00,
|
||||
'attribute_ids' => [], // explicitly remove extraAttr
|
||||
'variants' => [
|
||||
[
|
||||
'id' => $v1->id,
|
||||
'slug' => 'pantalon-cargo-38-updated',
|
||||
'nombre' => 'Pantalon Cargo 38 Updated',
|
||||
'stock' => 12,
|
||||
'precio' => 22000.00,
|
||||
'definitions' => [
|
||||
[
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => '38-updated',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'slug' => 'pantalon-cargo-42',
|
||||
'nombre' => 'Pantalon Cargo 42',
|
||||
'stock' => 15,
|
||||
'precio' => 22000.00,
|
||||
'definitions' => [
|
||||
[
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => '42',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->putJson(
|
||||
"/api/tenants/{$this->tenant->codigo}/productos/{$product->id}",
|
||||
$payload
|
||||
);
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
// Assert updated values
|
||||
$response->assertJsonPath('data.nombre', 'Pantalon Cargo V2');
|
||||
$response->assertJsonCount(2, 'data.variants');
|
||||
|
||||
// Check DB state
|
||||
// extraAttr must be detached
|
||||
$this->assertDatabaseMissing('products_attributes', [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $this->extraAttr->id,
|
||||
]);
|
||||
// sizeAttr must be attached (as it's used in variant v1 and v3 definitions)
|
||||
$this->assertDatabaseHas('products_attributes', [
|
||||
'product_id' => $product->id,
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
]);
|
||||
|
||||
// v1 must be updated
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $v1->id,
|
||||
'slug' => 'pantalon-cargo-38-updated',
|
||||
'stock' => 12,
|
||||
'precio' => 22000.00,
|
||||
]);
|
||||
$this->assertDatabaseHas('productos_variantes_values', [
|
||||
'producto_variante_id' => $v1->id,
|
||||
'attribute_id' => $this->sizeAttr->id,
|
||||
'value' => '38-updated',
|
||||
]);
|
||||
|
||||
// v2 must be deleted
|
||||
$this->assertDatabaseMissing('productos_variantes', [
|
||||
'id' => $v2->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('productos_variantes_values', [
|
||||
'producto_variante_id' => $v2->id,
|
||||
]);
|
||||
|
||||
// v3 must be created
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'producto_id' => $product->id,
|
||||
'slug' => 'pantalon-cargo-42',
|
||||
'stock' => 15,
|
||||
'precio' => 22000.00,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_does_not_modify_variants_if_not_present_in_update_payload(): void
|
||||
{
|
||||
$product = Product::create([
|
||||
'tenant_codigo' => $this->tenant->codigo,
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'short-running',
|
||||
'nombre' => 'Short Running',
|
||||
'precio' => 8000.00,
|
||||
]);
|
||||
|
||||
$v1 = $product->variants()->create([
|
||||
'slug' => 'short-running-m',
|
||||
'nombre' => 'Short Running M',
|
||||
'stock' => 5,
|
||||
'precio' => 8000.00,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'short-running',
|
||||
'nombre' => 'Short Running Updated',
|
||||
'precio' => 9000.00,
|
||||
];
|
||||
|
||||
$response = $this->putJson(
|
||||
"/api/tenants/{$this->tenant->codigo}/productos/{$product->id}",
|
||||
$payload
|
||||
);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertDatabaseHas('productos', [
|
||||
'id' => $product->id,
|
||||
'nombre' => 'Short Running Updated',
|
||||
]);
|
||||
|
||||
// Variant should still exist untouched
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $v1->id,
|
||||
'slug' => 'short-running-m',
|
||||
'stock' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_rejects_variants_with_invalid_attributes(): void
|
||||
{
|
||||
$payload = [
|
||||
'categoria_id' => 1,
|
||||
'slug' => 'remera-sport',
|
||||
'nombre' => 'Remera Sport',
|
||||
'precio' => 15000.00,
|
||||
'variants' => [
|
||||
[
|
||||
'slug' => 'remera-sport-s-azul',
|
||||
'nombre' => 'Remera Sport S Azul',
|
||||
'stock' => 10,
|
||||
'precio' => 15000.00,
|
||||
'definitions' => [
|
||||
[
|
||||
'attribute_id' => 99999, // Non-existent ID
|
||||
'value' => 'S',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->postJson("/api/tenants/{$this->tenant->codigo}/productos", $payload);
|
||||
|
||||
$response->assertUnprocessable();
|
||||
$response->assertJsonValidationErrors(['variants.0.definitions.0.attribute_id']);
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
{
|
||||
$hdrKey = (string) Str::uuid();
|
||||
$ftrKey = (string) Str::uuid();
|
||||
|
||||
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $hdrKey,
|
||||
'path' => 'tenants/' . $hdrKey . '.png',
|
||||
'filename' => 'logo_header.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $ftrKey,
|
||||
'path' => 'tenants/' . $ftrKey . '.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
'codigo' => $codigo,
|
||||
'nombre' => $nombre,
|
||||
'dominio' => $dominio,
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user