1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 10:54:04 +00:00

Apply fixes from StyleCI (#80)

This commit is contained in:
Samuel Štancl 2019-08-02 20:01:10 +02:00 committed by GitHub
parent bd6583b6af
commit eceacd9422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 83 additions and 70 deletions

View file

@ -10,7 +10,7 @@ class CacheManager extends BaseCacheManager
{ {
$tags = [config('tenancy.cache.tag_base') . tenant('uuid')]; $tags = [config('tenancy.cache.tag_base') . tenant('uuid')];
if ($method === "tags") { if ($method === 'tags') {
if (\count($parameters) !== 1) { if (\count($parameters) !== 1) {
throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed."); throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed.");
} }

View file

@ -37,7 +37,7 @@ class TenantList extends Command
*/ */
public function handle() public function handle()
{ {
$this->info("Listing all tenants."); $this->info('Listing all tenants.');
tenancy()->all()->each(function ($tenant) { tenancy()->all()->each(function ($tenant) {
$this->line("[Tenant] uuid: {$tenant['uuid']} @ {$tenant['domain']}"); $this->line("[Tenant] uuid: {$tenant['uuid']} @ {$tenant['domain']}");
}); });

View file

@ -90,7 +90,7 @@ class DatabaseManager
public function getDriver(): ?string public function getDriver(): ?string
{ {
return config("database.connections.tenant.driver"); return config('database.connections.tenant.driver');
} }
public function createTenantConnection(string $database_name) public function createTenantConnection(string $database_name)
@ -98,11 +98,11 @@ class DatabaseManager
// Create the `tenancy` database connection. // Create the `tenancy` database connection.
$based_on = config('tenancy.database.based_on') ?: config('database.default'); $based_on = config('tenancy.database.based_on') ?: config('database.default');
config()->set([ config()->set([
'database.connections.tenant' => config('database.connections.' . $based_on) 'database.connections.tenant' => config('database.connections.' . $based_on),
]); ]);
// Change DB name // Change DB name
$database_name = $this->getDriver() === "sqlite" ? database_path($database_name) : $database_name; $database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
config()->set(['database.connections.tenant.database' => $database_name]); config()->set(['database.connections.tenant.database' => $database_name]);
} }
} }

View file

@ -10,4 +10,4 @@ class GlobalCacheFacade extends Facade
{ {
return 'globalCache'; return 'globalCache';
} }
} }

View file

@ -5,13 +5,22 @@ namespace Stancl\Tenancy\Interfaces;
interface StorageDriver interface StorageDriver
{ {
public function identifyTenant(string $domain): array; public function identifyTenant(string $domain): array;
public function getAllTenants(array $uuids = []): array; public function getAllTenants(array $uuids = []): array;
public function getTenantById(string $uuid, array $fields = []): array; public function getTenantById(string $uuid, array $fields = []): array;
public function getTenantIdByDomain(string $domain): ?string; public function getTenantIdByDomain(string $domain): ?string;
public function createTenant(string $domain, string $uuid): array; public function createTenant(string $domain, string $uuid): array;
public function deleteTenant(string $uuid): bool; public function deleteTenant(string $uuid): bool;
public function get(string $uuid, string $key); public function get(string $uuid, string $key);
public function getMany(string $uuid, array $keys): array; public function getMany(string $uuid, array $keys): array;
public function put(string $uuid, string $key, $value); public function put(string $uuid, string $key, $value);
public function putMany(string $uuid, array $values): array; public function putMany(string $uuid, array $values): array;
} }

View file

@ -8,7 +8,7 @@ interface TenantDatabaseManager
* Create a database. * Create a database.
* *
* @param string $name Name of the database. * @param string $name Name of the database.
* @return boolean * @return bool
*/ */
public function createDatabase(string $name): bool; public function createDatabase(string $name): bool;
@ -16,7 +16,7 @@ interface TenantDatabaseManager
* Delete a database. * Delete a database.
* *
* @param string $name Name of the database. * @param string $name Name of the database.
* @return boolean * @return bool
*/ */
public function deleteDatabase(string $name): bool; public function deleteDatabase(string $name): bool;
} }

View file

@ -5,8 +5,8 @@ namespace Stancl\Tenancy\Jobs;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Stancl\Tenancy\Interfaces\TenantDatabaseManager; use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
class QueuedTenantDatabaseCreator implements ShouldQueue class QueuedTenantDatabaseCreator implements ShouldQueue

View file

@ -5,8 +5,8 @@ namespace Stancl\Tenancy\Jobs;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Stancl\Tenancy\Interfaces\TenantDatabaseManager; use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
class QueuedTenantDatabaseDeleter implements ShouldQueue class QueuedTenantDatabaseDeleter implements ShouldQueue

View file

@ -20,6 +20,7 @@ class RedisStorageDriver implements StorageDriver
if (! $id) { if (! $id) {
throw new \Exception("Tenant could not be identified on domain {$domain}"); throw new \Exception("Tenant could not be identified on domain {$domain}");
} }
return $this->getTenantById($id); return $this->getTenantById($id);
} }
@ -33,7 +34,7 @@ class RedisStorageDriver implements StorageDriver
public function getTenantById(string $uuid, array $fields = []): array public function getTenantById(string $uuid, array $fields = []): array
{ {
$fields = (array) $fields; $fields = (array) $fields;
if (! $fields) { if (! $fields) {
return $this->redis->hgetall("tenants:$uuid"); return $this->redis->hgetall("tenants:$uuid");
} }
@ -50,14 +51,15 @@ class RedisStorageDriver implements StorageDriver
{ {
$this->redis->hmset("domains:$domain", 'tenant_id', $uuid); $this->redis->hmset("domains:$domain", 'tenant_id', $uuid);
$this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain)); $this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain));
return $this->redis->hgetall("tenants:$uuid"); return $this->redis->hgetall("tenants:$uuid");
} }
/** /**
* @inheritDoc * {@inheritdoc}
* *
* @param string $id * @param string $id
* @return boolean * @return bool
* @todo Make tenant & domain deletion atomic. * @todo Make tenant & domain deletion atomic.
*/ */
public function deleteTenant(string $id): bool public function deleteTenant(string $id): bool
@ -69,6 +71,7 @@ class RedisStorageDriver implements StorageDriver
} }
$this->redis->del("domains:$domain"); $this->redis->del("domains:$domain");
return (bool) $this->redis->del("tenants:$id"); return (bool) $this->redis->del("tenants:$id");
} }
@ -91,7 +94,7 @@ class RedisStorageDriver implements StorageDriver
return substr($hash, strlen($redis_prefix)); return substr($hash, strlen($redis_prefix));
}, $this->redis->scan(null, $redis_prefix.'tenants:*')); }, $this->redis->scan(null, $redis_prefix.'tenants:*'));
} }
return array_map(function ($tenant) { return array_map(function ($tenant) {
return $this->redis->hgetall($tenant); return $this->redis->hgetall($tenant);
}, $hashes); }, $hashes);
@ -110,12 +113,14 @@ class RedisStorageDriver implements StorageDriver
public function put(string $uuid, string $key, $value) public function put(string $uuid, string $key, $value)
{ {
$this->redis->hset("tenants:$uuid", $key, $value); $this->redis->hset("tenants:$uuid", $key, $value);
return $value; return $value;
} }
public function putMany(string $uuid, array $values): array public function putMany(string $uuid, array $values): array
{ {
$this->redis->hmset("tenants:$uuid", $values); $this->redis->hmset("tenants:$uuid", $values);
return $values; return $values;
} }
} }

View file

@ -3,16 +3,14 @@
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Stancl\Tenancy\Commands\Seed; use Stancl\Tenancy\Commands\Seed;
use Stancl\Tenancy\TenantManager; use Illuminate\Cache\CacheManager;
use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Commands\Migrate; use Stancl\Tenancy\Commands\Migrate;
use Stancl\Tenancy\Commands\Rollback;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Commands\Rollback;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Commands\TenantList; use Stancl\Tenancy\Commands\TenantList;
use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Interfaces\ServerConfigManager; use Stancl\Tenancy\Interfaces\ServerConfigManager;
use Illuminate\Cache\CacheManager;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider
{ {
@ -39,7 +37,7 @@ class TenancyServiceProvider extends ServiceProvider
$this->loadRoutesFrom(__DIR__ . '/routes.php'); $this->loadRoutesFrom(__DIR__ . '/routes.php');
Route::middlewareGroup('tenancy', [ Route::middlewareGroup('tenancy', [
\Stancl\Tenancy\Middleware\InitializeTenancy::class \Stancl\Tenancy\Middleware\InitializeTenancy::class,
]); ]);
$this->app->register(TenantRouteServiceProvider::class); $this->app->register(TenantRouteServiceProvider::class);

View file

@ -23,7 +23,7 @@ class TenantManager
* @var StorageDriver * @var StorageDriver
*/ */
protected $storage; protected $storage;
/** /**
* Database manager. * Database manager.
* *
@ -49,6 +49,7 @@ class TenantManager
{ {
$this->setTenant($this->identify($domain)); $this->setTenant($this->identify($domain));
$this->bootstrap(); $this->bootstrap();
return $this->tenant; return $this->tenant;
} }
@ -57,7 +58,7 @@ class TenantManager
$domain = $domain ?: $this->currentDomain(); $domain = $domain ?: $this->currentDomain();
if (! $domain) { if (! $domain) {
throw new \Exception("No domain supplied nor detected."); throw new \Exception('No domain supplied nor detected.');
} }
$tenant = $this->storage->identifyTenant($domain); $tenant = $this->storage->identifyTenant($domain);
@ -79,7 +80,7 @@ class TenantManager
$tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain))); $tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)));
$this->database->create($this->getDatabaseName($tenant)); $this->database->create($this->getDatabaseName($tenant));
return $tenant; return $tenant;
} }
@ -98,6 +99,7 @@ class TenantManager
public function getTenantById(string $uuid, $fields = []) public function getTenantById(string $uuid, $fields = [])
{ {
$fields = (array) $fields; $fields = (array) $fields;
return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields)); return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields));
} }
@ -165,6 +167,7 @@ class TenantManager
public function getDatabaseName($tenant = []): string public function getDatabaseName($tenant = []): string
{ {
$tenant = $tenant ?: $this->tenant; $tenant = $tenant ?: $this->tenant;
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix']; return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
} }
@ -179,7 +182,7 @@ class TenantManager
$tenant = $this->jsonDecodeArrayValues($tenant); $tenant = $this->jsonDecodeArrayValues($tenant);
$this->tenant = $tenant; $this->tenant = $tenant;
return $tenant; return $tenant;
} }
@ -219,6 +222,7 @@ class TenantManager
{ {
$this->setTenant($this->storage->getTenantById($uuid)); $this->setTenant($this->storage->getTenantById($uuid));
$this->bootstrap(); $this->bootstrap();
return $this->tenant; return $this->tenant;
} }
@ -252,9 +256,9 @@ class TenantManager
{ {
if (\is_null($uuid)) { if (\is_null($uuid)) {
if (! isset($this->tenant['uuid'])) { if (! isset($this->tenant['uuid'])) {
throw new \Exception("No UUID supplied (and no tenant is currently identified)."); throw new \Exception('No UUID supplied (and no tenant is currently identified).');
} }
$uuid = $this->tenant['uuid']; $uuid = $this->tenant['uuid'];
// If $uuid is the uuid of the current tenant, put // If $uuid is the uuid of the current tenant, put
@ -313,7 +317,7 @@ class TenantManager
if (\is_null($attribute)) { if (\is_null($attribute)) {
return $this->tenant; return $this->tenant;
} }
return $this->tenant[(string) $attribute]; return $this->tenant[(string) $attribute];
} }
} }

View file

@ -13,7 +13,7 @@ trait BootstrapsTenancy
/** /**
* Was tenancy initialized/bootstrapped? * Was tenancy initialized/bootstrapped?
* *
* @var boolean * @var bool
*/ */
public $initialized = false; public $initialized = false;
@ -53,7 +53,7 @@ trait BootstrapsTenancy
foreach ($connections as $connection) { foreach ($connections as $connection) {
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid']; $prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
$client = Redis::connection($connection)->client(); $client = Redis::connection($connection)->client();
try { try {
$this->originalSettings['redis'][$connection] = $client->getOption($client::OPT_PREFIX); $this->originalSettings['redis'][$connection] = $client->getOption($client::OPT_PREFIX);
$client->setOption($client::OPT_PREFIX, $prefix); $client->setOption($client::OPT_PREFIX, $prefix);
@ -67,7 +67,7 @@ trait BootstrapsTenancy
{ {
foreach ($connections as $connection) { foreach ($connections as $connection) {
$client = Redis::connection($connection)->client(); $client = Redis::connection($connection)->client();
try { try {
$client->setOption($client::OPT_PREFIX, $this->originalSettings['redis'][$connection]); $client->setOption($client::OPT_PREFIX, $this->originalSettings['redis'][$connection]);
} catch (\Throwable $t) { } catch (\Throwable $t) {
@ -106,12 +106,12 @@ trait BootstrapsTenancy
// Storage facade // Storage facade
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
$old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix(); $old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix();
if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) {
Storage::disk($disk)->getAdapter()->setPathPrefix($root); Storage::disk($disk)->getAdapter()->setPathPrefix($root);
} else { } else {
$root = $this->app['config']["filesystems.disks.{$disk}.root"]; $root = $this->app['config']["filesystems.disks.{$disk}.root"];
Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}"); Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}");
} }
} }

View file

@ -9,7 +9,7 @@ trait HasATenantsOption
protected function getOptions() protected function getOptions()
{ {
return array_merge([ return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null] ['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
], parent::getOptions()); ], parent::getOptions());
} }
} }

View file

@ -57,7 +57,7 @@ class BootstrapsTenancyTest extends TestCase
Config::set('database.redis.client', 'predis'); Config::set('database.redis.client', 'predis');
Redis::setDriver('predis'); Redis::setDriver('predis');
Config::set('tenancy.redis.tenancy', true); Config::set('tenancy.redis.tenancy', true);
$this->expectException(PhpRedisNotInstalledException::class); $this->expectException(PhpRedisNotInstalledException::class);
$this->initTenancy(); $this->initTenancy();
} }
@ -72,16 +72,16 @@ class BootstrapsTenancyTest extends TestCase
} }
$this->initTenancy(); $this->initTenancy();
$new_storage_path = storage_path(); $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) { foreach (config('tenancy.filesystem.disks') as $disk) {
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid'); $suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix(); $current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
if ($override = config("tenancy.filesystem.root_override.{$disk}")) { 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 { } else {
if ($base = $old_storage_facade_roots[$disk]) { if ($base = $old_storage_facade_roots[$disk]) {
$correct_path_prefix = $base . "/$suffix/"; $correct_path_prefix = $base . "/$suffix/";

View file

@ -51,7 +51,7 @@ class CacheManagerTest extends TestCase
tenancy()->init('bar.localhost'); tenancy()->init('bar.localhost');
$this->assertNotSame('bar', cache()->get('foo')); $this->assertNotSame('bar', cache()->get('foo'));
cache()->put('foo', 'xyz', 1); cache()->put('foo', 'xyz', 1);
$this->assertSame('xyz', cache()->get('foo')); $this->assertSame('xyz', cache()->get('foo'));
} }

View file

@ -2,9 +2,9 @@
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
class CommandsTest extends TestCase class CommandsTest extends TestCase
@ -47,7 +47,7 @@ class CommandsTest extends TestCase
{ {
$tenant = tenant()->create('test.localhost'); $tenant = tenant()->create('test.localhost');
Artisan::call('tenants:migrate', [ Artisan::call('tenants:migrate', [
'--tenants' => [$tenant['uuid']] '--tenants' => [$tenant['uuid']],
]); ]);
$this->assertFalse(Schema::hasTable('users')); $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() public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back()
{ {
$originalDBName = DB::connection()->getDatabaseName(); $originalDBName = DB::connection()->getDatabaseName();
Artisan::call('tenants:migrate'); Artisan::call('tenants:migrate');
$this->assertSame($originalDBName, DB::connection()->getDatabaseName()); $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() public function database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back_when_tenancy_has_been_initialized()
{ {
tenancy()->init('localhost'); tenancy()->init('localhost');
$this->database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back(); $this->database_connection_is_switched_to_default_after_migrating_or_seeding_or_rolling_back();
} }
} }

View file

@ -2,11 +2,11 @@
namespace Stancl\Tenancy\Tests; 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\Str;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
class DataSeparationTest extends TestCase class DataSeparationTest extends TestCase
{ {
@ -19,7 +19,7 @@ class DataSeparationTest extends TestCase
$tenant1 = tenancy()->create('tenant1.localhost'); $tenant1 = tenancy()->create('tenant1.localhost');
$tenant2 = tenancy()->create('tenant2.localhost'); $tenant2 = tenancy()->create('tenant2.localhost');
\Artisan::call('tenants:migrate', [ \Artisan::call('tenants:migrate', [
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']] '--tenants' => [$tenant1['uuid'], $tenant2['uuid']],
]); ]);
tenancy()->init('tenant1.localhost'); tenancy()->init('tenant1.localhost');
@ -31,7 +31,7 @@ class DataSeparationTest extends TestCase
'remember_token' => Str::random(10), 'remember_token' => Str::random(10),
]); ]);
$this->assertSame('foo', User::first()->name); $this->assertSame('foo', User::first()->name);
tenancy()->init('tenant2.localhost'); tenancy()->init('tenant2.localhost');
$this->assertSame(null, User::first()); $this->assertSame(null, User::first());
@ -52,7 +52,7 @@ class DataSeparationTest extends TestCase
$tenant3 = tenancy()->create('tenant3.localhost'); $tenant3 = tenancy()->create('tenant3.localhost');
\Artisan::call('tenants:migrate', [ \Artisan::call('tenants:migrate', [
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']] '--tenants' => [$tenant1['uuid'], $tenant3['uuid']],
]); ]);
tenancy()->init('tenant3.localhost'); tenancy()->init('tenant3.localhost');
@ -72,7 +72,7 @@ class DataSeparationTest extends TestCase
tenancy()->init('tenant1.localhost'); tenancy()->init('tenant1.localhost');
Redis::set('foo', 'bar'); Redis::set('foo', 'bar');
$this->assertSame('bar', Redis::get('foo')); $this->assertSame('bar', Redis::get('foo'));
tenancy()->init('tenant2.localhost'); tenancy()->init('tenant2.localhost');
$this->assertSame(null, Redis::get('foo')); $this->assertSame(null, Redis::get('foo'));
Redis::set('foo', 'xyz'); Redis::set('foo', 'xyz');
@ -99,7 +99,7 @@ class DataSeparationTest extends TestCase
tenancy()->init('tenant1.localhost'); tenancy()->init('tenant1.localhost');
Cache::put('foo', 'bar', 60); Cache::put('foo', 'bar', 60);
$this->assertSame('bar', Cache::get('foo')); $this->assertSame('bar', Cache::get('foo'));
tenancy()->init('tenant2.localhost'); tenancy()->init('tenant2.localhost');
$this->assertSame(null, Cache::get('foo')); $this->assertSame(null, Cache::get('foo'));
Cache::put('foo', 'xyz', 60); Cache::put('foo', 'xyz', 60);
@ -126,7 +126,7 @@ class DataSeparationTest extends TestCase
tenancy()->init('tenant1.localhost'); tenancy()->init('tenant1.localhost');
Storage::disk('public')->put('foo', 'bar'); Storage::disk('public')->put('foo', 'bar');
$this->assertSame('bar', Storage::disk('public')->get('foo')); $this->assertSame('bar', Storage::disk('public')->get('foo'));
tenancy()->init('tenant2.localhost'); tenancy()->init('tenant2.localhost');
$this->assertFalse(Storage::disk('public')->exists('foo')); $this->assertFalse(Storage::disk('public')->exists('foo'));
Storage::disk('public')->put('foo', 'xyz'); Storage::disk('public')->put('foo', 'xyz');
@ -148,4 +148,4 @@ class DataSeparationTest extends TestCase
class User extends \Illuminate\Database\Eloquent\Model class User extends \Illuminate\Database\Eloquent\Model
{ {
protected $guarded = []; protected $guarded = [];
} }

View file

@ -13,7 +13,7 @@ class DatabaseManagerTest extends TestCase
tenancy()->init(); tenancy()->init();
tenancy()->disconnectDatabase(); tenancy()->disconnectDatabase();
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
$this->assertSame($old_connection_name, $new_connection_name); $this->assertSame($old_connection_name, $new_connection_name);
$this->assertNotEquals('tenant', $new_connection_name); $this->assertNotEquals('tenant', $new_connection_name);
} }

View file

@ -19,16 +19,16 @@ class GlobalCacheTest extends TestCase
tenant()->create('foo.localhost'); tenant()->create('foo.localhost');
tenancy()->init('foo.localhost'); tenancy()->init('foo.localhost');
$this->assertSame('bar', GlobalCache::get('foo')); $this->assertSame('bar', GlobalCache::get('foo'));
GlobalCache::put(['abc' => 'xyz'], 1); GlobalCache::put(['abc' => 'xyz'], 1);
cache(['def' => 'ghi'], 10); cache(['def' => 'ghi'], 10);
$this->assertSame('ghi', cache('def')); $this->assertSame('ghi', cache('def'));
tenancy()->end(); tenancy()->end();
$this->assertSame('xyz', GlobalCache::get('abc')); $this->assertSame('xyz', GlobalCache::get('abc'));
$this->assertSame('bar', GlobalCache::get('foo')); $this->assertSame('bar', GlobalCache::get('foo'));
$this->assertSame(null, cache('def')); $this->assertSame(null, cache('def'));
tenant()->create('bar.localhost'); tenant()->create('bar.localhost');
tenancy()->init('bar.localhost'); tenancy()->init('bar.localhost');
$this->assertSame('xyz', GlobalCache::get('abc')); $this->assertSame('xyz', GlobalCache::get('abc'));
@ -40,4 +40,4 @@ class GlobalCacheTest extends TestCase
tenancy()->init('foo.localhost'); tenancy()->init('foo.localhost');
$this->assertSame('ghi', cache('def')); $this->assertSame('ghi', cache('def'));
} }
} }

View file

@ -27,7 +27,7 @@ class ReidentificationTest extends TestCase
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix(); $current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
if ($override = config("tenancy.filesystem.root_override.{$disk}")) { 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 { } else {
if ($base = $originals[$disk]) { if ($base = $originals[$disk]) {
$correct_path_prefix = $base . "/$suffix/"; $correct_path_prefix = $base . "/$suffix/";
@ -49,7 +49,6 @@ class ReidentificationTest extends TestCase
tenant()->create('second.localhost'); tenant()->create('second.localhost');
tenancy()->init('second.localhost'); tenancy()->init('second.localhost');
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid'); $suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
$this->assertSame($original . "/$suffix", storage_path()); $this->assertSame($original . "/$suffix", storage_path());
} }

View file

@ -10,7 +10,7 @@ class TenantAssetTest extends TestCase
$filename = 'testfile' . $this->randomString(10); $filename = 'testfile' . $this->randomString(10);
\Storage::disk('public')->put($filename, 'bar'); \Storage::disk('public')->put($filename, 'bar');
$path = storage_path("app/public/$filename"); $path = storage_path("app/public/$filename");
// response()->file() returns BinaryFileResponse whose content is // response()->file() returns BinaryFileResponse whose content is
// inaccessible via getContent, so ->assertSee() can't be used // inaccessible via getContent, so ->assertSee() can't be used
$this->get(tenant_asset($filename))->assertSuccessful(); $this->get(tenant_asset($filename))->assertSuccessful();

View file

@ -139,7 +139,7 @@ class TenantDatabaseManagerTest extends TestCase
config()->set('tenancy.queue_database_deletion', true); config()->set('tenancy.queue_database_deletion', true);
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite'; $db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
app(DatabaseManager::class)->delete($db_name, 'sqlite'); app(DatabaseManager::class)->delete($db_name, 'sqlite');
Queue::assertPushed(QueuedTenantDatabaseDeleter::class); Queue::assertPushed(QueuedTenantDatabaseDeleter::class);
} }
} }

View file

@ -66,7 +66,7 @@ class TenantManagerTest extends TestCase
public function getTenantById_works() public function getTenantById_works()
{ {
$tenant = tenant()->create('foo.localhost'); $tenant = tenant()->create('foo.localhost');
$this->assertSame($tenant, tenancy()->getTenantById($tenant['uuid'])); $this->assertSame($tenant, tenancy()->getTenantById($tenant['uuid']));
} }
@ -120,7 +120,7 @@ class TenantManagerTest extends TestCase
tenant()->create('foo.localhost'); tenant()->create('foo.localhost');
tenancy()->init('foo.localhost'); tenancy()->init('foo.localhost');
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName()); $this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertNotSame($originals['storage_path'], storage_path()); $this->assertNotSame($originals['storage_path'], storage_path());
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix()); $this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
@ -146,7 +146,7 @@ class TenantManagerTest extends TestCase
tenant()->create('foo.localhost'); tenant()->create('foo.localhost');
tenancy()->init('foo.localhost'); tenancy()->init('foo.localhost');
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName()); $this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertNotSame($originals['storage_path'], storage_path()); $this->assertNotSame($originals['storage_path'], storage_path());
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix()); $this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
@ -182,7 +182,7 @@ class TenantManagerTest extends TestCase
$tenant = tenant()->create('foo.localhost'); $tenant = tenant()->create('foo.localhost');
tenant()->delete($tenant['uuid']); tenant()->delete($tenant['uuid']);
$this->assertSame([], tenancy()->all()->toArray()); $this->assertSame([], tenancy()->all()->toArray());
$tenant = tenant()->create('foo.localhost'); $tenant = tenant()->create('foo.localhost');
$this->assertSame([$tenant], tenancy()->all()->toArray()); $this->assertSame([$tenant], tenancy()->all()->toArray());
} }

View file

@ -2,15 +2,13 @@
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Interfaces\StorageDriver;
class TenantStorageTest extends TestCase class TenantStorageTest extends TestCase
{ {
/** @test */ /** @test */
public function deleting_a_tenant_works() public function deleting_a_tenant_works()
{ {
$abc = tenant()->create('abc.localhost'); $abc = tenant()->create('abc.localhost');
$this->assertTrue(tenant()->all()->contains($abc)); $this->assertTrue(tenant()->all()->contains($abc));
tenant()->delete($abc['uuid']); 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() public function put_returns_the_key_value_pairs_when_a_single_argument_is_used()
{ {
$value = ['foo' => 'bar', 'abc' => 'xyz']; $value = ['foo' => 'bar', 'abc' => 'xyz'];
$this->assertSame($value, tenancy()->put($value)); $this->assertSame($value, tenancy()->put($value));
} }
} }

View file

@ -10,7 +10,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
public $autoInitTenancy = true; public $autoInitTenancy = true;
/** /**
* Setup the test environment * Setup the test environment.
* *
* @return void * @return void
*/ */