feat: implement polymorphic relationships for group items to support bundles and product variants
This commit is contained in:
114
tests/Feature/Catalog/GroupItemTest.php
Normal file
114
tests/Feature/Catalog/GroupItemTest.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Catalog;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Bundle\Models\Bundle;
|
||||
use App\Domains\Catalog\Models\Category;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
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 GroupItemTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private FeaturedGroup $featuredGroup;
|
||||
|
||||
private ProductVariant $variant;
|
||||
|
||||
private Bundle $bundle;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$headerAttachment = $this->createAttachment('header.png');
|
||||
$footerAttachment = $this->createAttachment('footer.png');
|
||||
|
||||
$tenant = Tenant::query()->create([
|
||||
'codigo' => 'group-test',
|
||||
'nombre' => 'Group Test',
|
||||
'dominio' => 'group.test',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#28a745',
|
||||
'header_bg_color' => '#444444',
|
||||
'footer_bg_color' => '#555555',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'nombre' => 'Group items',
|
||||
]);
|
||||
|
||||
$product = Product::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'categoria_id' => $category->id,
|
||||
'slug' => 'group-item-product',
|
||||
'nombre' => 'Group Item Product',
|
||||
'precio' => 100,
|
||||
]);
|
||||
|
||||
$this->variant = ProductVariant::query()->create([
|
||||
'producto_id' => $product->id,
|
||||
'stock' => 10,
|
||||
]);
|
||||
|
||||
$this->bundle = Bundle::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'nombre' => 'Group Item Bundle',
|
||||
'precio' => 150,
|
||||
]);
|
||||
|
||||
$this->featuredGroup = FeaturedGroup::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'group_name' => 'Featured',
|
||||
'product_layout' => 'row',
|
||||
'group_order' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_a_group_item_can_reference_a_product_variant(): void
|
||||
{
|
||||
$groupItem = $this->variant->groupItems()->create([
|
||||
'featured_group_id' => $this->featuredGroup->id,
|
||||
'order' => 1,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(ProductVariant::class, $groupItem->groupable);
|
||||
$this->assertTrue($groupItem->groupable->is($this->variant));
|
||||
$this->assertTrue($this->variant->groupItems->first()->is($groupItem));
|
||||
}
|
||||
|
||||
public function test_a_group_item_can_reference_a_bundle(): void
|
||||
{
|
||||
$groupItem = $this->bundle->groupItems()->create([
|
||||
'featured_group_id' => $this->featuredGroup->id,
|
||||
'order' => 2,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(Bundle::class, $groupItem->groupable);
|
||||
$this->assertTrue($groupItem->groupable->is($this->bundle));
|
||||
$this->assertTrue($this->bundle->groupItems->first()->is($groupItem));
|
||||
}
|
||||
|
||||
private function createAttachment(string $filename): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'key' => (string) Str::uuid(),
|
||||
'path' => 'tests/'.$filename,
|
||||
'filename' => $filename,
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user