mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:34:03 +00:00
Convert test cases
This commit is contained in:
parent
405b91085e
commit
a8dcd0dfc9
27 changed files with 3059 additions and 3658 deletions
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
|
|
@ -12,124 +10,104 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class AutomaticModeTest extends TestCase
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
test('context is switched when tenancy is initialized', function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||
});
|
||||
|
||||
test('context is reverted when tenancy is ended', function () {
|
||||
$this->context_is_switched_when_tenancy_is_initialized();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$this->assertSame(true, app('tenancy_ended'));
|
||||
});
|
||||
|
||||
test('context is switched when tenancy is reinitialized', function () {
|
||||
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'));
|
||||
});
|
||||
|
||||
test('central helper runs callbacks in the central state', function () {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
$this->assertSame(null, tenant());
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
});
|
||||
|
||||
test('central helper returns the value from the callback', function () {
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
$this->assertSame('foo', tenancy()->central(function () {
|
||||
return 'foo';
|
||||
}));
|
||||
});
|
||||
|
||||
test('central helper reverts back to tenant context', function () {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
});
|
||||
|
||||
test('central helper doesnt change tenancy state when called in central context', function () {
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
});
|
||||
|
||||
// Helpers
|
||||
function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||
{
|
||||
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'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_runs_callbacks_in_the_central_state()
|
||||
{
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
$this->assertSame(null, tenant());
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_returns_the_value_from_the_callback()
|
||||
{
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
$this->assertSame('foo', tenancy()->central(function () {
|
||||
return 'foo';
|
||||
}));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_reverts_back_to_tenant_context()
|
||||
{
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_doesnt_change_tenancy_state_when_called_in_central_context()
|
||||
{
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
}
|
||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||
}
|
||||
|
||||
class MyBootstrapper implements TenancyBootstrapper
|
||||
function revert()
|
||||
{
|
||||
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||
{
|
||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
app()->instance('tenancy_ended', true);
|
||||
}
|
||||
app()->instance('tenancy_ended', true);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue