@@ -0,0 +1,36 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user