58 lines
2.2 KiB
PHP
58 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Migrations;
|
|
|
|
use App\Domains\Core\Administrator\Resources\AdministratorResource;
|
|
use App\Domains\Core\Auth\Models\User;
|
|
use App\Domains\Core\Auth\Resources\UserResource;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Tests\TestCase;
|
|
|
|
class AddEventIdToUsersTest extends TestCase
|
|
{
|
|
public function test_event_assignment_preserves_existing_users_and_restricts_event_deletion(): void
|
|
{
|
|
Schema::create('events', function (Blueprint $table): void {
|
|
$table->id();
|
|
});
|
|
Schema::create('users', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('tenant_codigo')->nullable();
|
|
$table->softDeletes();
|
|
$table->timestamps();
|
|
});
|
|
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'legacy']);
|
|
DB::table('events')->insert(['id' => 42]);
|
|
|
|
$migration = require database_path('migrations/2026_10_01_000000_add_event_id_to_users_table.php');
|
|
$migration->up();
|
|
|
|
$legacy = User::query()->findOrFail(1);
|
|
$this->assertNull($legacy->event_id);
|
|
$this->assertNull($legacy->event);
|
|
$this->assertSame('legacy', $legacy->tenant_codigo);
|
|
|
|
$legacy->update(['event_id' => '42']);
|
|
$user = $legacy->fresh();
|
|
$this->assertSame(42, $user->event_id);
|
|
$this->assertSame(42, $user->event->id);
|
|
$this->assertSame(42, UserResource::make($user)->resolve(new Request)['event_id']);
|
|
$this->assertSame(42, AdministratorResource::make($user)->resolve(new Request)['event_id']);
|
|
|
|
try {
|
|
DB::table('events')->where('id', 42)->delete();
|
|
$this->fail('An assigned event must not be deleted.');
|
|
} catch (QueryException $exception) {
|
|
$this->assertStringContainsString('FOREIGN KEY', $exception->getMessage());
|
|
}
|
|
|
|
$migration->down();
|
|
$this->assertFalse(Schema::hasColumn('users', 'event_id'));
|
|
$this->assertSame('legacy', DB::table('users')->where('id', 1)->value('tenant_codigo'));
|
|
}
|
|
}
|