1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:24:04 +00:00

Automatic mode

This commit is contained in:
Samuel Štancl 2020-05-10 23:47:11 +02:00
parent 2492345280
commit 73fc525126
16 changed files with 154 additions and 40 deletions

View file

@ -2,31 +2,85 @@
namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Tests\TestCase;
class AutomaticModeTest extends TestCase
{
/** @test */
public function custom_bootstrappers_can_be_registered()
public function setUp(): void
{
parent::setUp();
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}
/** @test */
public function context_is_switched_when_tenancy_is_initialized()
{
config(['tenancy.bootstrappers' => [
MyBootstrapper::class,
]]);
$tenant = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
}
/** @test */
public function context_is_reverted_when_tenancy_is_ended()
{
$this->context_is_switched_when_tenancy_is_initialized();
tenancy()->end();
$this->assertSame(true, app('tenancy_ended'));
}
/** @test */
public function context_is_switched_when_tenancy_is_reinitialized()
{
config(['tenancy.bootstrappers' => [
MyBootstrapper::class,
]]);
$tenant = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
$tenant2 = Tenant::create([
'id' => 'foobar',
]);
tenancy()->initialize($tenant2);
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
}
}
class MyBootstrapper implements TenancyBootstrapper
{
public function start(\Stancl\Tenancy\Contracts\Tenant $tenant)
{
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
}
public function end()
{
app()->instance('tenancy_ended', true);
}
}

View file

@ -0,0 +1 @@
// test DB creation, migration, seeding

View file

@ -2,6 +2,7 @@
namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
@ -10,6 +11,7 @@ use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase;
use Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator;
use Stancl\Tenancy\Contracts;
class TenantModelTest extends TestCase
{
@ -110,12 +112,43 @@ class TenantModelTest extends TestCase
/** @test */
public function custom_tenant_model_can_be_used()
{
$tenant = MyTenant::create();
tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof MyTenant);
}
/** @test */
public function custom_tenant_model_that_doesnt_extend_vendor_Tenant_model_can_be_used()
{
$tenant = AnotherTenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof AnotherTenant);
}
}
class MyTenant extends Tenant
{
protected $table = 'tenants';
}
class AnotherTenant extends Model implements Contracts\Tenant
{
protected $guarded = [];
protected $table = 'tenants';
public function getTenantKeyName(): string
{
return 'id';
}
public function getTenantKey(): string
{
return $this->getAttribute('id');
}
}