1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 13:14:05 +00:00

Improve the tests for central helper

Signed-off-by: michael lundbøl <michael.lundboel@gmail.com>
This commit is contained in:
michael lundbøl 2020-10-24 16:53:37 +02:00
parent 49e3273aa5
commit b33366e0c0
No known key found for this signature in database
GPG key ID: 213C976E2CFD1CAF

View file

@ -75,9 +75,6 @@ class AutomaticModeTest extends TestCase
/** @test */ /** @test */
public function running_the_central_tenancy_helper_with_tenant_already_initialized() public function running_the_central_tenancy_helper_with_tenant_already_initialized()
{ {
MyBootstrapper::$revertedCallCount = 0;
GlobalRun::$count = 0;
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [
MyBootstrapper::class, MyBootstrapper::class,
]]); ]]);
@ -91,38 +88,44 @@ class AutomaticModeTest extends TestCase
$this->assertSame('acme', app('tenancy_initialized_for_tenant')); $this->assertSame('acme', app('tenancy_initialized_for_tenant'));
tenancy()->central(function () { tenancy()->central(function () {
GlobalRun::$count = 1; $this->assertTrue(app('tenancy_ended'));
$this->assertEmpty(app('tenancy_initialized_for_tenant'));
}); });
$this->assertSame(1, MyBootstrapper::$revertedCallCount);
$this->assertSame(1, GlobalRun::$count);
$this->assertSame('acme', app('tenancy_initialized_for_tenant')); $this->assertSame('acme', app('tenancy_initialized_for_tenant'));
} }
/** @test */ /** @test */
public function running_the_central_tenancy_helper_with_tenant_not_already_initialized() public function running_the_central_tenancy_helper_with_tenant_not_already_initialized()
{ {
MyBootstrapper::$revertedCallCount = 0;
GlobalRun::$count = 0;
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [
MyBootstrapper::class, MyBootstrapper::class,
]]); ]]);
tenancy()->central(function () { $runned = 0;
GlobalRun::$count = 1;
$this->assertFalse(tenancy()->initialized);
$this->assertFalse(app()->bound('tenancy_ended'));
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));
tenancy()->central(function () use (&$runned) {
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));
$this->assertFalse(app()->bound('tenancy_ended'));
$this->assertFalse(tenancy()->initialized);
$runned = 1;
}); });
$this->assertSame(0, MyBootstrapper::$revertedCallCount); $this->assertSame(1, $runned);
$this->assertSame(1, GlobalRun::$count);
$this->assertFalse(app()->bound('tenancy_ended'));
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant')); $this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));
$this->assertFalse(tenancy()->initialized);
} }
} }
class MyBootstrapper implements TenancyBootstrapper class MyBootstrapper implements TenancyBootstrapper
{ {
public static $revertedCallCount = 0;
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant) public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
{ {
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey()); app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
@ -130,12 +133,7 @@ class MyBootstrapper implements TenancyBootstrapper
public function revert() public function revert()
{ {
static::$revertedCallCount++; app()->instance('tenancy_initialized_for_tenant', '');
app()->instance('tenancy_ended', true); app()->instance('tenancy_ended', true);
} }
} }
class GlobalRun {
public static $count = 0;
}