feat: add attachment relationship to ProductVariant model and database schema
This commit is contained in:
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Domains\Catalog\Models;
|
namespace App\Domains\Catalog\Models;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
@@ -46,4 +48,17 @@ class ProductVariant extends Model
|
|||||||
{
|
{
|
||||||
return $this->hasMany(ProductVariantDefinition::class, 'producto_variante_id');
|
return $this->hasMany(ProductVariantDefinition::class, 'producto_variante_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsToMany<Attachment, $this>
|
||||||
|
*/
|
||||||
|
public function attachments(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(
|
||||||
|
Attachment::class,
|
||||||
|
'variantes_attachments',
|
||||||
|
'variante_id',
|
||||||
|
'attachment_id'
|
||||||
|
)->withTimestamps();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('variantes_attachments', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('variante_id')
|
||||||
|
->constrained('productos_variantes')
|
||||||
|
->cascadeOnDelete();
|
||||||
|
$table->foreignId('attachment_id')
|
||||||
|
->constrained('attachments')
|
||||||
|
->cascadeOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['variante_id', 'attachment_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('variantes_attachments');
|
||||||
|
}
|
||||||
|
};
|
||||||
93
tests/Feature/Catalog/ProductVariantAttachmentTest.php
Normal file
93
tests/Feature/Catalog/ProductVariantAttachmentTest.php
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Catalog;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
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 ProductVariantAttachmentTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_can_associate_attachments_to_product_variants(): void
|
||||||
|
{
|
||||||
|
// 1. Create Tenant
|
||||||
|
$hdrKey = (string) Str::uuid();
|
||||||
|
$ftrKey = (string) Str::uuid();
|
||||||
|
$headerAttachment = Attachment::create([
|
||||||
|
'key' => $hdrKey,
|
||||||
|
'path' => 'tenants/' . $hdrKey . '.png',
|
||||||
|
'filename' => 'logo_header.png',
|
||||||
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
]);
|
||||||
|
$footerAttachment = Attachment::create([
|
||||||
|
'key' => $ftrKey,
|
||||||
|
'path' => 'tenants/' . $ftrKey . '.png',
|
||||||
|
'filename' => 'logo_footer.png',
|
||||||
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'codigo' => 'acme',
|
||||||
|
'nombre' => 'Acme Inc.',
|
||||||
|
'dominio' => 'acme.com',
|
||||||
|
'primary_color' => '#ffffff',
|
||||||
|
'secondary_color' => '#ffffff',
|
||||||
|
'danger_color' => '#ffffff',
|
||||||
|
'header_footer_bg_color' => '#ffffff',
|
||||||
|
'header_logo_id' => $headerAttachment->id,
|
||||||
|
'footer_logo_id' => $footerAttachment->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 2. Create Product
|
||||||
|
$product = Product::create([
|
||||||
|
'tenant_codigo' => $tenant->codigo,
|
||||||
|
'categoria_id' => 1,
|
||||||
|
'slug' => 'test-product',
|
||||||
|
'nombre' => 'Test Product',
|
||||||
|
'descripcion' => 'A test product description',
|
||||||
|
'precio' => 99.99,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 3. Create Variant
|
||||||
|
$variant = ProductVariant::create([
|
||||||
|
'producto_id' => $product->id,
|
||||||
|
'slug' => 'test-variant-1',
|
||||||
|
'nombre' => 'Test Variant 1',
|
||||||
|
'stock' => 10,
|
||||||
|
'precio' => 99.99,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 4. Create Attachments
|
||||||
|
$attachment1 = Attachment::create([
|
||||||
|
'key' => (string) Str::uuid(),
|
||||||
|
'path' => 'attachments/image1.png',
|
||||||
|
'filename' => 'image1.png',
|
||||||
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$attachment2 = Attachment::create([
|
||||||
|
'key' => (string) Str::uuid(),
|
||||||
|
'path' => 'attachments/image2.png',
|
||||||
|
'filename' => 'image2.png',
|
||||||
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||||
|
'mime_type' => 'image/png',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 5. Associate
|
||||||
|
$variant->attachments()->attach([$attachment1->id, $attachment2->id]);
|
||||||
|
|
||||||
|
// 6. Assert relations
|
||||||
|
$this->assertCount(2, $variant->attachments);
|
||||||
|
$this->assertTrue($variant->attachments->contains($attachment1));
|
||||||
|
$this->assertTrue($variant->attachments->contains($attachment2));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user