feat(categories): add category retrieval and pagination for tenant-specific products

This commit is contained in:
2026-07-28 12:20:17 -03:00
parent ff9d7728e8
commit 8dc80411d7
14 changed files with 355 additions and 8 deletions

View File

@@ -0,0 +1,123 @@
<?php
namespace Tests\Feature\Catalog;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CategoryDetailTest extends TestCase
{
use RefreshDatabase;
public function test_it_returns_category_products_paginated_with_the_tenant_search_layout(): void
{
$tenant = $this->createTenant('category-detail');
$tenant->update([
'search_product_layout' => ProductLayout::Row->value,
'search_group_layout' => GroupLayout::SimpleVertical->value,
'search_items_per_page' => 2,
]);
$category = $this->createCategory($tenant, 'Remeras');
$otherCategory = $this->createCategory($tenant, 'Pantalones');
$this->createCatalogItem($tenant, $category, 'Remera C');
$firstItem = $this->createCatalogItem($tenant, $category, 'Remera A');
$secondItem = $this->createCatalogItem($tenant, $category, 'Remera B');
$this->createCatalogItem($tenant, $otherCategory, 'Pantalón');
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}")
->assertOk()
->assertJsonPath('category.id', $category->id)
->assertJsonPath('category.nombre', 'Remeras')
->assertJsonPath('category.categoria_id', null)
->assertJsonPath('layout', ProductLayout::Row->value)
->assertJsonPath('group_layout', GroupLayout::SimpleVertical->value)
->assertJsonPath('meta.current_page', 1)
->assertJsonPath('meta.per_page', 2)
->assertJsonPath('meta.total', 3)
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $firstItem->id)
->assertJsonPath('data.1.id', $secondItem->id)
->assertJsonMissing(['nombre' => 'Pantalón']);
}
public function test_it_returns_an_empty_paginated_response_for_a_category_without_products(): void
{
$tenant = $this->createTenant('empty-category');
$category = $this->createCategory($tenant, 'Sin productos');
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}")
->assertOk()
->assertJsonPath('category.id', $category->id)
->assertJsonPath('meta.total', 0)
->assertJsonPath('meta.current_page', 1)
->assertJsonCount(0, 'data');
}
public function test_it_rejects_categories_from_another_tenant(): void
{
$tenant = $this->createTenant('category-owner');
$otherTenant = $this->createTenant('category-foreign');
$foreignCategory = $this->createCategory($otherTenant, 'Ajena');
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$foreignCategory->id}")
->assertNotFound();
}
public function test_it_validates_the_page(): void
{
$tenant = $this->createTenant('category-page');
$category = $this->createCategory($tenant, 'Remeras');
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$category->id}?page=0")
->assertUnprocessable()
->assertJsonValidationErrors('page');
}
private function createTenant(string $code): Tenant
{
return Tenant::query()->create([
'codigo' => $code,
'nombre' => ucfirst($code),
'dominio' => "{$code}.local",
'primary_color' => '#000000',
'secondary_color' => '#000000',
'danger_color' => '#000000',
'success_color' => '#000000',
'header_bg_color' => '#000000',
'footer_bg_color' => '#000000',
]);
}
private function createCategory(Tenant $tenant, string $name): Category
{
return Category::query()->create([
'tenant_code' => $tenant->codigo,
'nombre' => $name,
]);
}
private function createCatalogItem(
Tenant $tenant,
Category $category,
string $name,
): CatalogItem {
$inventory = Inventory::query()->create(['real_stock' => 10]);
return CatalogItem::query()->create([
'tenant_code' => $tenant->codigo,
'category_id' => $category->id,
'inventory_id' => $inventory->id,
'slug' => str($name)->slug()->toString(),
'nombre' => $name,
'descripcion' => "{$name} description",
'precio' => 100,
]);
}
}

View File

@@ -9,6 +9,7 @@ use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\FeaturedGroup;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Tenant\Models\Tenant;
@@ -155,6 +156,24 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
);
$this->assertSame(9, CatalogItem::query()->where('tenant_code', $tenant->codigo)->count());
$this->assertSame(10, Inventory::query()->count());
$this->assertSame(
['Entradas', 'Estacionamiento', 'Gastronomía'],
Category::query()
->where('tenant_code', $tenant->codigo)
->orderBy('nombre')
->pluck('nombre')
->all(),
);
$this->assertSame(
'Estacionamiento',
CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('slug', 'estacionamiento-auto')
->with('category')
->sole()
->category
->nombre,
);
$featuredGroups = FeaturedGroup::query()
->where('tenant_code', $tenant->codigo)

View File

@@ -7,6 +7,7 @@ use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\FeaturedGroup;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\AttributeSeeder;
@@ -91,5 +92,19 @@ class ProductCatalogFromImagesSeederTest extends TestCase
->pluck('catalog_item_id')
->diff($catalogItemIds),
);
$this->assertSame(
['Accesorios', 'Buzos', 'Pantalones', 'Remeras', 'Zapatillas'],
Category::query()
->where('tenant_code', $tenant->codigo)
->orderBy('nombre')
->pluck('nombre')
->all(),
);
$this->assertFalse(
CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->whereHas('category', fn ($query) => $query->where('tenant_code', '!=', $tenant->codigo))
->exists(),
);
}
}

View File

@@ -4,6 +4,7 @@ namespace Tests\Feature\Tenant;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Models\Category;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -95,6 +96,45 @@ class BootstrapTenantControllerTest extends TestCase
->assertJsonPath('data.dominio', 'acme.com');
}
public function test_it_returns_only_the_tenant_categories_in_the_bootstrap(): void
{
$tenant = $this->createTenant();
$otherTenant = $this->createTenant([
'codigo' => 'globex',
'nombre' => 'Globex',
'dominio' => 'globex.com',
]);
$parent = Category::query()->create([
'tenant_code' => $tenant->codigo,
'nombre' => 'Remeras',
]);
Category::query()->create([
'tenant_code' => $tenant->codigo,
'categoria_id' => $parent->id,
'nombre' => 'Manga corta',
]);
Category::query()->create([
'tenant_code' => $otherTenant->codigo,
'nombre' => 'Otra categoría',
]);
Category::query()->create([
'tenant_code' => null,
'nombre' => 'Global',
]);
$this->getJson('/api/tenants/bootstrap/acme.com')
->assertOk()
->assertJsonCount(1, 'data.categories')
->assertJsonPath('data.categories.0.nombre', 'Remeras')
->assertJsonCount(1, 'data.categories.0.subcategories')
->assertJsonPath('data.categories.0.subcategories.0.nombre', 'Manga corta')
->assertJsonPath('data.categories.0.subcategories.0.subcategories', [])
->assertJsonMissingPath('data.categories.0.categoria_id')
->assertJsonMissing(['nombre' => 'Otra categoría'])
->assertJsonMissing(['nombre' => 'Global']);
}
public function test_it_returns_not_found_when_the_domain_does_not_exist(): void
{
$response = $this->getJson('/api/tenants/bootstrap/missing.example');