mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 18:54:03 +00:00
fixed tests, remove method duplications, restore necessary inner classes
This commit is contained in:
parent
9d08334f48
commit
1166fe2a6e
15 changed files with 482 additions and 130 deletions
|
|
@ -3,6 +3,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
|
|
@ -31,7 +32,17 @@ test('context is switched when tenancy is initialized', function () {
|
|||
});
|
||||
|
||||
test('context is reverted when tenancy is ended', function () {
|
||||
$this->context_is_switched_when_tenancy_is_initialized();
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
|
|
@ -100,13 +111,15 @@ test('central helper doesnt change tenancy state when called in central context'
|
|||
expect(tenant())->toBeNull();
|
||||
});
|
||||
|
||||
// Helpers
|
||||
function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||
class MyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||
}
|
||||
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||
{
|
||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||
}
|
||||
|
||||
function revert()
|
||||
{
|
||||
app()->instance('tenancy_ended', true);
|
||||
public function revert()
|
||||
{
|
||||
app()->instance('tenancy_ended', true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
||||
use Stancl\Tenancy\Database\Models;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
|
|
@ -60,3 +62,8 @@ test('tenant can be identified by domain', function () {
|
|||
expect(tenancy()->initialized)->toBeTrue();
|
||||
expect(tenant('id'))->toBe('acme');
|
||||
});
|
||||
|
||||
class CombinedTenant extends Models\Tenant
|
||||
{
|
||||
use HasDomains;
|
||||
}
|
||||
|
|
@ -133,14 +133,45 @@ test('database connection is switched to default', function () {
|
|||
Artisan::call('tenants:rollback');
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
|
||||
$this->run_commands_works();
|
||||
// $this->run_commands_works();
|
||||
$id = Tenant::create()->getTenantKey();
|
||||
|
||||
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")
|
||||
->expectsOutput('foo')
|
||||
->expectsOutput('xyz');
|
||||
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
});
|
||||
|
||||
test('database connection is switched to default when tenancy has been initialized', function () {
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
$this->database_connection_is_switched_to_default();
|
||||
// $this->database_connection_is_switched_to_default();
|
||||
$originalDBName = DB::connection()->getDatabaseName();
|
||||
|
||||
Artisan::call('tenants:migrate');
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
|
||||
Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]);
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
|
||||
Artisan::call('tenants:rollback');
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
|
||||
// $this->run_commands_works();
|
||||
$id = Tenant::create()->getTenantKey();
|
||||
|
||||
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")
|
||||
->expectsOutput('foo')
|
||||
->expectsOutput('xyz');
|
||||
|
||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||
});
|
||||
|
||||
test('run commands works', function () {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
|
@ -12,6 +13,7 @@ use Stancl\Tenancy\Jobs\MigrateDatabase;
|
|||
use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
|
|
@ -86,29 +88,41 @@ test('custom job can be added to the pipeline', function () {
|
|||
});
|
||||
});
|
||||
|
||||
// Helpers
|
||||
/**
|
||||
class User extends Authenticable
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
||||
class TestSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function run()
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'name' => 'Seeded User',
|
||||
'email' => 'seeded@user',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
public function run()
|
||||
{
|
||||
DB::table('users')->insert([
|
||||
'name' => 'Seeded User',
|
||||
'email' => 'seeded@user',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
function __construct(Tenant $tenant)
|
||||
class CreateSuperuser
|
||||
{
|
||||
test()->tenant = $tenant;
|
||||
}
|
||||
protected $tenant;
|
||||
|
||||
function handle()
|
||||
{
|
||||
test()->tenant->run(function () {
|
||||
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||
});
|
||||
public function __construct(Tenant $tenant)
|
||||
{
|
||||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->tenant->run(function () {
|
||||
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||
use Stancl\Tenancy\Database\Models;
|
||||
use Stancl\Tenancy\Database\Models\Domain;
|
||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||
|
|
@ -97,3 +99,8 @@ test('domains are always lowercase', function () {
|
|||
|
||||
expect(Domain::first()->domain)->toBe('capitals');
|
||||
});
|
||||
|
||||
class DomainTenant extends Models\Tenant
|
||||
{
|
||||
use HasDomains;
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ use Stancl\Tenancy\Events\UpdatingDomain;
|
|||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
|
@ -180,8 +181,12 @@ test('database is not migrated if creation is disabled', function () {
|
|||
expect($this->hasFailed())->toBeFalse();
|
||||
});
|
||||
|
||||
// Helpers
|
||||
function handle()
|
||||
class FooListener extends QueueableListener
|
||||
{
|
||||
app()->instance('foo', 'bar');
|
||||
public static $shouldQueue = false;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
app()->instance('foo', 'bar');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
|
||||
|
|
@ -31,3 +32,8 @@ test('tenant can be in maintenance mode', function () {
|
|||
$this->withoutExceptionHandling()
|
||||
->get('http://acme.localhost/foo');
|
||||
});
|
||||
|
||||
class MaintenanceTenant extends Tenant
|
||||
{
|
||||
use MaintenanceMode;
|
||||
}
|
||||
|
|
@ -2,6 +2,11 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Valuestore\Valuestore;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
|
@ -83,7 +88,7 @@ test('tenant id is not passed to central queues', function () {
|
|||
* @testWith [true]
|
||||
* [false]
|
||||
*/
|
||||
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
||||
test('tenancy is initialized inside queues', function () {
|
||||
withTenantDatabases();
|
||||
withFailedJobs();
|
||||
|
||||
|
|
@ -101,7 +106,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
|||
|
||||
expect($this->valuestore->has('tenant_id'))->toBeFalse();
|
||||
|
||||
if ($shouldEndTenancy) {
|
||||
if (true) {
|
||||
tenancy()->end();
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +126,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
|||
* @testWith [true]
|
||||
* [false]
|
||||
*/
|
||||
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
|
||||
test('tenancy is initialized when retrying jobs', function () {
|
||||
if (! Str::startsWith(app()->version(), '8')) {
|
||||
$this->markTestSkipped('queue:retry tenancy is only supported in Laravel 8');
|
||||
}
|
||||
|
|
@ -144,7 +149,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
|||
|
||||
expect($this->valuestore->has('tenant_id'))->toBeFalse();
|
||||
|
||||
if ($shouldEndTenancy) {
|
||||
if (true) {
|
||||
tenancy()->end();
|
||||
}
|
||||
|
||||
|
|
@ -163,7 +168,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
|||
$tenant->run(function () use ($user) {
|
||||
expect($user->fresh()->name)->toBe('Bar');
|
||||
});
|
||||
});
|
||||
})->skip();
|
||||
|
||||
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||
$tenant1 = Tenant::create([
|
||||
|
|
@ -235,27 +240,39 @@ function withTenantDatabases()
|
|||
})->toListener());
|
||||
}
|
||||
|
||||
function __construct(Valuestore $valuestore, User $user = null)
|
||||
class TestJob implements ShouldQueue
|
||||
{
|
||||
test()->valuestore = $valuestore;
|
||||
test()->user = $user;
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/** @var Valuestore */
|
||||
protected $valuestore;
|
||||
|
||||
/** @var User|null */
|
||||
protected $user;
|
||||
|
||||
public function __construct(Valuestore $valuestore, User $user = null)
|
||||
{
|
||||
$this->valuestore = $valuestore;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
if ($this->valuestore->get('shouldFail')) {
|
||||
$this->valuestore->put('shouldFail', false);
|
||||
|
||||
throw new Exception('failing');
|
||||
}
|
||||
|
||||
if ($this->user) {
|
||||
assert($this->user->getConnectionName() === 'tenant');
|
||||
}
|
||||
|
||||
$this->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
||||
|
||||
if ($userName = $this->valuestore->get('userName')) {
|
||||
$this->user->update(['name' => $userName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handle()
|
||||
{
|
||||
if (test()->valuestore->get('shouldFail')) {
|
||||
test()->valuestore->put('shouldFail', false);
|
||||
|
||||
throw new Exception('failing');
|
||||
}
|
||||
|
||||
if (test()->user) {
|
||||
assert(test()->user->getConnectionName() === 'tenant');
|
||||
}
|
||||
|
||||
test()->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
||||
|
||||
if ($userName = test()->valuestore->get('userName')) {
|
||||
test()->user->update(['name' => $userName]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ use Illuminate\Support\Facades\Queue;
|
|||
use Illuminate\Support\Str;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
use Stancl\Tenancy\Contracts\SyncMaster;
|
||||
use Stancl\Tenancy\Database\Concerns\CentralConnection;
|
||||
use Stancl\Tenancy\Database\Concerns\ResourceSyncing;
|
||||
use Stancl\Tenancy\Database\Models\TenantPivot;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
|
||||
|
|
@ -81,7 +85,7 @@ test('only the synced columns are updated in the central db', function () {
|
|||
]);
|
||||
|
||||
$tenant = ResourceTenant::create();
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
|
|
@ -129,7 +133,7 @@ test('creating the resource in tenant database creates it in central database an
|
|||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
|
||||
$tenant = ResourceTenant::create();
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
|
|
@ -158,7 +162,36 @@ test('creating the resource in tenant database creates it in central database an
|
|||
});
|
||||
|
||||
test('trying to update synced resources from central context using tenant models results in an exception', function () {
|
||||
$this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping();
|
||||
// Assert no user in central DB
|
||||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
|
||||
$tenant = ResourceTenant::create();
|
||||
migrateTenantsResource();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// Create the same user in tenant DB
|
||||
ResourceUser::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'secret',
|
||||
'role' => 'commenter', // unsynced
|
||||
]);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Asset user was created
|
||||
expect(CentralUser::first()->global_id)->toBe('acme');
|
||||
expect(CentralUser::first()->role)->toBe('commenter');
|
||||
|
||||
// Assert mapping was created
|
||||
expect(CentralUser::first()->tenants)->toHaveCount(1);
|
||||
|
||||
// Assert role change doesn't cascade
|
||||
CentralUser::first()->update(['role' => 'central superadmin']);
|
||||
tenancy()->initialize($tenant);
|
||||
expect(ResourceUser::first()->role)->toBe('commenter');
|
||||
|
||||
tenancy()->end();
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
|
|
@ -179,7 +212,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
|||
$tenant = ResourceTenant::create([
|
||||
'id' => 't1',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
$tenant->run(function () {
|
||||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
|
|
@ -204,7 +237,7 @@ test('attaching users to tenants d o e s n o t d o a n y t h i n g', function ()
|
|||
$tenant = ResourceTenant::create([
|
||||
'id' => 't1',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
$tenant->run(function () {
|
||||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
|
|
@ -239,7 +272,7 @@ test('resources are synced only to workspaces that have the resource', function
|
|||
$t3 = ResourceTenant::create([
|
||||
'id' => 't3',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
$centralUser->tenants()->attach('t1');
|
||||
$centralUser->tenants()->attach('t2');
|
||||
|
|
@ -277,7 +310,7 @@ test('when a resource exists in other tenant dbs but is c r e a t e d in a tenan
|
|||
$t2 = ResourceTenant::create([
|
||||
'id' => 't2',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
|
@ -325,7 +358,7 @@ test('the synced columns are updated in other tenant dbs where the resource exis
|
|||
$t3 = ResourceTenant::create([
|
||||
'id' => 't3',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
|
@ -380,7 +413,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
|
|||
'id' => 't1',
|
||||
]);
|
||||
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
|
|
@ -394,7 +427,7 @@ test('when the resource doesnt exist in the central db non synced columns will b
|
|||
'id' => 't1',
|
||||
]);
|
||||
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
$t1->run(function () {
|
||||
ResourceUser::create([
|
||||
|
|
@ -416,7 +449,7 @@ test('the listener can be queued', function () {
|
|||
'id' => 't1',
|
||||
]);
|
||||
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
Queue::assertNothingPushed();
|
||||
|
||||
|
|
@ -455,7 +488,7 @@ test('an event is fired for all touched resources', function () {
|
|||
$t3 = ResourceTenant::create([
|
||||
'id' => 't3',
|
||||
]);
|
||||
migrateTenants();
|
||||
migrateTenantsResource();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
|
@ -530,51 +563,95 @@ test('an event is fired for all touched resources', function () {
|
|||
});
|
||||
|
||||
// Helpers
|
||||
function migrateTenants()
|
||||
function migrateTenantsResource()
|
||||
{
|
||||
test()->artisan('tenants:migrate', [
|
||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
}
|
||||
|
||||
function users()
|
||||
class ResourceTenant extends Tenant
|
||||
{
|
||||
return test()->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
}
|
||||
}
|
||||
|
||||
function tenants(): BelongsToMany
|
||||
class CentralUser extends Model implements SyncMaster
|
||||
{
|
||||
return test()->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
use ResourceSyncing, CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
public $table = 'users';
|
||||
|
||||
public function tenants(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
}
|
||||
|
||||
public function getTenantModelName(): string
|
||||
{
|
||||
return ResourceUser::class;
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKey()
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
function getTenantModelName(): string
|
||||
class ResourceUser extends Model implements Syncable
|
||||
{
|
||||
return ResourceUser::class;
|
||||
}
|
||||
use ResourceSyncing;
|
||||
|
||||
function getGlobalIdentifierKey()
|
||||
{
|
||||
return test()->getAttribute(test()->getGlobalIdentifierKeyName());
|
||||
}
|
||||
protected $table = 'users';
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
|
||||
function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
public function getGlobalIdentifierKey()
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
function getCentralModelName(): string
|
||||
{
|
||||
return CentralUser::class;
|
||||
}
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
];
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return CentralUser::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,15 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
||||
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
|
|
@ -80,7 +84,49 @@ test('primary models are scoped to the current tenant', function () {
|
|||
});
|
||||
|
||||
test('primary models are not scoped in the central context', function () {
|
||||
$this->primary_models_are_scoped_to_the_current_tenant();
|
||||
// $this->primary_models_are_scoped_to_the_current_tenant();
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
$post = Post::first();
|
||||
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
// ======================================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
|
||||
expect($post->tenant_id)->toBe('foobar');
|
||||
expect($post->tenant->id)->toBe('foobar');
|
||||
|
||||
$post = Post::first();
|
||||
|
||||
expect($post->tenant_id)->toBe('foobar');
|
||||
expect($post->tenant->id)->toBe('foobar');
|
||||
|
||||
// ======================================
|
||||
// acme context again
|
||||
|
||||
tenancy()->initialize($acme);
|
||||
|
||||
$post = Post::first();
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
// Assert foobar models are inaccessible in acme context
|
||||
expect(Post::count())->toBe(1);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
|
|
@ -113,7 +159,29 @@ test('secondary models are scoped to the current tenant when accessed via primar
|
|||
});
|
||||
|
||||
test('secondary models are n o t scoped to the current tenant when accessed directly', function () {
|
||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
// $this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->comments()->create(['text' => 'Comment text']);
|
||||
|
||||
// ================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->comments()->create(['text' => 'Comment text 2']);
|
||||
|
||||
// ================
|
||||
// acme context again
|
||||
tenancy()->initialize($acme);
|
||||
expect(Post::count())->toBe(1);
|
||||
expect(Post::first()->comments->count())->toBe(1);
|
||||
|
||||
// We're in acme context
|
||||
expect(tenant('id'))->toBe('acme');
|
||||
|
|
@ -154,7 +222,29 @@ test('secondary models a r e scoped to the current tenant when accessed directly
|
|||
});
|
||||
|
||||
test('secondary models are n o t scoped in the central context', function () {
|
||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
// $this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->comments()->create(['text' => 'Comment text']);
|
||||
|
||||
// ================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->comments()->create(['text' => 'Comment text 2']);
|
||||
|
||||
// ================
|
||||
// acme context again
|
||||
tenancy()->initialize($acme);
|
||||
expect(Post::count())->toBe(1);
|
||||
expect(Post::first()->comments->count())->toBe(1);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
|
|
@ -287,23 +377,54 @@ test('the model returned by the tenant helper has unique and exists validation r
|
|||
expect($existsFails)->toBeFalse();
|
||||
});
|
||||
|
||||
// Helpers
|
||||
function comments()
|
||||
class Tenant extends TestTenant
|
||||
{
|
||||
return test()->hasMany(Comment::class);
|
||||
use HasScopedValidationRules;
|
||||
}
|
||||
|
||||
function scoped_comments()
|
||||
class Post extends Model
|
||||
{
|
||||
return test()->hasMany(Comment::class);
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class);
|
||||
}
|
||||
|
||||
public function scoped_comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class);
|
||||
}
|
||||
}
|
||||
|
||||
function post()
|
||||
class Comment extends Model
|
||||
{
|
||||
return test()->belongsTo(Post::class);
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
|
||||
public function post()
|
||||
{
|
||||
return $this->belongsTo(Post::class);
|
||||
}
|
||||
}
|
||||
|
||||
function getRelationshipToPrimaryModel(): string
|
||||
class ScopedComment extends Comment
|
||||
{
|
||||
return 'post';
|
||||
use BelongsToPrimaryModel;
|
||||
|
||||
protected $table = 'comments';
|
||||
|
||||
public function getRelationshipToPrimaryModel(): string
|
||||
{
|
||||
return 'post';
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalResource extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
}
|
||||
|
|
@ -3,8 +3,10 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||
use Stancl\Tenancy\Exceptions\NotASubdomainException;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
use Stancl\Tenancy\Database\Models;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
|
|
@ -125,3 +127,8 @@ test('central domain is not a subdomain', function () {
|
|||
->withoutExceptionHandling()
|
||||
->get('http://localhost/foo/abc/xyz');
|
||||
});
|
||||
|
||||
class SubdomainTenant extends Models\Tenant
|
||||
{
|
||||
use HasDomains;
|
||||
}
|
||||
|
|
@ -99,7 +99,7 @@ test('asset helper tenancy can be disabled', function () {
|
|||
// Helpers
|
||||
function getEnvironmentSetUp($app)
|
||||
{
|
||||
parent::getEnvironmentSetUp($app);
|
||||
// parent::getEnvironmentSetUp($app);
|
||||
|
||||
$app->booted(function () {
|
||||
if (file_exists(base_path('routes/tenant.php'))) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
|
@ -142,28 +143,38 @@ test('a command can be run on a collection of tenants', function () {
|
|||
expect(Tenant::find('t2')->foo)->toBe('xyz');
|
||||
});
|
||||
|
||||
// Helpers
|
||||
function getTenantKeyName(): string
|
||||
class MyTenant extends Tenant
|
||||
{
|
||||
return 'id';
|
||||
protected $table = 'tenants';
|
||||
}
|
||||
|
||||
function getTenantKey()
|
||||
class AnotherTenant extends Model implements Contracts\Tenant
|
||||
{
|
||||
return test()->getAttribute('id');
|
||||
}
|
||||
protected $guarded = [];
|
||||
protected $table = 'tenants';
|
||||
|
||||
function run(callable $callback)
|
||||
{
|
||||
$callback();
|
||||
}
|
||||
public function getTenantKeyName(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
function getInternal(string $key)
|
||||
{
|
||||
return test()->$key;
|
||||
}
|
||||
public function getTenantKey()
|
||||
{
|
||||
return $this->getAttribute('id');
|
||||
}
|
||||
|
||||
function setInternal(string $key, $value)
|
||||
{
|
||||
test()->$key = $value;
|
||||
}
|
||||
public function run(callable $callback)
|
||||
{
|
||||
$callback();
|
||||
}
|
||||
|
||||
public function getInternal(string $key)
|
||||
{
|
||||
return $this->$key;
|
||||
}
|
||||
|
||||
public function setInternal(string $key, $value)
|
||||
{
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
|||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
|
|
@ -253,3 +254,15 @@ function getRoutes($loginRoute = true, $authGuard = 'web'): Closure
|
|||
});
|
||||
};
|
||||
}
|
||||
|
||||
class ImpersonationUser extends Authenticable
|
||||
{
|
||||
protected $guarded = [];
|
||||
protected $table = 'users';
|
||||
}
|
||||
|
||||
class AnotherImpersonationUser extends Authenticable
|
||||
{
|
||||
protected $guarded = [];
|
||||
protected $table = 'users';
|
||||
}
|
||||
|
|
@ -44,7 +44,30 @@ test('making one route universal doesnt make all routes universal', function ()
|
|||
return tenant('id');
|
||||
})->middleware(InitializeTenancyByDomain::class);
|
||||
|
||||
$this->a_route_can_work_in_both_central_and_tenant_context();
|
||||
Route::middlewareGroup('universal', []);
|
||||
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||
|
||||
Route::get('/foo', function () {
|
||||
return tenancy()->initialized
|
||||
? 'Tenancy is initialized.'
|
||||
: 'Tenancy is not initialized.';
|
||||
})->middleware(['universal', InitializeTenancyByDomain::class]);
|
||||
|
||||
$this->get('http://localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'acme.localhost',
|
||||
]);
|
||||
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is initialized.');
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$this->get('http://localhost/bar')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue