Files
Wian Drs 1e80be180e
tests / ci (push) Has been cancelled
Initial commit device-management
2026-08-25 09:42:00 +07:00

37 lines
1014 B
PHP

<?php
namespace Tests\Feature;
use App\Models\Tenant;
use App\Models\User;
use App\Support\TenantContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TenantIsolationTest extends TestCase
{
use RefreshDatabase;
public function test_user_queries_are_scoped_to_current_tenant(): void
{
$tenantA = Tenant::factory()->create();
$tenantB = Tenant::factory()->create();
User::factory()->for($tenantA)->create();
User::factory()->for($tenantB)->create();
app(TenantContext::class)->set($tenantA->id);
$this->assertCount(1, User::all());
$this->assertTrue(User::first()->tenant->is($tenantA));
}
public function test_tenant_id_is_assigned_from_context_on_create(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant->id);
$user = User::factory()->create(['tenant_id' => null]);
$this->assertSame($tenant->id, $user->tenant_id);
}
}