feat: implement product management module with CRUD operations, service layer, and validation logic

This commit is contained in:
2026-06-30 09:50:40 -03:00
parent f664fbe2dc
commit 72261bf422
5 changed files with 125 additions and 8 deletions

View File

@@ -389,6 +389,75 @@ class ProductControllerTest extends TestCase
]);
}
public function test_it_returns_product_detail_with_variants_and_default_variant(): void
{
// 1. Create a product
$product = Product::create([
'tenant_codigo' => $this->tenant->codigo,
'categoria_id' => 1,
'slug' => 'test-product-show',
'nombre' => 'Test Product Show',
'precio' => 100.00,
]);
// Associate attributes to product
$product->attributes()->sync([$this->sizeAttr->id, $this->colorAttr->id]);
// 2. Create variant
$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,
'value' => 'S',
],
[
'attribute_id' => $this->colorAttr->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',
]);
$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 default_variant structure and data
$response->assertJsonStructure([
'data' => [
'id',
'nombre',
'default_variant' => [
'variant_id',
'images',
'attributes' => [
'talle',
'color',
],
],
],
]);
$response->assertJsonPath('data.default_variant.variant_id', $variant->id);
$response->assertJsonPath('data.default_variant.attributes.talle', 'S');
$response->assertJsonPath('data.default_variant.attributes.color', 'Azul');
$response->assertJsonCount(1, 'data.default_variant.images');
}
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
{
$hdrKey = (string) Str::uuid();