forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenantManagerEventsTest.php
138 lines (109 loc) · 4.25 KB
/
TenantManagerEventsTest.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
136
137
138
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant;
use Tenancy;
class TenantManagerEventsTest extends TestCase
{
public $autoInitTenancy = false;
/** @test */
public function bootstrapping_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function bootstrapped_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function ending_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame(null, config('tenancy.foo'));
tenancy()->endTenancy();
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function ended_event_works()
{
Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('ended', function ($tenantManager) {
config(['tenancy.foo' => 'bar']);
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame(null, config('tenancy.foo'));
tenancy()->endTenancy();
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function database_can_be_reconnected_using_event_hooks()
{
config(['database.connections.tenantabc' => [
'driver' => 'sqlite',
'database' => database_path('some_special_database.sqlite'),
]]);
$id = Tenant::create('abc.localhost')['id'];
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->getTenant()['id'] === $id) {
$tenancy->database->switchConnection('tenantabc');
return ['database'];
}
});
$this->assertNotSame('tenantabc', \DB::connection()->getConfig()['name']);
tenancy()->init('abc.localhost');
$this->assertSame('tenantabc', \DB::connection()->getConfig()['name']);
}
/** @test */
public function database_cannot_be_reconnected_without_using_prevents()
{
config(['database.connections.tenantabc' => [
'driver' => 'sqlite',
'database' => database_path('some_special_database.sqlite'),
]]);
$id = Tenant::create('abc.localhost')['id'];
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->getTenant()['id'] === $id) {
$tenancy->database->switchConnection('tenantabc');
// return ['database'];
}
});
$this->assertNotSame('tenantabc', \DB::connection()->getConfig()['name']);
tenancy()->init('abc.localhost');
$this->assertSame('tenant', \DB::connection()->getConfig()['name']);
}
/** @test */
public function tenant_is_persisted_before_the_created_hook_is_called()
{
$was_persisted = false;
Tenancy::eventListener('tenant.created', function ($tenancy, $tenant) use (&$was_persisted) {
$was_persisted = $tenant->persisted;
});
Tenant::new()->save();
$this->assertTrue($was_persisted);
}
}