feat(category): add banner functionality to categories with related resource and migration tests

This commit is contained in:
2026-10-01 14:29:31 -03:00
parent b56b139631
commit 95442d4e0b
7 changed files with 212 additions and 5 deletions

View 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']);
}
}