feat: refactor product detail retrieval to simplify variant handling and enhance response structure

This commit is contained in:
2026-07-01 01:05:21 +00:00
parent 429fa36b05
commit 1aa831f921
5 changed files with 98 additions and 90 deletions

View File

@@ -2,6 +2,8 @@
namespace Tests\Feature\Catalog;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
@@ -15,8 +17,11 @@ class ProductControllerTest extends TestCase
use RefreshDatabase;
private Tenant $tenant;
private Attribute $sizeAttr;
private Attribute $colorAttr;
private Attribute $extraAttr;
protected function setUp(): void
@@ -358,7 +363,7 @@ class ProductControllerTest extends TestCase
'options' => [
['value' => 'P', 'label' => 'Piqueno'],
['value' => 'M', 'label' => 'Medio'],
]
],
]);
// 2. Create product and associate attribute
@@ -384,12 +389,12 @@ class ProductControllerTest extends TestCase
[
'attribute_id' => $selectAttr->id,
'value' => 'G', // Invalid value
]
]
],
],
]);
}
public function test_it_returns_product_detail_with_variants_and_selected_variant_resource(): void
public function test_it_returns_product_detail_with_variant_mapping(): void
{
// 1. Create a product
$product = Product::create([
@@ -402,70 +407,77 @@ class ProductControllerTest extends TestCase
// Associate attributes to product
$product->attributes()->sync([$this->sizeAttr->id, $this->colorAttr->id]);
$sizeProductAttribute = $product->productAttributes()
->where('attribute_id', $this->sizeAttr->id)
->firstOrFail();
$colorProductAttribute = $product->productAttributes()
->where('attribute_id', $this->colorAttr->id)
->firstOrFail();
// 2. Create variant
// 2. Create variants
$variant = $product->createVariant([
'slug' => 'test-product-show-s-azul',
'nombre' => 'Test Product Show S Azul',
'stock' => 10,
'precio' => 105.00,
'definitions' => [
[
'attribute_id' => $this->sizeAttr->id,
'products_attribute_id' => $sizeProductAttribute->id,
'value' => 'S',
],
[
'attribute_id' => $this->colorAttr->id,
'products_attribute_id' => $colorProductAttribute->id,
'value' => 'Azul',
],
],
]);
// Attach an image to the variant
$attachment = \App\Domains\Attachable\Models\Attachment::create([
'key' => (string) Str::uuid(),
'path' => 'attachments/variant-img.png',
'filename' => 'variant-img.png',
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
'mime_type' => 'image/png',
$secondVariant = $product->createVariant([
'stock' => 4,
'definitions' => [
[
'products_attribute_id' => $sizeProductAttribute->id,
'value' => '38',
],
[
'products_attribute_id' => $colorProductAttribute->id,
'value' => 'Rojo',
],
],
]);
$variant->attachments()->attach($attachment->id);
// 3. Request product show detail
$response = $this->getJson("/api/tenants/{$this->tenant->codigo}/productos/{$product->id}");
$response->assertOk();
// 4. Assert selected variant resource structure and data
// 4. Assert variant mapping structure and data
$response->assertJsonStructure([
'data' => [
'id',
'nombre',
'variant' => [
'id',
'producto_id',
'stock',
'images',
'definitions' => [
'*' => [
'id',
'producto_variante_id',
'products_attribute_id',
'attribute_id',
'value',
'attribute',
'metadata',
'variants_map' => [
'*' => [
'variant_id',
'stock',
'attributes' => [
'talle',
'color',
],
],
],
],
]);
$response->assertJsonPath('data.variant.id', $variant->id);
$response->assertJsonPath('data.variant.producto_id', $product->id);
$response->assertJsonPath('data.variant.stock', 10);
$response->assertJsonCount(1, 'data.variant.images');
$response->assertJsonCount(2, 'data.variant.definitions');
$response->assertJsonMissingPath('data.attributes');
$response->assertJsonMissingPath('data.variants');
$response->assertJsonMissingPath('data.variant');
$response->assertJsonPath('data.variants_map.0.variant_id', $variant->id);
$response->assertJsonPath('data.variants_map.0.stock', 10);
$response->assertJsonPath('data.variants_map.0.attributes.talle', 'S');
$response->assertJsonPath('data.variants_map.0.attributes.color', 'Azul');
$response->assertJsonPath('data.variants_map.1.variant_id', $secondVariant->id);
$response->assertJsonPath('data.variants_map.1.stock', 4);
$response->assertJsonPath('data.variants_map.1.attributes.talle', '38');
$response->assertJsonPath('data.variants_map.1.attributes.color', 'Rojo');
$response->assertJsonCount(2, 'data.variants_map');
}
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
@@ -473,18 +485,18 @@ class ProductControllerTest extends TestCase
$hdrKey = (string) Str::uuid();
$ftrKey = (string) Str::uuid();
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
$headerAttachment = Attachment::create([
'key' => $hdrKey,
'path' => 'tenants/' . $hdrKey . '.png',
'path' => 'tenants/'.$hdrKey.'.png',
'filename' => 'logo_header.png',
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
$footerAttachment = Attachment::create([
'key' => $ftrKey,
'path' => 'tenants/' . $ftrKey . '.png',
'path' => 'tenants/'.$ftrKey.'.png',
'filename' => 'logo_footer.png',
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);