mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:34:02 +00:00
merge 1.x into .
This commit is contained in:
commit
545c2330d4
33 changed files with 214 additions and 103 deletions
|
|
@ -24,6 +24,10 @@ class BootstrapsTenancyTest extends TestCase
|
|||
/** @test */
|
||||
public function redis_is_prefixed()
|
||||
{
|
||||
if (! config('tenancy.redis.tenancy')) {
|
||||
$this->markTestSkipped('Redis tenancy disabled.');
|
||||
}
|
||||
|
||||
$this->initTenancy();
|
||||
foreach (config('tenancy.redis.prefixed_connections', ['default']) as $connection) {
|
||||
$prefix = config('tenancy.redis.prefix_base') . tenant('uuid');
|
||||
|
|
@ -35,6 +39,7 @@ class BootstrapsTenancyTest extends TestCase
|
|||
/** @test */
|
||||
public function predis_is_supported()
|
||||
{
|
||||
// No setDriver() before that version.
|
||||
if (app()->version() < 'v5.8.27') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
|
@ -57,7 +62,7 @@ class BootstrapsTenancyTest extends TestCase
|
|||
Config::set('database.redis.client', 'predis');
|
||||
Redis::setDriver('predis');
|
||||
Config::set('tenancy.redis.tenancy', true);
|
||||
|
||||
|
||||
$this->expectException(PhpRedisNotInstalledException::class);
|
||||
$this->initTenancy();
|
||||
}
|
||||
|
|
@ -72,16 +77,16 @@ class BootstrapsTenancyTest extends TestCase
|
|||
}
|
||||
|
||||
$this->initTenancy();
|
||||
|
||||
|
||||
$new_storage_path = storage_path();
|
||||
$this->assertEquals($old_storage_path . "/" . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
||||
$this->assertEquals($old_storage_path . '/' . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
||||
|
||||
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace("%storage_path%", storage_path(), $override);
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $old_storage_facade_roots[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ class CacheManagerTest extends TestCase
|
|||
tenancy()->init('bar.localhost');
|
||||
|
||||
$this->assertNotSame('bar', cache()->get('foo'));
|
||||
|
||||
|
||||
cache()->put('foo', 'xyz', 1);
|
||||
$this->assertSame('xyz', cache()->get('foo'));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||
|
||||
class CommandsTest extends TestCase
|
||||
|
|
@ -47,7 +47,7 @@ class CommandsTest extends TestCase
|
|||
{
|
||||
$tenant = tenant()->create('test.localhost');
|
||||
Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant['uuid']]
|
||||
'--tenants' => [$tenant['uuid']],
|
||||
]);
|
||||
|
||||
$this->assertFalse(Schema::hasTable('users'));
|
||||
|
|
@ -79,7 +79,7 @@ class CommandsTest extends TestCase
|
|||
public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back()
|
||||
{
|
||||
$originalDBName = DB::connection()->getDatabaseName();
|
||||
|
||||
|
||||
Artisan::call('tenants:migrate');
|
||||
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
||||
|
||||
|
|
@ -94,7 +94,7 @@ class CommandsTest extends TestCase
|
|||
public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back_when_tenancy_has_been_initialized()
|
||||
{
|
||||
tenancy()->init('localhost');
|
||||
|
||||
|
||||
$this->database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
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;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class DataSeparationTest extends TestCase
|
||||
{
|
||||
|
|
@ -19,7 +19,7 @@ class DataSeparationTest extends TestCase
|
|||
$tenant1 = tenancy()->create('tenant1.localhost');
|
||||
$tenant2 = tenancy()->create('tenant2.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']]
|
||||
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']],
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
|
|
@ -31,7 +31,7 @@ class DataSeparationTest extends TestCase
|
|||
'remember_token' => Str::random(10),
|
||||
]);
|
||||
$this->assertSame('foo', User::first()->name);
|
||||
|
||||
|
||||
tenancy()->init('tenant2.localhost');
|
||||
$this->assertSame(null, User::first());
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ class DataSeparationTest extends TestCase
|
|||
|
||||
$tenant3 = tenancy()->create('tenant3.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']]
|
||||
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']],
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant3.localhost');
|
||||
|
|
@ -66,13 +66,17 @@ class DataSeparationTest extends TestCase
|
|||
/** @test */
|
||||
public function redis_is_separated()
|
||||
{
|
||||
if (! config('tenancy.redis.tenancy')) {
|
||||
$this->markTestSkipped('Redis tenancy disabled.');
|
||||
}
|
||||
|
||||
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');
|
||||
|
|
@ -99,7 +103,7 @@ class DataSeparationTest extends TestCase
|
|||
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);
|
||||
|
|
@ -126,7 +130,7 @@ class DataSeparationTest extends TestCase
|
|||
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');
|
||||
|
|
@ -148,4 +152,4 @@ class DataSeparationTest extends TestCase
|
|||
class User extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ class DatabaseManagerTest extends TestCase
|
|||
tenancy()->init();
|
||||
tenancy()->disconnectDatabase();
|
||||
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
||||
|
||||
|
||||
$this->assertSame($old_connection_name, $new_connection_name);
|
||||
$this->assertNotEquals('tenant', $new_connection_name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,16 +19,16 @@ class GlobalCacheTest extends TestCase
|
|||
tenant()->create('foo.localhost');
|
||||
tenancy()->init('foo.localhost');
|
||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||
|
||||
|
||||
GlobalCache::put(['abc' => 'xyz'], 1);
|
||||
cache(['def' => 'ghi'], 10);
|
||||
$this->assertSame('ghi', cache('def'));
|
||||
|
||||
|
||||
tenancy()->end();
|
||||
$this->assertSame('xyz', GlobalCache::get('abc'));
|
||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||
$this->assertSame(null, cache('def'));
|
||||
|
||||
|
||||
tenant()->create('bar.localhost');
|
||||
tenancy()->init('bar.localhost');
|
||||
$this->assertSame('xyz', GlobalCache::get('abc'));
|
||||
|
|
@ -40,4 +40,4 @@ class GlobalCacheTest extends TestCase
|
|||
tenancy()->init('foo.localhost');
|
||||
$this->assertSame('ghi', cache('def'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class ReidentificationTest extends TestCase
|
|||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace("%storage_path%", storage_path(), $override);
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $originals[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
@ -49,7 +49,6 @@ class ReidentificationTest extends TestCase
|
|||
tenant()->create('second.localhost');
|
||||
tenancy()->init('second.localhost');
|
||||
|
||||
|
||||
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||
$this->assertSame($original . "/$suffix", storage_path());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ class TenantAssetTest extends TestCase
|
|||
$filename = 'testfile' . $this->randomString(10);
|
||||
\Storage::disk('public')->put($filename, 'bar');
|
||||
$path = storage_path("app/public/$filename");
|
||||
|
||||
|
||||
// response()->file() returns BinaryFileResponse whose content is
|
||||
// inaccessible via getContent, so ->assertSee() can't be used
|
||||
$this->get(tenant_asset($filename))->assertSuccessful();
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
config()->set('tenancy.queue_database_deletion', true);
|
||||
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
|
||||
app(DatabaseManager::class)->delete($db_name, 'sqlite');
|
||||
|
||||
|
||||
Queue::assertPushed(QueuedTenantDatabaseDeleter::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ class TenantManagerTest extends TestCase
|
|||
public function getTenantById_works()
|
||||
{
|
||||
$tenant = tenant()->create('foo.localhost');
|
||||
|
||||
|
||||
$this->assertSame($tenant, tenancy()->getTenantById($tenant['uuid']));
|
||||
}
|
||||
|
||||
|
|
@ -120,7 +120,7 @@ class TenantManagerTest extends TestCase
|
|||
|
||||
tenant()->create('foo.localhost');
|
||||
tenancy()->init('foo.localhost');
|
||||
|
||||
|
||||
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||
$this->assertNotSame($originals['storage_path'], storage_path());
|
||||
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||
|
|
@ -146,7 +146,7 @@ class TenantManagerTest extends TestCase
|
|||
|
||||
tenant()->create('foo.localhost');
|
||||
tenancy()->init('foo.localhost');
|
||||
|
||||
|
||||
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||
$this->assertNotSame($originals['storage_path'], storage_path());
|
||||
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||
|
|
@ -182,8 +182,16 @@ class TenantManagerTest extends TestCase
|
|||
$tenant = tenant()->create('foo.localhost');
|
||||
tenant()->delete($tenant['uuid']);
|
||||
$this->assertSame([], tenancy()->all()->toArray());
|
||||
|
||||
|
||||
$tenant = tenant()->create('foo.localhost');
|
||||
$this->assertSame([$tenant], tenancy()->all()->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all_returns_a_list_of_all_tenants()
|
||||
{
|
||||
$tenant1 = tenant()->create('foo.localhost');
|
||||
$tenant2 = tenant()->create('bar.localhost');
|
||||
$this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenant()->all()->toArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,15 +2,13 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||
|
||||
class TenantStorageTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function deleting_a_tenant_works()
|
||||
{
|
||||
$abc = tenant()->create('abc.localhost');
|
||||
|
||||
|
||||
$this->assertTrue(tenant()->all()->contains($abc));
|
||||
|
||||
tenant()->delete($abc['uuid']);
|
||||
|
|
@ -110,7 +108,7 @@ class TenantStorageTest extends TestCase
|
|||
public function put_returns_the_key_value_pairs_when_a_single_argument_is_used()
|
||||
{
|
||||
$value = ['foo' => 'bar', 'abc' => 'xyz'];
|
||||
|
||||
|
||||
$this->assertSame($value, tenancy()->put($value));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,15 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
|
||||
private function checkRequirements(): void
|
||||
{
|
||||
parent::checkRequirements();
|
||||
|
||||
dd($this->getAnnotations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the test environment
|
||||
* Setup the test environment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
|
@ -49,13 +56,14 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
if (file_exists(__DIR__ . '/../.env')) {
|
||||
$this->loadDotEnv();
|
||||
\Dotenv\Dotenv::create(__DIR__ . '/..')->load();
|
||||
}
|
||||
|
||||
$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.options.prefix' => 'foo',
|
||||
'database.redis.tenancy' => [
|
||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
||||
|
|
@ -63,6 +71,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
// Use the #14 Redis database unless specified otherwise.
|
||||
// Make sure you don't store anything in this db!
|
||||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
||||
'prefix' => 'abc', // todo unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation
|
||||
],
|
||||
'tenancy.database' => [
|
||||
'based_on' => 'sqlite',
|
||||
|
|
@ -81,14 +90,19 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'tenancy.redis.prefixed_connections' => ['default'],
|
||||
'tenancy.migrations_directory' => database_path('../migrations'),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function loadDotEnv()
|
||||
{
|
||||
if (app()::VERSION > '5.8.0') {
|
||||
\Dotenv\Dotenv::create(__DIR__ . '/..')->load();
|
||||
} else {
|
||||
(new \Dotenv\Dotenv(__DIR__ . '/..'))->load();
|
||||
switch ((string) env('STANCL_TENANCY_TEST_VARIANT', '1')) {
|
||||
case '2':
|
||||
$app['config']->set([
|
||||
'tenancy.redis.tenancy' => false,
|
||||
'database.redis.client' => 'predis',
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
$app['config']->set([
|
||||
'tenancy.redis.tenancy' => true,
|
||||
'database.redis.client' => 'phpredis',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue