forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenantStorageTest.php
194 lines (151 loc) · 5.95 KB
/
TenantStorageTest.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\StorageDrivers\Database\TenantRepository;
use Stancl\Tenancy\Tenant;
class TenantStorageTest extends TestCase
{
/** @test */
public function deleting_a_tenant_works()
{
$abc = Tenant::new()->withDomains(['abc.localhost'])->save();
$exists = function () use ($abc) {
return tenancy()->all()->contains(function ($tenant) use ($abc) {
return $tenant->id === $abc->id;
});
};
$this->assertTrue($exists());
$abc->delete();
$this->assertFalse($exists());
}
/** @test */
public function set_is_a_working_alias_for_put()
{
tenant()->set('foo', 'bar');
$this->assertSame('bar', tenant()->get('foo'));
}
/** @test */
public function put_works_with_key_and_value_as_separate_args()
{
tenant()->put('foo', 'bar');
$this->assertSame('bar', tenant()->get('foo'));
}
/** @test */
public function put_works_with_key_and_value_as_a_single_arg()
{
$keys = ['foo', 'abc'];
$vals = ['bar', 'xyz'];
$data = array_combine($keys, $vals);
tenant()->put($data);
$this->assertSame($data, tenant()->get($keys));
}
/** @test */
public function put_on_the_current_tenant_pushes_the_value_into_the_tenant_property_array()
{
tenant()->put('foo', 'bar');
$this->assertSame('bar', tenancy()->getTenant('foo'));
}
/** @test */
public function arrays_can_be_stored()
{
tenant()->put('foo', [1, 2]);
$this->assertSame([1, 2], tenant()->get('foo'));
}
/** @test */
public function associative_arrays_can_be_stored()
{
$data = ['a' => 'b', 'c' => 'd'];
tenant()->put('foo', $data);
$this->assertSame($data, tenant()->get('foo'));
}
/** @test */
public function correct_storage_driver_is_used()
{
if (config('tenancy.storage_driver') == 'db') {
$this->assertSame('DatabaseStorageDriver', class_basename(tenancy()->storage));
} elseif (config('tenancy.storage_driver') == 'redis') {
$this->assertSame('RedisStorageDriver', class_basename(tenancy()->storage));
} else {
dd(class_basename(config('tenancy.storage_driver')));
}
}
/** @test */
public function data_is_stored_with_correct_data_types()
{
tenant()->put('someBool', false);
$this->assertSame('boolean', gettype(tenant()->get('someBool')));
$this->assertSame('boolean', gettype(tenant()->get(['someBool'])['someBool']));
tenant()->put('someInt', 5);
$this->assertSame('integer', gettype(tenant()->get('someInt')));
$this->assertSame('integer', gettype(tenant()->get(['someInt'])['someInt']));
tenant()->put('someDouble', 11.40);
$this->assertSame('double', gettype(tenant()->get('someDouble')));
$this->assertSame('double', gettype(tenant()->get(['someDouble'])['someDouble']));
tenant()->put('string', 'foo');
$this->assertSame('string', gettype(tenant()->get('string')));
$this->assertSame('string', gettype(tenant()->get(['string'])['string']));
}
/** @test */
public function tenant_repository_uses_correct_connection()
{
config(['database.connections.foo' => config('database.connections.sqlite')]);
config(['tenancy.storage_drivers.db.connection' => 'foo']);
$this->assertSame('foo', app(TenantRepository::class)->database->getName());
}
/** @test */
public function retrieving_data_without_cache_works()
{
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
tenant()->put('foo', 'bar');
$this->assertSame('bar', tenant()->get('foo'));
$this->assertSame(['foo' => 'bar'], tenant()->get(['foo']));
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', tenant()->get('foo'));
$this->assertSame(['foo' => 'bar'], tenant()->get(['foo']));
}
/** @test */
public function custom_columns_work_with_db_storage_driver()
{
if (config('tenancy.storage_driver') != 'db') {
$this->markTestSkipped();
}
tenancy()->endTenancy();
$this->loadMigrationsFrom([
'--path' => __DIR__ . '/Etc',
'--database' => 'central',
]);
config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom
config(['tenancy.storage_drivers.db.custom_columns' => [
'foo',
]]);
tenancy()->create(['foo.localhost']);
tenancy()->init('foo.localhost');
tenant()->put('foo', '111');
$this->assertSame('111', tenant()->get('foo'));
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
}
/** @test */
public function custom_columns_can_be_used_on_tenant_create()
{
if (config('tenancy.storage_driver') != 'db') {
$this->markTestSkipped();
}
tenancy()->endTenancy();
$this->loadMigrationsFrom([
'--path' => __DIR__ . '/Etc',
'--database' => 'central',
]);
config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom
config(['tenancy.storage_drivers.db.custom_columns' => [
'foo',
]]);
tenancy()->create(['foo.localhost'], ['foo' => 'bar', 'abc' => 'xyz']);
tenancy()->init('foo.localhost');
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
}
}