87 lines
2.7 KiB
PHP
87 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use Illuminate\Database\Migrations\Migration;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Facades\Schema;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class DropUsersMigrationTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
// Each require returns a fresh anonymous-class instance; there is no name to collide.
|
||
|
|
private function migration(): Migration
|
||
|
|
{
|
||
|
|
return require __DIR__.'/../../database/migrations/2026_08_15_000004_drop_users_and_sessions.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_the_migrated_schema_has_no_user_tables(): void
|
||
|
|
{
|
||
|
|
$this->assertFalse(Schema::hasTable('users'));
|
||
|
|
$this->assertFalse(Schema::hasTable('sessions'));
|
||
|
|
$this->assertFalse(Schema::hasTable('password_reset_tokens'));
|
||
|
|
$this->assertFalse(Schema::hasColumn('trackers', 'user_id'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_down_restores_the_tables_and_column(): void
|
||
|
|
{
|
||
|
|
$this->migration()->down();
|
||
|
|
|
||
|
|
$this->assertTrue(Schema::hasTable('users'));
|
||
|
|
$this->assertTrue(Schema::hasTable('sessions'));
|
||
|
|
$this->assertTrue(Schema::hasTable('password_reset_tokens'));
|
||
|
|
$this->assertTrue(Schema::hasColumn('trackers', 'user_id'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_restored_user_id_is_nullable_so_existing_trackers_survive(): void
|
||
|
|
{
|
||
|
|
DB::table('trackers')->insert([
|
||
|
|
'label' => 'Counter',
|
||
|
|
'unit' => 'units',
|
||
|
|
'count' => 3,
|
||
|
|
'created_at' => now(),
|
||
|
|
'updated_at' => now(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->migration()->down();
|
||
|
|
|
||
|
|
$nullable = DB::selectOne(
|
||
|
|
'select is_nullable from information_schema.columns
|
||
|
|
where table_schema = database() and table_name = ? and column_name = ?',
|
||
|
|
['trackers', 'user_id']
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertSame('YES', $nullable->is_nullable ?? $nullable->IS_NULLABLE);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_up_drops_them_again_respecting_foreign_keys(): void
|
||
|
|
{
|
||
|
|
$this->migration()->down();
|
||
|
|
$this->migration()->up();
|
||
|
|
|
||
|
|
$this->assertFalse(Schema::hasTable('users'));
|
||
|
|
$this->assertFalse(Schema::hasColumn('trackers', 'user_id'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_the_counter_survives_the_round_trip(): void
|
||
|
|
{
|
||
|
|
$trackerId = DB::table('trackers')->insertGetId([
|
||
|
|
'label' => 'Counter',
|
||
|
|
'unit' => 'units',
|
||
|
|
'count' => 77,
|
||
|
|
'created_at' => now(),
|
||
|
|
'updated_at' => now(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->migration()->down();
|
||
|
|
$this->migration()->up();
|
||
|
|
|
||
|
|
$this->assertSame(77, (int) DB::table('trackers')->where('id', $trackerId)->value('count'));
|
||
|
|
}
|
||
|
|
}
|