forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomaticModeTest.php
135 lines (103 loc) · 3.31 KB
/
AutomaticModeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant;
class AutomaticModeTest extends TestCase
{
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());
}
}
class MyBootstrapper implements TenancyBootstrapper
{
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
{
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
}
public function revert()
{
app()->instance('tenancy_ended', true);
}
}