mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
Fix some tests
This commit is contained in:
parent
a632b545b7
commit
1532ccf019
15 changed files with 121 additions and 118 deletions
|
|
@ -49,22 +49,23 @@ class Migrate extends MigrateCommand
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
$originalTenant = tenancy()->getTenant();
|
||||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||||
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
|
||||||
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
||||||
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
||||||
$this->input->setOption('database', 'tenant');
|
$this->input->setOption('database', 'tenant');
|
||||||
$this->database->connectToTenant($tenant); // todo test that this works with multiple tenants with MySQL
|
tenancy()->initialize($tenant); // todo test that this works with multiple tenants with MySQL
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tenancy()->initialized) {
|
if ($originalTenant) {
|
||||||
tenancy()->switchDatabaseConnection();
|
tenancy()->initialize($originalTenant);
|
||||||
} else {
|
} else {
|
||||||
$this->database->disconnect();
|
tenancy()->endTenancy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,18 +51,19 @@ class Rollback extends RollbackCommand
|
||||||
|
|
||||||
$this->input->setOption('database', 'tenant');
|
$this->input->setOption('database', 'tenant');
|
||||||
|
|
||||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
$originalTenant = tenancy()->getTenant();
|
||||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||||
$this->database->connectToTenant($tenant);
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tenancy()->initialized) {
|
if ($originalTenant) {
|
||||||
tenancy()->switchDatabaseConnection();
|
tenancy()->initialize($originalTenant);
|
||||||
} else {
|
} else {
|
||||||
$this->database->disconnect();
|
tenancy()->endTenancy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,13 +32,10 @@ class Run extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
if ($tenancy_was_initialized = tenancy()->initialized) {
|
$originalTenant = tenancy()->getTenant();
|
||||||
$previous_tenants_domain = tenant('domain');
|
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||||
}
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
|
||||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
|
||||||
tenancy()->init($tenant['domain']);
|
|
||||||
|
|
||||||
$callback = function ($prefix = '') {
|
$callback = function ($prefix = '') {
|
||||||
return function ($arguments, $argument) use ($prefix) {
|
return function ($arguments, $argument) use ($prefix) {
|
||||||
|
|
@ -58,11 +55,13 @@ class Run extends Command
|
||||||
// Run command
|
// Run command
|
||||||
$this->call($this->argument('commandname'), \array_merge($arguments, $options));
|
$this->call($this->argument('commandname'), \array_merge($arguments, $options));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($tenancy_was_initialized) {
|
if ($originalTenant) {
|
||||||
tenancy()->init($previous_tenants_domain);
|
tenancy()->initialize($originalTenant);
|
||||||
|
} else {
|
||||||
|
tenancy()->endTenancy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,18 +49,19 @@ class Seed extends SeedCommand
|
||||||
|
|
||||||
$this->input->setOption('database', 'tenant');
|
$this->input->setOption('database', 'tenant');
|
||||||
|
|
||||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
$originalTenant = tenancy()->getTenant();
|
||||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||||
$this->database->connectToTenant($tenant);
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
// Seed
|
// Seed
|
||||||
parent::handle();
|
parent::handle();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tenancy()->initialized) {
|
if ($originalTenant) {
|
||||||
tenancy()->switchDatabaseConnection();
|
tenancy()->initialize($originalTenant);
|
||||||
} else {
|
} else {
|
||||||
$this->database->disconnect();
|
tenancy()->endTenancy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class TenantList extends Command
|
||||||
{
|
{
|
||||||
$this->info('Listing all tenants.');
|
$this->info('Listing all tenants.');
|
||||||
tenancy()->all()->each(function ($tenant) {
|
tenancy()->all()->each(function ($tenant) {
|
||||||
$this->line("[Tenant] id: {$tenant['id']} @ {$tenant['domain']}");
|
$this->line("[Tenant] id: {$tenant['id']} @ ", implode('; ', $tenant->domains));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Exceptions;
|
|
||||||
|
|
||||||
class NoTenantIdentifiedException extends \Exception
|
|
||||||
{
|
|
||||||
protected $message = 'No tenant has been identified yet.';
|
|
||||||
}
|
|
||||||
|
|
@ -102,7 +102,9 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
*/
|
*/
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
{
|
{
|
||||||
return Tenants::getAllTenants($ids)->toArray();
|
return Tenants::getAllTenants($ids)->map(function ($array) {
|
||||||
|
return Tenant::fromStorage($array)->withDomains([]); // todo domains
|
||||||
|
})->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
|
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException;
|
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -34,9 +34,12 @@ class TenantManager
|
||||||
/** @var DatabaseManager */
|
/** @var DatabaseManager */
|
||||||
protected $database;
|
protected $database;
|
||||||
|
|
||||||
/** @var callable[][] */
|
/** @var callable[][] Event listeners */
|
||||||
protected $listeners = [];
|
protected $listeners = [];
|
||||||
|
|
||||||
|
/** @var bool Has tenancy been initialized. */
|
||||||
|
public $initialized;
|
||||||
|
|
||||||
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
|
|
@ -136,25 +139,32 @@ class TenantManager
|
||||||
* Get all tenants.
|
* Get all tenants.
|
||||||
*
|
*
|
||||||
* @param Tenant[]|string[] $only
|
* @param Tenant[]|string[] $only
|
||||||
* @return Tenant[]
|
* @return Collection<Tenant>
|
||||||
*/
|
*/
|
||||||
public function all($only = []): array
|
public function all($only = []): Collection
|
||||||
{
|
{
|
||||||
$only = array_map(function ($item) {
|
$only = array_map(function ($item) {
|
||||||
return $item instanceof Tenant ? $item->id : $item;
|
return $item instanceof Tenant ? $item->id : $item;
|
||||||
}, $only);
|
}, (array) $only);
|
||||||
|
|
||||||
return $this->storage->all($only);
|
return collect($this->storage->all($only));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initializeTenancy(Tenant $tenant): self
|
public function initializeTenancy(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$this->bootstrapTenancy($tenant);
|
$this->bootstrapTenancy($tenant);
|
||||||
$this->setTenant($tenant);
|
$this->setTenant($tenant);
|
||||||
|
$this->initialized = true;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @alias initializeTenancy */
|
||||||
|
public function initialize(Tenant $tenant): self
|
||||||
|
{
|
||||||
|
return $this->initializeTenancy($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
public function bootstrapTenancy(Tenant $tenant): self
|
public function bootstrapTenancy(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$prevented = $this->event('bootstrapping');
|
$prevented = $this->event('bootstrapping');
|
||||||
|
|
@ -176,22 +186,30 @@ class TenantManager
|
||||||
$this->app[$bootstrapper]->end();
|
$this->app[$bootstrapper]->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->initialized = false;
|
||||||
|
$this->tenant = null;
|
||||||
|
|
||||||
$this->event('ended');
|
$this->event('ended');
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @alias endTenancy */
|
||||||
|
public function end(): self
|
||||||
|
{
|
||||||
|
return $this->endTenancy();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current tenant.
|
* Get the current tenant.
|
||||||
*
|
*
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return Tenant|mixed
|
* @return Tenant|null|mixed
|
||||||
* @throws NoTenantIdentifiedException
|
|
||||||
*/
|
*/
|
||||||
public function getTenant(string $key = null)
|
public function getTenant(string $key = null)
|
||||||
{
|
{
|
||||||
if (! $this->tenant) {
|
if (! $this->tenant) {
|
||||||
throw new NoTenantIdentifiedException;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! is_null($key)) {
|
if (! is_null($key)) {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class CacheManagerTest extends TestCase
|
class CacheManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -43,13 +45,13 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tags_separate_cache_well_enough()
|
public function tags_separate_cache_well_enough()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
cache()->put('foo', 'bar', 1);
|
cache()->put('foo', 'bar', 1);
|
||||||
$this->assertSame('bar', cache()->get('foo'));
|
$this->assertSame('bar', cache()->get('foo'));
|
||||||
|
|
||||||
tenant()->create('bar.localhost');
|
Tenant::new()->withDomains(['bar.localhost'])->save();
|
||||||
tenancy()->init('bar.localhost');
|
tenancy()->init('bar.localhost');
|
||||||
|
|
||||||
$this->assertNotSame('bar', cache()->get('foo'));
|
$this->assertNotSame('bar', cache()->get('foo'));
|
||||||
|
|
@ -61,13 +63,13 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invoking_the_cache_helper_works()
|
public function invoking_the_cache_helper_works()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
cache(['foo' => 'bar'], 1);
|
cache(['foo' => 'bar'], 1);
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
|
|
||||||
tenant()->create('bar.localhost');
|
Tenant::new()->withDomains(['bar.localhost'])->save();
|
||||||
tenancy()->init('bar.localhost');
|
tenancy()->init('bar.localhost');
|
||||||
|
|
||||||
$this->assertNotSame('bar', cache('foo'));
|
$this->assertNotSame('bar', cache('foo'));
|
||||||
|
|
@ -79,13 +81,13 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function cache_is_persisted()
|
public function cache_is_persisted()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
cache(['foo' => 'bar'], 10);
|
cache(['foo' => 'bar'], 10);
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
|
|
@ -94,15 +96,15 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function cache_is_persisted_when_reidentification_is_used()
|
public function cache_is_persisted_when_reidentification_is_used()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenant()->create('bar.localhost');
|
Tenant::new()->withDomains(['bar.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
cache(['foo' => 'bar'], 10);
|
cache(['foo' => 'bar'], 10);
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
|
|
||||||
tenancy()->init('bar.localhost');
|
tenancy()->init('bar.localhost');
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Tests;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
|
|
||||||
class CommandsTest extends TestCase
|
class CommandsTest extends TestCase
|
||||||
|
|
@ -40,23 +41,23 @@ class CommandsTest extends TestCase
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('test.localhost');
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function migrate_command_works_with_tenants_option()
|
public function migrate_command_works_with_tenants_option()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('test.localhost');
|
$tenant = Tenant::new()->withDomains(['test2.localhost'])->save();
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$tenant['id']],
|
'--tenants' => [$tenant['id']],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('test.localhost');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
||||||
tenancy()->init('test.localhost');
|
tenancy()->init('test2.localhost');
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,7 +66,7 @@ class CommandsTest extends TestCase
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('test.localhost');
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
Artisan::call('tenants:rollback');
|
Artisan::call('tenants:rollback');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
@ -98,7 +99,7 @@ class CommandsTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function database_connection_is_switched_to_default_when_tenancy_has_been_initialized()
|
public function database_connection_is_switched_to_default_when_tenancy_has_been_initialized()
|
||||||
{
|
{
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('test.localhost');
|
||||||
|
|
||||||
$this->database_connection_is_switched_to_default();
|
$this->database_connection_is_switched_to_default();
|
||||||
}
|
}
|
||||||
|
|
@ -106,9 +107,9 @@ class CommandsTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function run_commands_works()
|
public function run_commands_works()
|
||||||
{
|
{
|
||||||
$id = tenant()->create('run.localhost')['id'];
|
$id = Tenant::new()->withDomains(['run.localhost'])->save()['id'];
|
||||||
|
|
||||||
Artisan::call('tenants:migrate', ['--tenants' => $id]);
|
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
||||||
|
|
||||||
$this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
$this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
||||||
->expectsOutput("User's name is Test command")
|
->expectsOutput("User's name is Test command")
|
||||||
|
|
@ -128,6 +129,7 @@ class CommandsTest extends TestCase
|
||||||
\mkdir($dir, 0777, true);
|
\mkdir($dir, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo move this to a file
|
||||||
\file_put_contents(app_path('Http/Kernel.php'), "<?php
|
\file_put_contents(app_path('Http/Kernel.php'), "<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace App\Http;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use GlobalCache;
|
use GlobalCache;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class GlobalCacheTest extends TestCase
|
class GlobalCacheTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -18,7 +19,7 @@ class GlobalCacheTest extends TestCase
|
||||||
GlobalCache::put(['foo' => 'bar'], 1);
|
GlobalCache::put(['foo' => 'bar'], 1);
|
||||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||||
|
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||||
|
|
||||||
|
|
@ -26,12 +27,12 @@ class GlobalCacheTest extends TestCase
|
||||||
cache(['def' => 'ghi'], 10);
|
cache(['def' => 'ghi'], 10);
|
||||||
$this->assertSame('ghi', cache('def'));
|
$this->assertSame('ghi', cache('def'));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
$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::new()->withDomains(['bar.localhost'])->save();
|
||||||
tenancy()->init('bar.localhost');
|
tenancy()->init('bar.localhost');
|
||||||
$this->assertSame('xyz', GlobalCache::get('abc'));
|
$this->assertSame('xyz', GlobalCache::get('abc'));
|
||||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class ReidentificationTest extends TestCase
|
class ReidentificationTest extends TestCase
|
||||||
{
|
{
|
||||||
public $autoInitTenancy = false;
|
public $autoInitTenancy = false;
|
||||||
|
|
@ -21,7 +23,7 @@ class ReidentificationTest extends TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('localhost');
|
||||||
tenant()->create('second.localhost');
|
Tenant::new()->withDomains(['second.localhost'])->save();
|
||||||
tenancy()->init('second.localhost');
|
tenancy()->init('second.localhost');
|
||||||
|
|
||||||
foreach (config('tenancy.filesystem.disks') as $disk) {
|
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||||
|
|
@ -48,7 +50,7 @@ class ReidentificationTest extends TestCase
|
||||||
$original = storage_path();
|
$original = storage_path();
|
||||||
|
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('localhost');
|
||||||
tenant()->create('second.localhost');
|
Tenant::new()->withDomains(['second.localhost'])->save();
|
||||||
tenancy()->init('second.localhost');
|
tenancy()->init('second.localhost');
|
||||||
|
|
||||||
$suffix = config('tenancy.filesystem.suffix_base') . tenant('id');
|
$suffix = config('tenancy.filesystem.suffix_base') . tenant('id');
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class TenantManagerTest extends TestCase
|
class TenantManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -15,7 +16,7 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function current_tenant_is_stored_in_the_tenant_property()
|
public function current_tenant_is_stored_in_the_tenant_property()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('localhost');
|
$tenant = Tenant::new()->withDomains(['localhost'])->save();
|
||||||
|
|
||||||
tenancy()->init('localhost');
|
tenancy()->init('localhost');
|
||||||
|
|
||||||
|
|
@ -25,7 +26,7 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invoke_works()
|
public function invoke_works()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
$this->assertSame(tenant('id'), tenant()('id'));
|
$this->assertSame(tenant('id'), tenant()('id'));
|
||||||
|
|
@ -34,7 +35,7 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function initById_works()
|
public function initById_works()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
|
|
||||||
$this->assertNotSame($tenant, tenancy()->tenant);
|
$this->assertNotSame($tenant, tenancy()->tenant);
|
||||||
|
|
||||||
|
|
@ -46,7 +47,7 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function findByDomain_works()
|
public function findByDomain_works()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
|
|
||||||
$this->assertSame($tenant, tenant()->findByDomain('foo.localhost'));
|
$this->assertSame($tenant, tenant()->findByDomain('foo.localhost'));
|
||||||
}
|
}
|
||||||
|
|
@ -54,14 +55,14 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function getIdByDomain_works()
|
public function getIdByDomain_works()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
$this->assertSame(tenant()->getTenantIdByDomain('foo.localhost'), tenant()->getIdByDomain('foo.localhost'));
|
$this->assertSame(tenant()->getTenantIdByDomain('foo.localhost'), tenant()->getIdByDomain('foo.localhost'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function find_works()
|
public function find_works()
|
||||||
{
|
{
|
||||||
tenant()->create('dev.localhost');
|
Tenant::new()->withDomains(['dev.localhost'])->save();
|
||||||
tenancy()->init('dev.localhost');
|
tenancy()->init('dev.localhost');
|
||||||
|
|
||||||
$this->assertSame(tenant()->tenant, tenant()->find(tenant('id')));
|
$this->assertSame(tenant()->tenant, tenant()->find(tenant('id')));
|
||||||
|
|
@ -70,34 +71,17 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function getTenantById_works()
|
public function getTenantById_works()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
|
|
||||||
$this->assertSame($tenant, tenancy()->getTenantById($tenant['id']));
|
$this->assertSame($tenant, tenancy()->getTenantById($tenant['id']));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function init_returns_the_tenant()
|
|
||||||
{
|
|
||||||
$tenant = tenant()->create('foo.localhost');
|
|
||||||
|
|
||||||
$this->assertSame($tenant, tenancy()->init('foo.localhost'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function initById_returns_the_tenant()
|
|
||||||
{
|
|
||||||
$tenant = tenant()->create('foo.localhost');
|
|
||||||
$id = $tenant['id'];
|
|
||||||
|
|
||||||
$this->assertSame($tenant, tenancy()->initById($id));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function create_returns_the_supplied_domain()
|
public function create_returns_the_supplied_domain()
|
||||||
{
|
{
|
||||||
$domain = 'foo.localhost';
|
$domain = 'foo.localhost';
|
||||||
|
|
||||||
$this->assertSame($domain, tenant()->create($domain)['domain']);
|
$this->assertSame($domain, Tenant::new()->withDomains([$domain])->save()['domain']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -123,7 +107,7 @@ class TenantManagerTest extends TestCase
|
||||||
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||||
$this->assertSame($originals['cache'], app('cache'));
|
$this->assertSame($originals['cache'], app('cache'));
|
||||||
|
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
|
|
@ -131,7 +115,7 @@ class TenantManagerTest extends TestCase
|
||||||
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||||
$this->assertNotSame($originals['cache'], app('cache'));
|
$this->assertNotSame($originals['cache'], app('cache'));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
$this->assertSame($originals['storage_path'], storage_path());
|
$this->assertSame($originals['storage_path'], storage_path());
|
||||||
|
|
@ -149,7 +133,7 @@ class TenantManagerTest extends TestCase
|
||||||
'cache' => app('cache'),
|
'cache' => app('cache'),
|
||||||
];
|
];
|
||||||
|
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
|
|
@ -157,7 +141,7 @@ class TenantManagerTest extends TestCase
|
||||||
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||||
$this->assertNotSame($originals['cache'], app('cache'));
|
$this->assertNotSame($originals['cache'], app('cache'));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
$this->assertSame($originals['storage_path'], storage_path());
|
$this->assertSame($originals['storage_path'], storage_path());
|
||||||
|
|
@ -165,7 +149,7 @@ class TenantManagerTest extends TestCase
|
||||||
$this->assertSame($originals['cache'], app('cache'));
|
$this->assertSame($originals['cache'], app('cache'));
|
||||||
|
|
||||||
// Reidentify tenant
|
// Reidentify tenant
|
||||||
tenant()->create('bar.localhost');
|
Tenant::new()->withDomains(['bar.localhost'])->save();
|
||||||
tenancy()->init('bar.localhost');
|
tenancy()->init('bar.localhost');
|
||||||
|
|
||||||
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
|
|
@ -173,7 +157,7 @@ class TenantManagerTest extends TestCase
|
||||||
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
|
||||||
$this->assertNotSame($originals['cache'], app('cache'));
|
$this->assertNotSame($originals['cache'], app('cache'));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
|
||||||
$this->assertSame($originals['storage_path'], storage_path());
|
$this->assertSame($originals['storage_path'], storage_path());
|
||||||
|
|
@ -184,27 +168,27 @@ class TenantManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tenant_can_be_deleted()
|
public function tenant_can_be_deleted()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenant()->delete($tenant['id']);
|
tenant()->delete($tenant['id']);
|
||||||
$this->assertSame([], tenancy()->all()->toArray());
|
$this->assertSame([], tenancy()->all()->toArray());
|
||||||
|
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
$this->assertSame([$tenant], tenancy()->all()->toArray());
|
$this->assertSame([$tenant], tenancy()->all()->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function all_returns_a_list_of_all_tenants()
|
public function all_returns_a_list_of_all_tenants()
|
||||||
{
|
{
|
||||||
$tenant1 = tenant()->create('foo.localhost');
|
$tenant1 = Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
$tenant2 = tenant()->create('bar.localhost');
|
$tenant2 = Tenant::new()->withDomains(['bar.localhost'])->save();
|
||||||
$this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenant()->all()->toArray());
|
$this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenancy()->all()->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function properites_can_be_passed_in_the_create_method()
|
public function properites_can_be_passed_in_the_create_method()
|
||||||
{
|
{
|
||||||
$data = ['plan' => 'free', 'subscribed_until' => '2020-01-01'];
|
$data = ['plan' => 'free', 'subscribed_until' => '2020-01-01'];
|
||||||
$tenant = tenant()->create('foo.localhost', $data);
|
$tenant = Tenant::new()->withDomains(['foo.localhost', $data])->save();
|
||||||
|
|
||||||
$tenant_data = $tenant;
|
$tenant_data = $tenant;
|
||||||
unset($tenant_data['id']);
|
unset($tenant_data['id']);
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,13 @@ 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::new()->withDomains(['abc.localhost'])->save();
|
||||||
|
|
||||||
$this->assertTrue(tenant()->all()->contains($abc));
|
$this->assertTrue(tenancy()->all()->contains($abc));
|
||||||
|
|
||||||
tenant()->delete($abc['id']);
|
tenant()->delete($abc['id']);
|
||||||
|
|
||||||
$this->assertFalse(tenant()->all()->contains($abc));
|
$this->assertFalse(tenancy()->all()->contains($abc));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -62,7 +62,7 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function put_works_on_a_tenant_different_than_the_current_one_when_two_args_are_used()
|
public function put_works_on_a_tenant_different_than_the_current_one_when_two_args_are_used()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('second.localhost');
|
$tenant = Tenant::new()->withDomains(['second.localhost'])->save();
|
||||||
$id = $tenant['id'];
|
$id = $tenant['id'];
|
||||||
|
|
||||||
tenancy()->put('foo', 'bar', $id);
|
tenancy()->put('foo', 'bar', $id);
|
||||||
|
|
@ -74,7 +74,7 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function put_works_on_a_tenant_different_than_the_current_one_when_a_single_arg_is_used()
|
public function put_works_on_a_tenant_different_than_the_current_one_when_a_single_arg_is_used()
|
||||||
{
|
{
|
||||||
$tenant = tenant()->create('second.localhost');
|
$tenant = Tenant::new()->withDomains(['second.localhost'])->save();
|
||||||
$id = $tenant['id'];
|
$id = $tenant['id'];
|
||||||
|
|
||||||
$keys = ['foo', 'abc'];
|
$keys = ['foo', 'abc'];
|
||||||
|
|
@ -159,14 +159,14 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function retrieving_data_without_cache_works()
|
public function retrieving_data_without_cache_works()
|
||||||
{
|
{
|
||||||
tenant()->create('foo.localhost');
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
tenancy()->put('foo', 'bar');
|
tenancy()->put('foo', 'bar');
|
||||||
$this->assertSame('bar', tenancy()->get('foo'));
|
$this->assertSame('bar', tenancy()->get('foo'));
|
||||||
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
$this->assertSame('bar', tenancy()->get('foo'));
|
$this->assertSame('bar', tenancy()->get('foo'));
|
||||||
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
||||||
|
|
@ -179,7 +179,7 @@ class TenantStorageTest extends TestCase
|
||||||
$this->markTestSkipped();
|
$this->markTestSkipped();
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->endTenancy();
|
||||||
|
|
||||||
$this->loadMigrationsFrom([
|
$this->loadMigrationsFrom([
|
||||||
'--path' => __DIR__ . '/Etc',
|
'--path' => __DIR__ . '/Etc',
|
||||||
|
|
|
||||||
|
|
@ -106,13 +106,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'tenancy.storage_driver' => RedisStorageDriver::class,
|
'tenancy.storage_driver' => RedisStorageDriver::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// tenancy()->storage = $app->make(RedisStorageDriver::class); // todo this shouldn't be necessary
|
// tenancy()->storage = $app->make(RedisStorageDriver::class); // TODO this shouldn't be necessary but is necessary
|
||||||
} elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') {
|
} elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') {
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
'tenancy.storage_driver' => DatabaseStorageDriver::class,
|
'tenancy.storage_driver' => DatabaseStorageDriver::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// tenancy()->storage = $app->make(DatabaseStorageDriver::class); // todo this shouldn't be necessary
|
// tenancy()->storage = $app->make(DatabaseStorageDriver::class); // TODO this shouldn't be necessary but is necessary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue