0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

stancl/tenancyのテスト作成

Posted at

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();

でテナントのテストをする

中央ドメインのテストはまだやってないのでわからない

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?