forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomaticModeTest.php
119 lines (88 loc) · 2.91 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
<?php
declare(strict_types=1);
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;
beforeEach(function () {
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
test('context is switched when tenancy is initialized', function () {
contextIsSwitchedWhenTenancyInitialized();
});
test('context is reverted when tenancy is ended', function () {
contextIsSwitchedWhenTenancyInitialized();
tenancy()->end();
expect(app('tenancy_ended'))->toBe(true);
});
test('context is switched when tenancy is reinitialized', function () {
config(['tenancy.bootstrappers' => [
MyBootstrapper::class,
]]);
$tenant = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
$tenant2 = Tenant::create([
'id' => 'foobar',
]);
tenancy()->initialize($tenant2);
expect(app('tenancy_initialized_for_tenant'))->toBe('foobar');
});
test('central helper runs callbacks in the central state', function () {
tenancy()->initialize($tenant = Tenant::create());
tenancy()->central(function () {
expect(tenant())->toBe(null);
});
expect(tenant())->toBe($tenant);
});
test('central helper returns the value from the callback', function () {
tenancy()->initialize(Tenant::create());
pest()->assertSame('foo', tenancy()->central(function () {
return 'foo';
}));
});
test('central helper reverts back to tenant context', function () {
tenancy()->initialize($tenant = Tenant::create());
tenancy()->central(function () {
//
});
expect(tenant())->toBe($tenant);
});
test('central helper doesnt change tenancy state when called in central context', function () {
expect(tenancy()->initialized)->toBeFalse();
expect(tenant())->toBeNull();
tenancy()->central(function () {
//
});
expect(tenancy()->initialized)->toBeFalse();
expect(tenant())->toBeNull();
});
// todo@tests
function contextIsSwitchedWhenTenancyInitialized()
{
config(['tenancy.bootstrappers' => [
MyBootstrapper::class,
]]);
$tenant = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
}
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);
}
}