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

Fix some tests

This commit is contained in:
Samuel Štancl 2019-09-16 16:57:55 +02:00
parent a632b545b7
commit 1532ccf019
15 changed files with 121 additions and 118 deletions

View file

@ -49,22 +49,23 @@ class Migrate extends MigrateCommand
return;
}
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
$originalTenant = tenancy()->getTenant();
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
// Database connections are cached by Illuminate\Database\ConnectionResolver.
$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
parent::handle();
});
if (tenancy()->initialized) {
tenancy()->switchDatabaseConnection();
if ($originalTenant) {
tenancy()->initialize($originalTenant);
} else {
$this->database->disconnect();
tenancy()->endTenancy();
}
}
}

View file

@ -51,18 +51,19 @@ class Rollback extends RollbackCommand
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
$this->database->connectToTenant($tenant);
$originalTenant = tenancy()->getTenant();
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
tenancy()->initialize($tenant);
// Migrate
parent::handle();
});
if (tenancy()->initialized) {
tenancy()->switchDatabaseConnection();
if ($originalTenant) {
tenancy()->initialize($originalTenant);
} else {
$this->database->disconnect();
tenancy()->endTenancy();
}
}
}

View file

@ -32,13 +32,10 @@ class Run extends Command
*/
public function handle()
{
if ($tenancy_was_initialized = tenancy()->initialized) {
$previous_tenants_domain = tenant('domain');
}
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
tenancy()->init($tenant['domain']);
$originalTenant = tenancy()->getTenant();
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
tenancy()->initialize($tenant);
$callback = function ($prefix = '') {
return function ($arguments, $argument) use ($prefix) {
@ -58,11 +55,13 @@ class Run extends Command
// Run command
$this->call($this->argument('commandname'), \array_merge($arguments, $options));
tenancy()->end();
tenancy()->endTenancy();
});
if ($tenancy_was_initialized) {
tenancy()->init($previous_tenants_domain);
if ($originalTenant) {
tenancy()->initialize($originalTenant);
} else {
tenancy()->endTenancy();
}
}
}

View file

@ -49,18 +49,19 @@ class Seed extends SeedCommand
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
$this->database->connectToTenant($tenant);
$originalTenant = tenancy()->getTenant();
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
tenancy()->initialize($tenant);
// Seed
parent::handle();
});
if (tenancy()->initialized) {
tenancy()->switchDatabaseConnection();
if ($originalTenant) {
tenancy()->initialize($originalTenant);
} else {
$this->database->disconnect();
tenancy()->endTenancy();
}
}
}

View file

@ -31,7 +31,7 @@ class TenantList extends Command
{
$this->info('Listing all tenants.');
tenancy()->all()->each(function ($tenant) {
$this->line("[Tenant] id: {$tenant['id']} @ {$tenant['domain']}");
$this->line("[Tenant] id: {$tenant['id']} @ ", implode('; ', $tenant->domains));
});
}
}

View file

@ -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.';
}

View file

@ -102,7 +102,9 @@ class DatabaseStorageDriver implements StorageDriver
*/
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();
}
/**

View file

@ -6,8 +6,8 @@ namespace Stancl\Tenancy;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
/**
@ -34,9 +34,12 @@ class TenantManager
/** @var DatabaseManager */
protected $database;
/** @var callable[][] */
/** @var callable[][] Event listeners */
protected $listeners = [];
/** @var bool Has tenancy been initialized. */
public $initialized;
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
{
$this->app = $app;
@ -136,25 +139,32 @@ class TenantManager
* Get all tenants.
*
* @param Tenant[]|string[] $only
* @return Tenant[]
* @return Collection<Tenant>
*/
public function all($only = []): array
public function all($only = []): Collection
{
$only = array_map(function ($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
{
$this->bootstrapTenancy($tenant);
$this->setTenant($tenant);
$this->initialized = true;
return $this;
}
/** @alias initializeTenancy */
public function initialize(Tenant $tenant): self
{
return $this->initializeTenancy($tenant);
}
public function bootstrapTenancy(Tenant $tenant): self
{
$prevented = $this->event('bootstrapping');
@ -176,22 +186,30 @@ class TenantManager
$this->app[$bootstrapper]->end();
}
$this->initialized = false;
$this->tenant = null;
$this->event('ended');
return $this;
}
/** @alias endTenancy */
public function end(): self
{
return $this->endTenancy();
}
/**
* Get the current tenant.
*
* @param string $key
* @return Tenant|mixed
* @throws NoTenantIdentifiedException
* @return Tenant|null|mixed
*/
public function getTenant(string $key = null)
{
if (! $this->tenant) {
throw new NoTenantIdentifiedException;
return null;
}
if (! is_null($key)) {

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant;
class CacheManagerTest extends TestCase
{
/** @test */
@ -43,13 +45,13 @@ class CacheManagerTest extends TestCase
/** @test */
public function tags_separate_cache_well_enough()
{
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache()->put('foo', 'bar', 1);
$this->assertSame('bar', cache()->get('foo'));
tenant()->create('bar.localhost');
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$this->assertNotSame('bar', cache()->get('foo'));
@ -61,13 +63,13 @@ class CacheManagerTest extends TestCase
/** @test */
public function invoking_the_cache_helper_works()
{
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 1);
$this->assertSame('bar', cache('foo'));
tenant()->create('bar.localhost');
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$this->assertNotSame('bar', cache('foo'));
@ -79,13 +81,13 @@ class CacheManagerTest extends TestCase
/** @test */
public function cache_is_persisted()
{
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->end();
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));
@ -94,15 +96,15 @@ class CacheManagerTest extends TestCase
/** @test */
public function cache_is_persisted_when_reidentification_is_used()
{
tenant()->create('foo.localhost');
tenant()->create('bar.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->init('bar.localhost');
tenancy()->end();
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
class CommandsTest extends TestCase
@ -40,23 +41,23 @@ class CommandsTest extends TestCase
$this->assertFalse(Schema::hasTable('users'));
Artisan::call('tenants:migrate');
$this->assertFalse(Schema::hasTable('users'));
tenancy()->init('localhost');
tenancy()->init('test.localhost');
$this->assertTrue(Schema::hasTable('users'));
}
/** @test */
public function migrate_command_works_with_tenants_option()
{
$tenant = tenant()->create('test.localhost');
$tenant = Tenant::new()->withDomains(['test2.localhost'])->save();
Artisan::call('tenants:migrate', [
'--tenants' => [$tenant['id']],
]);
$this->assertFalse(Schema::hasTable('users'));
tenancy()->init('localhost');
tenancy()->init('test.localhost');
$this->assertFalse(Schema::hasTable('users'));
tenancy()->init('test.localhost');
tenancy()->init('test2.localhost');
$this->assertTrue(Schema::hasTable('users'));
}
@ -65,7 +66,7 @@ class CommandsTest extends TestCase
{
Artisan::call('tenants:migrate');
$this->assertFalse(Schema::hasTable('users'));
tenancy()->init('localhost');
tenancy()->init('test.localhost');
$this->assertTrue(Schema::hasTable('users'));
Artisan::call('tenants:rollback');
$this->assertFalse(Schema::hasTable('users'));
@ -98,7 +99,7 @@ class CommandsTest extends TestCase
/** @test */
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();
}
@ -106,9 +107,9 @@ class CommandsTest extends TestCase
/** @test */
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'")
->expectsOutput("User's name is Test command")
@ -128,6 +129,7 @@ class CommandsTest extends TestCase
\mkdir($dir, 0777, true);
}
// todo move this to a file
\file_put_contents(app_path('Http/Kernel.php'), "<?php
namespace App\Http;

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use GlobalCache;
use Stancl\Tenancy\Tenant;
class GlobalCacheTest extends TestCase
{
@ -18,7 +19,7 @@ class GlobalCacheTest extends TestCase
GlobalCache::put(['foo' => 'bar'], 1);
$this->assertSame('bar', GlobalCache::get('foo'));
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
$this->assertSame('bar', GlobalCache::get('foo'));
@ -26,12 +27,12 @@ class GlobalCacheTest extends TestCase
cache(['def' => 'ghi'], 10);
$this->assertSame('ghi', cache('def'));
tenancy()->end();
tenancy()->endTenancy();
$this->assertSame('xyz', GlobalCache::get('abc'));
$this->assertSame('bar', GlobalCache::get('foo'));
$this->assertSame(null, cache('def'));
tenant()->create('bar.localhost');
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$this->assertSame('xyz', GlobalCache::get('abc'));
$this->assertSame('bar', GlobalCache::get('foo'));

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant;
class ReidentificationTest extends TestCase
{
public $autoInitTenancy = false;
@ -21,7 +23,7 @@ class ReidentificationTest extends TestCase
}
tenancy()->init('localhost');
tenant()->create('second.localhost');
Tenant::new()->withDomains(['second.localhost'])->save();
tenancy()->init('second.localhost');
foreach (config('tenancy.filesystem.disks') as $disk) {
@ -48,7 +50,7 @@ class ReidentificationTest extends TestCase
$original = storage_path();
tenancy()->init('localhost');
tenant()->create('second.localhost');
Tenant::new()->withDomains(['second.localhost'])->save();
tenancy()->init('second.localhost');
$suffix = config('tenancy.filesystem.suffix_base') . tenant('id');

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Tenant;
class TenantManagerTest extends TestCase
{
@ -15,7 +16,7 @@ class TenantManagerTest extends TestCase
/** @test */
public function current_tenant_is_stored_in_the_tenant_property()
{
$tenant = tenant()->create('localhost');
$tenant = Tenant::new()->withDomains(['localhost'])->save();
tenancy()->init('localhost');
@ -25,7 +26,7 @@ class TenantManagerTest extends TestCase
/** @test */
public function invoke_works()
{
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
$this->assertSame(tenant('id'), tenant()('id'));
@ -34,7 +35,7 @@ class TenantManagerTest extends TestCase
/** @test */
public function initById_works()
{
$tenant = tenant()->create('foo.localhost');
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
$this->assertNotSame($tenant, tenancy()->tenant);
@ -46,7 +47,7 @@ class TenantManagerTest extends TestCase
/** @test */
public function findByDomain_works()
{
$tenant = tenant()->create('foo.localhost');
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
$this->assertSame($tenant, tenant()->findByDomain('foo.localhost'));
}
@ -54,14 +55,14 @@ class TenantManagerTest extends TestCase
/** @test */
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'));
}
/** @test */
public function find_works()
{
tenant()->create('dev.localhost');
Tenant::new()->withDomains(['dev.localhost'])->save();
tenancy()->init('dev.localhost');
$this->assertSame(tenant()->tenant, tenant()->find(tenant('id')));
@ -70,34 +71,17 @@ class TenantManagerTest extends TestCase
/** @test */
public function getTenantById_works()
{
$tenant = tenant()->create('foo.localhost');
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
$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 */
public function create_returns_the_supplied_domain()
{
$domain = 'foo.localhost';
$this->assertSame($domain, tenant()->create($domain)['domain']);
$this->assertSame($domain, Tenant::new()->withDomains([$domain])->save()['domain']);
}
/** @test */
@ -123,7 +107,7 @@ class TenantManagerTest extends TestCase
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertSame($originals['cache'], app('cache'));
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
$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['cache'], app('cache'));
tenancy()->end();
tenancy()->endTenancy();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
@ -149,7 +133,7 @@ class TenantManagerTest extends TestCase
'cache' => app('cache'),
];
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
$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['cache'], app('cache'));
tenancy()->end();
tenancy()->endTenancy();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
@ -165,7 +149,7 @@ class TenantManagerTest extends TestCase
$this->assertSame($originals['cache'], app('cache'));
// Reidentify tenant
tenant()->create('bar.localhost');
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$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['cache'], app('cache'));
tenancy()->end();
tenancy()->endTenancy();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
@ -184,27 +168,27 @@ class TenantManagerTest extends TestCase
/** @test */
public function tenant_can_be_deleted()
{
$tenant = tenant()->create('foo.localhost');
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
tenant()->delete($tenant['id']);
$this->assertSame([], tenancy()->all()->toArray());
$tenant = tenant()->create('foo.localhost');
$tenant = Tenant::new()->withDomains(['foo.localhost'])->save();
$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());
$tenant1 = Tenant::new()->withDomains(['foo.localhost'])->save();
$tenant2 = Tenant::new()->withDomains(['bar.localhost'])->save();
$this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenancy()->all()->toArray());
}
/** @test */
public function properites_can_be_passed_in_the_create_method()
{
$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;
unset($tenant_data['id']);

View file

@ -14,13 +14,13 @@ class TenantStorageTest extends TestCase
/** @test */
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']);
$this->assertFalse(tenant()->all()->contains($abc));
$this->assertFalse(tenancy()->all()->contains($abc));
}
/** @test */
@ -62,7 +62,7 @@ class TenantStorageTest extends TestCase
/** @test */
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'];
tenancy()->put('foo', 'bar', $id);
@ -74,7 +74,7 @@ class TenantStorageTest extends TestCase
/** @test */
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'];
$keys = ['foo', 'abc'];
@ -159,14 +159,14 @@ class TenantStorageTest extends TestCase
/** @test */
public function retrieving_data_without_cache_works()
{
tenant()->create('foo.localhost');
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
tenancy()->put('foo', 'bar');
$this->assertSame('bar', tenancy()->get('foo'));
$this->assertSame(['bar'], tenancy()->get(['foo']));
tenancy()->end();
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', tenancy()->get('foo'));
$this->assertSame(['bar'], tenancy()->get(['foo']));
@ -179,7 +179,7 @@ class TenantStorageTest extends TestCase
$this->markTestSkipped();
}
tenancy()->end();
tenancy()->endTenancy();
$this->loadMigrationsFrom([
'--path' => __DIR__ . '/Etc',

View file

@ -106,13 +106,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'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') {
$app['config']->set([
'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
}
}