incr/tests/Feature/DropAssetsMigrationTest.php

78 lines
2.5 KiB
PHP
Raw Normal View History

<?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 DropAssetsMigrationTest 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_000003_drop_assets_and_pricing.php';
}
public function test_the_migrated_schema_has_no_asset_or_price_columns(): void
{
$this->assertFalse(Schema::hasTable('assets'));
$this->assertFalse(Schema::hasTable('asset_prices'));
$this->assertFalse(Schema::hasColumn('trackers', 'asset_id'));
$this->assertFalse(Schema::hasColumn('trackers', 'price_tracking_enabled'));
}
public function test_down_restores_the_tables_and_columns(): void
{
$this->migration()->down();
$this->assertTrue(Schema::hasTable('assets'));
$this->assertTrue(Schema::hasTable('asset_prices'));
$this->assertTrue(Schema::hasColumn('trackers', 'asset_id'));
$this->assertTrue(Schema::hasColumn('trackers', 'price_tracking_enabled'));
}
public function test_up_drops_them_again_respecting_foreign_keys(): void
{
$this->migration()->down();
$this->migration()->up();
$this->assertFalse(Schema::hasTable('assets'));
$this->assertFalse(Schema::hasTable('asset_prices'));
$this->assertFalse(Schema::hasColumn('trackers', 'asset_id'));
$this->assertFalse(Schema::hasColumn('trackers', 'price_tracking_enabled'));
}
public function test_dropping_assets_does_not_disturb_the_counter(): void
{
$this->migration()->down();
$userId = DB::table('users')->insertGetId([
'name' => 'Test',
'email' => 'assets@example.test',
'password' => 'x',
'created_at' => now(),
'updated_at' => now(),
]);
$trackerId = DB::table('trackers')->insertGetId([
'user_id' => $userId,
'label' => 'Counter',
'unit' => 'units',
'count' => 31,
'created_at' => now(),
'updated_at' => now(),
]);
$this->migration()->up();
$this->assertSame(31, (int) DB::table('trackers')->where('id', $trackerId)->value('count'));
}
}