56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Tenant;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Tests\TestCase;
|
|
|
|
abstract class TenantTestCase extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected Tenant $tenant;
|
|
protected User $tenantUser;
|
|
protected string $token;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->tenant = Tenant::factory()->create([
|
|
'name' => 'Test Tenant',
|
|
'slug' => 'test-tenant',
|
|
]);
|
|
|
|
$this->tenantUser = User::factory()->create([
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'tenant_id' => $this->tenant->id,
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
$this->token = 'mock-jwt-token-' . $this->tenant->id . '-' . time();
|
|
}
|
|
|
|
protected function authenticatedRequest($method, $uri, array $data = [], array $headers = [])
|
|
{
|
|
$headers['Authorization'] = 'Bearer ' . $this->token;
|
|
|
|
// Temporarily override the middleware to skip auth and set tenant
|
|
$this->app['router']->pushMiddlewareToGroup('api', MockTenantMiddleware::class, 'mock-tenant');
|
|
|
|
return $this->withHeaders($headers)->json($method, $uri, $data);
|
|
}
|
|
|
|
protected function mockTenantContext()
|
|
{
|
|
$this->actingAs($this->tenantUser);
|
|
|
|
// Set tenant globally for tests
|
|
$this->app->instance('tenant', $this->tenant);
|
|
}
|
|
} |