mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 12:24:04 +00:00
add docker [wip]
This commit is contained in:
commit
acdf39d15b
17 changed files with 358 additions and 47 deletions
|
|
@ -3,6 +3,8 @@
|
|||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
|
||||
|
||||
class BootstrapsTenancyTest extends TestCase
|
||||
{
|
||||
|
|
@ -30,6 +32,36 @@ class BootstrapsTenancyTest extends TestCase
|
|||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function predis_is_supported()
|
||||
{
|
||||
if (app()->version() < 'v5.8.27') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
Config::set('database.redis.client', 'predis');
|
||||
Redis::setDriver('predis');
|
||||
Config::set('tenancy.redis.tenancy', false);
|
||||
|
||||
// assert no exception is thrown from initializing tenancy
|
||||
$this->assertNotNull($this->initTenancy());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function predis_is_not_supported_without_disabling_redis_multitenancy()
|
||||
{
|
||||
if (app()->version() < 'v5.8.27') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
Config::set('database.redis.client', 'predis');
|
||||
Redis::setDriver('predis');
|
||||
Config::set('tenancy.redis.tenancy', true);
|
||||
|
||||
$this->expectException(PhpRedisNotInstalledException::class);
|
||||
$this->initTenancy();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function filesystem_is_suffixed()
|
||||
{
|
||||
|
|
|
|||
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 = [];
|
||||
}
|
||||
|
|
@ -40,7 +40,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function mysql_database_can_be_created_and_deleted()
|
||||
{
|
||||
if (! $this->isTravis()) {
|
||||
if (! $this->isContainerized()) {
|
||||
$this->markTestSkipped('As to not bloat your MySQL instance with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function mysql_database_can_be_created_and_deleted_using_queued_commands()
|
||||
{
|
||||
if (! $this->isTravis()) {
|
||||
if (! $this->isContainerized()) {
|
||||
$this->markTestSkipped('As to not bloat your MySQL instance with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +81,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function pgsql_database_can_be_created_and_deleted()
|
||||
{
|
||||
if (! $this->isTravis()) {
|
||||
if (! $this->isContainerized()) {
|
||||
$this->markTestSkipped('As to not bloat your PostgreSQL instance with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
|
|
@ -98,7 +98,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function pgsql_database_can_be_created_and_deleted_using_queued_commands()
|
||||
{
|
||||
if (! $this->isTravis()) {
|
||||
if (! $this->isContainerized()) {
|
||||
$this->markTestSkipped('As to not bloat your PostgreSQL instance with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +126,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
|
||||
config()->set('tenancy.queue_database_creation', true);
|
||||
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
|
||||
$this->assertTrue(app(DatabaseManager::class)->create($db_name, 'sqlite'));
|
||||
app(DatabaseManager::class)->create($db_name, 'sqlite');
|
||||
|
||||
Queue::assertPushed(QueuedTenantDatabaseCreator::class);
|
||||
}
|
||||
|
|
@ -138,7 +138,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
|
||||
config()->set('tenancy.queue_database_deletion', true);
|
||||
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
|
||||
$this->assertTrue(app(DatabaseManager::class)->delete($db_name, 'sqlite'));
|
||||
app(DatabaseManager::class)->delete($db_name, 'sqlite');
|
||||
|
||||
Queue::assertPushed(QueuedTenantDatabaseDeleter::class);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
|
||||
public function initTenancy($domain = 'localhost')
|
||||
{
|
||||
tenancy()->init($domain);
|
||||
return tenancy()->init($domain);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -53,6 +53,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
|
||||
$app['config']->set([
|
||||
'database.redis.client' => 'phpredis',
|
||||
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.tenancy' => [
|
||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
||||
|
|
@ -67,12 +69,16 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'suffix' => '.sqlite',
|
||||
],
|
||||
'database.connections.sqlite.database' => ':memory:',
|
||||
'database.connections.pgsql.username' => 'postgres',
|
||||
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
|
||||
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
|
||||
// 'database.connections.pgsql.username' => 'pgsqluser',
|
||||
'tenancy.filesystem.disks' => [
|
||||
'local',
|
||||
'public',
|
||||
's3',
|
||||
],
|
||||
'tenancy.redis.tenancy' => true,
|
||||
'tenancy.migrations_directory' => database_path('../migrations'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
@ -113,11 +119,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
|
||||
}
|
||||
|
||||
public function isTravis()
|
||||
public function isContainerized()
|
||||
{
|
||||
// Multiple, just to make sure. Someone might accidentally
|
||||
// set one of these environment vars on their computer.
|
||||
return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION');
|
||||
return env('CONTINUOUS_INTEGRATION') || env('DOCKER');
|
||||
}
|
||||
|
||||
public function assertArrayIsSubset($subset, $array, string $message = ''): void
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue