mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:54:03 +00:00
parent
f950265f58
commit
e7b5a77a6b
3 changed files with 153 additions and 0 deletions
151
tests/DataSeparationTest.php
Normal file
151
tests/DataSeparationTest.php
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DataSeparationTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = false;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/** @test */
|
||||
public function databases_are_separated()
|
||||
{
|
||||
$tenant1 = tenancy()->create('tenant1.localhost');
|
||||
$tenant2 = tenancy()->create('tenant2.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']]
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
User::create([
|
||||
'name' => 'foo',
|
||||
'email' => 'foo@bar.com',
|
||||
'email_verified_at' => now(),
|
||||
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
||||
'remember_token' => Str::random(10),
|
||||
]);
|
||||
$this->assertSame('foo', User::first()->name);
|
||||
|
||||
tenancy()->init('tenant2.localhost');
|
||||
$this->assertSame(null, User::first());
|
||||
|
||||
User::create([
|
||||
'name' => 'xyz',
|
||||
'email' => 'xyz@bar.com',
|
||||
'email_verified_at' => now(),
|
||||
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
||||
'remember_token' => Str::random(10),
|
||||
]);
|
||||
|
||||
$this->assertSame('xyz', User::first()->name);
|
||||
$this->assertSame('xyz@bar.com', User::first()->email);
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
$this->assertSame('foo', User::first()->name);
|
||||
$this->assertSame('foo@bar.com', User::first()->email);
|
||||
|
||||
$tenant3 = tenancy()->create('tenant3.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']]
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant3.localhost');
|
||||
$this->assertSame(null, User::first());
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
DB::table('users')->where('id', 1)->update(['name' => 'xxx']);
|
||||
$this->assertSame('xxx', User::first()->name);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function redis_is_separated()
|
||||
{
|
||||
tenancy()->create('tenant1.localhost');
|
||||
tenancy()->create('tenant2.localhost');
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
Redis::set('foo', 'bar');
|
||||
$this->assertSame('bar', Redis::get('foo'));
|
||||
|
||||
tenancy()->init('tenant2.localhost');
|
||||
$this->assertSame(null, Redis::get('foo'));
|
||||
Redis::set('foo', 'xyz');
|
||||
Redis::set('abc', 'def');
|
||||
$this->assertSame('xyz', Redis::get('foo'));
|
||||
$this->assertSame('def', Redis::get('abc'));
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
$this->assertSame('bar', Redis::get('foo'));
|
||||
$this->assertSame(null, Redis::get('abc'));
|
||||
|
||||
tenancy()->create('tenant3.localhost');
|
||||
tenancy()->init('tenant3.localhost');
|
||||
$this->assertSame(null, Redis::get('foo'));
|
||||
$this->assertSame(null, Redis::get('abc'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function cache_is_separated()
|
||||
{
|
||||
tenancy()->create('tenant1.localhost');
|
||||
tenancy()->create('tenant2.localhost');
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
Cache::put('foo', 'bar', 60);
|
||||
$this->assertSame('bar', Cache::get('foo'));
|
||||
|
||||
tenancy()->init('tenant2.localhost');
|
||||
$this->assertSame(null, Cache::get('foo'));
|
||||
Cache::put('foo', 'xyz', 60);
|
||||
Cache::put('abc', 'def', 60);
|
||||
$this->assertSame('xyz', Cache::get('foo'));
|
||||
$this->assertSame('def', Cache::get('abc'));
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
$this->assertSame('bar', Cache::get('foo'));
|
||||
$this->assertSame(null, Cache::get('abc'));
|
||||
|
||||
tenancy()->create('tenant3.localhost');
|
||||
tenancy()->init('tenant3.localhost');
|
||||
$this->assertSame(null, Cache::get('foo'));
|
||||
$this->assertSame(null, Cache::get('abc'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function filesystem_is_separated()
|
||||
{
|
||||
tenancy()->create('tenant1.localhost');
|
||||
tenancy()->create('tenant2.localhost');
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
Storage::disk('public')->put('foo', 'bar');
|
||||
$this->assertSame('bar', Storage::disk('public')->get('foo'));
|
||||
|
||||
tenancy()->init('tenant2.localhost');
|
||||
$this->assertFalse(Storage::disk('public')->exists('foo'));
|
||||
Storage::disk('public')->put('foo', 'xyz');
|
||||
Storage::disk('public')->put('abc', 'def');
|
||||
$this->assertSame('xyz', Storage::disk('public')->get('foo'));
|
||||
$this->assertSame('def', Storage::disk('public')->get('abc'));
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
$this->assertSame('bar', Storage::disk('public')->get('foo'));
|
||||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||
|
||||
tenancy()->create('tenant3.localhost');
|
||||
tenancy()->init('tenant3.localhost');
|
||||
$this->assertFalse(Storage::disk('public')->exists('foo'));
|
||||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||
}
|
||||
}
|
||||
|
||||
class User extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
|
@ -72,6 +72,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'public',
|
||||
's3',
|
||||
],
|
||||
'tenancy.migrations_directory' => database_path('../migrations'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue