TestCase
下記でテナントschemaの削除までできる
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use App\Models\Tenant;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use Tests\TestMiddleware\SimulateNetworkIssues;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $tenancy = false;
protected $currentTenant;
public function setUp(): void
{
parent::setUp();
if ($this->tenancy) {
$this->initializeTenancy();
$this->initializeUser();
}
}
protected function tearDown(): void
{
if ($this->tenancy && $this->currentTenant) {
// テナント接続を終了
tenancy()->end();
// テナントを削除(これによりスキーマも削除される)
$this->currentTenant->delete();
}
parent::tearDown();
}
public function initializeTenancy()
{
$this->currentTenant = Tenant::create();
tenancy()->initialize($this->currentTenant);
$tenant = $this->currentTenant;
}
public function initializeUser()
{
User::factory()->create();
}
}
Feature
下記と
protected $tenancy = true;
下記
$tenant = Tenant::first();
でテナントのテストをする
中央ドメインのテストはまだやってないのでわからない