test set order migration

This commit is contained in:
Yisroel Baum 2026-07-31 10:56:25 +03:00
parent 99ceeff5b8
commit 7579c3c1b3
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,86 @@
<?php
namespace Tests\Feature;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class SetOrderingMigrationTest extends TestCase
{
use RefreshDatabase;
private const MIGRATION_PATH =
'migrations/2026_07_31_000000_add_sort_order_to_sets_table.php';
public function testBackfillsExistingSetsInCurrentDisplayOrder(): void
{
$migration = $this->runMigrationAgainstLegacySets();
$this->assertSame(
[2, 7],
DB::table('sets')->orderBy('sort_order')->pluck('id')->all(),
);
$this->assertSame(
[1, 2],
DB::table('sets')
->orderBy('sort_order')
->pluck('sort_order')
->all(),
);
$this->expectException(QueryException::class);
DB::table('sets')->insert([
'id' => 9,
'name' => 'Unordered Set',
'description' => 'A set without an explicit position',
'icon_image_url' => '/assets/unordered.png',
]);
}
public function testRollbackRemovesOrderWithoutDeletingSets(): void
{
$migration = $this->runMigrationAgainstLegacySets();
$migration->down();
$this->assertFalse(Schema::hasColumn('sets', 'sort_order'));
$this->assertSame(
[2, 7],
DB::table('sets')->orderBy('id')->pluck('id')->all(),
);
}
private function runMigrationAgainstLegacySets(): object
{
if (Schema::hasColumn('sets', 'sort_order')) {
Schema::table('sets', function (Blueprint $table): void {
$table->dropColumn('sort_order');
});
}
DB::table('sets')->delete();
DB::table('sets')->insert([
[
'id' => 7,
'name' => 'Later Set',
'description' => 'Created later',
'icon_image_url' => '/assets/later.png',
],
[
'id' => 2,
'name' => 'Earlier Set',
'description' => 'Created earlier',
'icon_image_url' => '/assets/earlier.png',
],
]);
$migration = require database_path(self::MIGRATION_PATH);
$migration->up();
return $migration;
}
}