feat(seeder): add Mutual SMEP category seeder and test cases

This commit is contained in:
2026-09-24 11:42:45 -03:00
parent 2db83271fa
commit 6b0efbc6ee
4 changed files with 384 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
<?php
namespace Tests\Feature\Seeders;
use App\Domains\Commerce\Catalog\Models\Category;
use Database\Seeders\MutualSmepCategorySeeder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class MutualSmepCategorySeederTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Schema::create('tenants', function (Blueprint $table): void {
$table->string('codigo')->primary();
});
Schema::create('categorias', function (Blueprint $table): void {
$table->id();
$table->string('tenant_code')->nullable();
$table->unsignedBigInteger('categoria_id')->nullable();
$table->string('nombre');
$table->boolean('is_enabled')->default(true);
$table->timestamps();
});
}
protected function tearDown(): void
{
Schema::dropIfExists('categorias');
Schema::dropIfExists('tenants');
parent::tearDown();
}
public function test_it_creates_the_mutual_smep_category_hierarchy_idempotently(): void
{
$this->createTenant('mutual_smep');
$this->seed(MutualSmepCategorySeeder::class);
$this->seed(MutualSmepCategorySeeder::class);
$parents = Category::query()
->where('tenant_code', 'mutual_smep')
->whereNull('categoria_id')
->with('subCategories')
->get()
->keyBy('nombre');
$this->assertCount(6, $parents);
$this->assertCount(37, Category::query()->where('tenant_code', 'mutual_smep')->get());
$this->assertEqualsCanonicalizing([
'Colchones',
'Bases y Sommiers',
'Acolchados y Edredones',
'Frazadas y Mantas',
'Infantil y Cuna',
], $parents->get('Dormitorio y Blanco')->subCategories->pluck('nombre')->all());
$this->assertEqualsCanonicalizing([
'Celulares',
'Notebooks',
'Smart TV',
'Audio',
'Accesorios de Computación',
'Soportes para TV',
], $parents->get('Tecnología')->subCategories->pluck('nombre')->all());
}
public function test_down_removes_only_the_seeded_mutual_smep_hierarchy(): void
{
$this->createTenant('mutual_smep');
$this->createTenant('other');
Category::query()->create([
'tenant_code' => 'other',
'nombre' => 'Tecnología',
]);
$seeder = app(MutualSmepCategorySeeder::class);
$seeder->run();
$seeder->down();
$this->assertDatabaseMissing('categorias', [
'tenant_code' => 'mutual_smep',
]);
$this->assertDatabaseHas('categorias', [
'tenant_code' => 'other',
'nombre' => 'Tecnología',
]);
}
public function test_the_data_migration_runs_and_rolls_back_the_seeder(): void
{
$this->createTenant('mutual_smep');
$migration = require database_path(
'migrations/2026_09_24_010000_seed_mutual_smep_categories.php'
);
$migration->up();
$this->assertCount(37, Category::query()->where('tenant_code', 'mutual_smep')->get());
$migration->down();
$this->assertDatabaseMissing('categorias', [
'tenant_code' => 'mutual_smep',
]);
}
private function createTenant(string $code): void
{
DB::table('tenants')->insert([
'codigo' => $code,
]);
}
}