feat(category): add banner functionality to categories with related resource and migration tests
This commit is contained in:
@@ -8,6 +8,8 @@ use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Commerce\Catalog\Models\Category;
|
||||
use App\Domains\Commerce\Catalog\Models\Inventory;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use App\Shared\Storage\Services\TemporaryUrlService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -72,6 +74,39 @@ class CategoryDetailTest extends TestCase
|
||||
->assertJsonCount(0, 'data');
|
||||
}
|
||||
|
||||
public function test_it_returns_the_banner_url_only_when_a_banner_exists_and_is_enabled(): void
|
||||
{
|
||||
$tenant = $this->createTenant('category-banner');
|
||||
$category = $this->createCategory($tenant, 'Remeras');
|
||||
$banner = Attachment::query()->create([
|
||||
'path' => 'categories/banner.jpg',
|
||||
'filename' => 'banner.jpg',
|
||||
'type' => 'image',
|
||||
'mime_type' => 'image/jpeg',
|
||||
'extension' => 'jpg',
|
||||
'size' => 100,
|
||||
]);
|
||||
$this->mock(TemporaryUrlService::class, function ($mock): void {
|
||||
$mock->shouldReceive('generate')->once()
|
||||
->with('categories/banner.jpg', 1440)
|
||||
->andReturn([
|
||||
'temporary_url' => 'https://example.com/banner.jpg',
|
||||
'temporary_url_expires_at' => now()->addDay()->toIso8601String(),
|
||||
]);
|
||||
});
|
||||
$url = "/api/tenants/{$tenant->codigo}/categories/{$category->id}";
|
||||
|
||||
$category->update(['banner_enabled' => true]);
|
||||
$this->getJson($url)->assertOk()->assertJsonPath('category.banner_url', null);
|
||||
|
||||
$category->update(['banner_id' => $banner->id, 'banner_enabled' => false]);
|
||||
$this->getJson($url)->assertOk()->assertJsonPath('category.banner_url', null);
|
||||
|
||||
$category->update(['banner_enabled' => true]);
|
||||
$this->getJson($url)->assertOk()
|
||||
->assertJsonPath('category.banner_url', 'https://example.com/banner.jpg');
|
||||
}
|
||||
|
||||
public function test_it_includes_products_from_all_descendant_categories(): void
|
||||
{
|
||||
$tenant = $this->createTenant('category-descendants');
|
||||
|
||||
75
tests/Feature/Migrations/AddBannerToCategoriesTest.php
Normal file
75
tests/Feature/Migrations/AddBannerToCategoriesTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Migrations;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\Category;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AddBannerToCategoriesTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Schema::create('attachments', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
});
|
||||
Schema::create('categorias', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('nombre');
|
||||
$table->boolean('is_enabled')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
DB::table('categorias')->insert(['nombre' => 'Existente']);
|
||||
|
||||
(require database_path('migrations/2026_10_01_000000_add_banner_to_categorias_table.php'))->up();
|
||||
}
|
||||
|
||||
public function test_existing_categories_default_to_no_banner_and_disabled(): void
|
||||
{
|
||||
$category = Category::query()->firstOrFail();
|
||||
|
||||
$this->assertNull($category->banner_id);
|
||||
$this->assertFalse($category->banner_enabled);
|
||||
$this->assertNull($category->banner);
|
||||
}
|
||||
|
||||
public function test_banner_can_be_assigned_and_deleting_it_preserves_the_category(): void
|
||||
{
|
||||
$bannerId = DB::table('attachments')->insertGetId([]);
|
||||
$category = Category::query()->create([
|
||||
'nombre' => 'Con banner',
|
||||
'banner_id' => $bannerId,
|
||||
'banner_enabled' => true,
|
||||
])->fresh();
|
||||
|
||||
$this->assertSame($bannerId, $category->banner_id);
|
||||
$this->assertTrue($category->banner_enabled);
|
||||
$this->assertSame($bannerId, $category->banner->id);
|
||||
|
||||
DB::table('attachments')->where('id', $bannerId)->delete();
|
||||
|
||||
$this->assertNull($category->fresh()->banner_id);
|
||||
$this->assertDatabaseHas('categorias', ['id' => $category->id]);
|
||||
}
|
||||
|
||||
public function test_banner_must_reference_an_existing_attachment(): void
|
||||
{
|
||||
$this->expectException(QueryException::class);
|
||||
|
||||
Category::query()->create(['nombre' => 'Inválida', 'banner_id' => 999]);
|
||||
}
|
||||
|
||||
public function test_migration_can_be_rolled_back_without_removing_categories(): void
|
||||
{
|
||||
(require database_path('migrations/2026_10_01_000000_add_banner_to_categorias_table.php'))->down();
|
||||
|
||||
$this->assertFalse(Schema::hasColumn('categorias', 'banner_id'));
|
||||
$this->assertFalse(Schema::hasColumn('categorias', 'banner_enabled'));
|
||||
$this->assertDatabaseHas('categorias', ['nombre' => 'Existente']);
|
||||
}
|
||||
}
|
||||
37
tests/Unit/Catalog/CategoryResourceTest.php
Normal file
37
tests/Unit/Catalog/CategoryResourceTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Catalog;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\Category;
|
||||
use App\Domains\Commerce\Catalog\Resources\CategoryResource;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use App\Shared\Storage\Services\TemporaryUrlService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CategoryResourceTest extends TestCase
|
||||
{
|
||||
public function test_it_returns_the_banner_url_only_when_the_banner_exists_and_is_enabled(): void
|
||||
{
|
||||
$category = new Category(['nombre' => 'Remeras', 'banner_enabled' => true]);
|
||||
$category->setRelation('banner', null);
|
||||
$this->assertNull(CategoryResource::make($category)->resolve()['banner_url']);
|
||||
|
||||
$category->setRelation('banner', new Attachment(['path' => 'categories/banner.jpg']));
|
||||
$category->banner_enabled = false;
|
||||
$this->mock(TemporaryUrlService::class, function ($mock): void {
|
||||
$mock->shouldReceive('generate')->once()
|
||||
->with('categories/banner.jpg', 1440)
|
||||
->andReturn([
|
||||
'temporary_url' => 'https://example.com/banner.jpg',
|
||||
'temporary_url_expires_at' => now()->addDay()->toIso8601String(),
|
||||
]);
|
||||
});
|
||||
$this->assertNull(CategoryResource::make($category)->resolve()['banner_url']);
|
||||
|
||||
$category->banner_enabled = true;
|
||||
$this->assertSame(
|
||||
'https://example.com/banner.jpg',
|
||||
CategoryResource::make($category)->resolve()['banner_url'],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user