mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 13:14:05 +00:00
Merge branch 'master' of github.com:archtechx/tenancy into unware-feature
This commit is contained in:
commit
b0560331ed
87 changed files with 2308 additions and 533 deletions
|
|
@ -50,6 +50,8 @@ test('context is switched when tenancy is reinitialized', function () {
|
|||
});
|
||||
|
||||
test('central helper runs callbacks in the central state', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
|
|
@ -60,6 +62,8 @@ test('central helper runs callbacks in the central state', function () {
|
|||
});
|
||||
|
||||
test('central helper returns the value from the callback', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
pest()->assertSame('foo', tenancy()->central(function () {
|
||||
|
|
@ -68,6 +72,8 @@ test('central helper returns the value from the callback', function () {
|
|||
});
|
||||
|
||||
test('central helper reverts back to tenant context', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ beforeEach(function () {
|
|||
});
|
||||
|
||||
test('batch repository is set to tenant connection and reverted', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,28 +4,37 @@ declare(strict_types=1);
|
|||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Events\DeletingTenant;
|
||||
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
|
|
@ -326,20 +335,176 @@ test('local storage public urls are generated correctly', function() {
|
|||
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper binds TenancyBroadcastManager to BroadcastManager and reverts the binding when tenancy is ended', function() {
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(TenancyBroadcastManager::class);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
|
||||
config([
|
||||
'broadcasting.connections.testing.driver' => 'testing',
|
||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
||||
]);
|
||||
|
||||
BroadcastTenancyBootstrapper::$credentialsMap = [
|
||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
||||
];
|
||||
|
||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(array_key_exists('testing_broadcaster_message', tenant()->getAttributes()))->toBeTrue();
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($tenantMessage);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($secondTenantMessage);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper makes the app use broadcasters with the correct credentials', function() {
|
||||
config([
|
||||
'broadcasting.default' => 'testing',
|
||||
'broadcasting.connections.testing.driver' => 'testing',
|
||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
||||
]);
|
||||
|
||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'testing';
|
||||
BroadcastTenancyBootstrapper::$credentialsMap = [
|
||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
||||
];
|
||||
|
||||
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster($config['message']));
|
||||
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
||||
|
||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($tenantMessage);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($secondTenantMessage);
|
||||
|
||||
tenancy()->end();
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
||||
});
|
||||
|
||||
test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = [
|
||||
'mail.mailers.smtp.username' => 'smtp_username',
|
||||
'mail.mailers.smtp.password' => 'smtp_password'
|
||||
];
|
||||
|
||||
config([
|
||||
'mail.default' => 'smtp',
|
||||
'mail.mailers.smtp.username' => $defaultUsername = 'default username',
|
||||
'mail.mailers.smtp.password' => 'no password'
|
||||
]);
|
||||
|
||||
$tenant = Tenant::create(['smtp_password' => $password = 'testing password']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(array_key_exists('smtp_password', tenant()->getAttributes()))->toBeTrue();
|
||||
expect(array_key_exists('smtp_host', tenant()->getAttributes()))->toBeFalse();
|
||||
expect(config('mail.mailers.smtp.username'))->toBe($defaultUsername);
|
||||
expect(config('mail.mailers.smtp.password'))->toBe(tenant()->smtp_password);
|
||||
|
||||
// Assert that the current mailer uses tenant's smtp_password
|
||||
assertMailerTransportUsesPassword($password);
|
||||
});
|
||||
|
||||
test('MailTenancyBootstrapper reverts the config and mailer credentials to default when tenancy ends', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'no password']);
|
||||
|
||||
tenancy()->initialize(Tenant::create(['smtp_password' => $tenantPassword = 'testing password']));
|
||||
|
||||
expect(config('mail.mailers.smtp.password'))->toBe($tenantPassword);
|
||||
|
||||
assertMailerTransportUsesPassword($tenantPassword);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(config('mail.mailers.smtp.password'))->toBe($defaultPassword);
|
||||
|
||||
// Assert that the current mailer uses the default SMTP password
|
||||
assertMailerTransportUsesPassword($defaultPassword);
|
||||
});
|
||||
|
||||
function getDiskPrefix(string $disk): string
|
||||
{
|
||||
/** @var FilesystemAdapter $disk */
|
||||
$disk = Storage::disk($disk);
|
||||
$adapter = $disk->getAdapter();
|
||||
$prefix = invade(invade($adapter)->prefixer)->prefix;
|
||||
|
||||
$prefixer = (new ReflectionObject($adapter))->getProperty('prefixer');
|
||||
$prefixer->setAccessible(true);
|
||||
|
||||
// reflection -> instance
|
||||
$prefixer = $prefixer->getValue($adapter);
|
||||
|
||||
$prefix = (new ReflectionProperty($prefixer, 'prefix'));
|
||||
$prefix->setAccessible(true);
|
||||
|
||||
return $prefix->getValue($prefixer);
|
||||
return $prefix;
|
||||
}
|
||||
|
||||
test('url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one after tenancy ends', function() {
|
||||
config(['tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class]);
|
||||
|
||||
Route::group([
|
||||
'middleware' => InitializeTenancyBySubdomain::class,
|
||||
], function () {
|
||||
Route::get('/', function () {
|
||||
return true;
|
||||
})->name('home');
|
||||
});
|
||||
|
||||
$baseUrl = url(route('home'));
|
||||
config(['app.url' => $baseUrl]);
|
||||
|
||||
$rootUrlOverride = function (Tenant $tenant) use ($baseUrl) {
|
||||
$scheme = str($baseUrl)->before('://');
|
||||
$hostname = str($baseUrl)->after($scheme . '://');
|
||||
|
||||
return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
|
||||
};
|
||||
|
||||
UrlTenancyBootstrapper::$rootUrlOverride = $rootUrlOverride;
|
||||
|
||||
$tenant = Tenant::create();
|
||||
$tenantUrl = $rootUrlOverride($tenant);
|
||||
|
||||
expect($tenantUrl)->not()->toBe($baseUrl);
|
||||
|
||||
expect(url(route('home')))->toBe($baseUrl);
|
||||
expect(URL::to('/'))->toBe($baseUrl);
|
||||
expect(config('app.url'))->toBe($baseUrl);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(url(route('home')))->toBe($tenantUrl);
|
||||
expect(URL::to('/'))->toBe($tenantUrl);
|
||||
expect(config('app.url'))->toBe($tenantUrl);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(url(route('home')))->toBe($baseUrl);
|
||||
expect(URL::to('/'))->toBe($baseUrl);
|
||||
expect(config('app.url'))->toBe($baseUrl);
|
||||
});
|
||||
|
|
|
|||
65
tests/BroadcastingTest.php
Normal file
65
tests/BroadcastingTest.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
||||
|
||||
beforeEach(function() {
|
||||
withTenantDatabases();
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
|
||||
config(['broadcasting.default' => 'null']);
|
||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'null';
|
||||
|
||||
$originalBroadcaster = app(BroadcasterContract::class);
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
// TenancyBroadcastManager binds new broadcaster
|
||||
$tenantBroadcaster = app(BroadcastManager::class)->driver();
|
||||
|
||||
expect($tenantBroadcaster)->not()->toBe($originalBroadcaster);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
||||
});
|
||||
|
||||
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
||||
config([
|
||||
'broadcasting.default' => $driver = 'testing',
|
||||
'broadcasting.connections.testing.driver' => $driver,
|
||||
]);
|
||||
|
||||
TenancyBroadcastManager::$tenantBroadcasters[] = $driver;
|
||||
|
||||
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing'));
|
||||
$getCurrentChannels = fn() => array_keys(invade(app(BroadcastManager::class)->driver())->channels);
|
||||
|
||||
$registerTestingBroadcaster();
|
||||
Broadcast::channel($channel = 'testing-channel', fn() => true);
|
||||
|
||||
expect($channel)->toBeIn($getCurrentChannels());
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect($channel)->toBeIn($getCurrentChannels());
|
||||
|
||||
tenancy()->end();
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect($channel)->toBeIn($getCurrentChannels());
|
||||
});
|
||||
|
|
@ -18,14 +18,16 @@ use Stancl\Tenancy\Events\TenantCreated;
|
|||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||
use Stancl\Tenancy\Events\DeletingTenant;
|
||||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
|
||||
|
||||
beforeEach(function () {
|
||||
if (file_exists($schemaPath = database_path('schema/tenant-schema.dump'))) {
|
||||
if (file_exists($schemaPath = 'tests/Etc/tenant-schema-test.dump')) {
|
||||
unlink($schemaPath);
|
||||
}
|
||||
|
||||
|
|
@ -109,30 +111,86 @@ test('migrate command loads schema state', function () {
|
|||
expect(Schema::hasTable('users'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('dump command works', function () {
|
||||
$tenant = Tenant::create();
|
||||
Artisan::call('tenants:migrate');
|
||||
test('migrate command only throws exceptions if skip-failing is not passed', function() {
|
||||
Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
$tenantWithoutDatabase = Tenant::create();
|
||||
$databaseToDrop = $tenantWithoutDatabase->run(fn() => DB::connection()->getDatabaseName());
|
||||
|
||||
Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"');
|
||||
expect('tests/Etc/tenant-schema-test.dump')->toBeFile();
|
||||
DB::statement("DROP DATABASE `$databaseToDrop`");
|
||||
|
||||
Tenant::create();
|
||||
|
||||
expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'))->toThrow(TenantDatabaseDoesNotExistException::class);
|
||||
expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump" --skip-failing'))->not()->toThrow(TenantDatabaseDoesNotExistException::class);
|
||||
});
|
||||
|
||||
test('tenant dump file gets created as tenant-schema.dump in the database schema folder by default', function() {
|
||||
config(['tenancy.migration_parameters.--schema-path' => $schemaPath = database_path('schema/tenant-schema.dump')]);
|
||||
test('migrate command does not stop after the first failure if skip-failing is passed', function() {
|
||||
$tenants = collect([
|
||||
Tenant::create(),
|
||||
$tenantWithoutDatabase = Tenant::create(),
|
||||
Tenant::create(),
|
||||
]);
|
||||
|
||||
$migratedTenants = 0;
|
||||
|
||||
Event::listen(DatabaseMigrated::class, function() use (&$migratedTenants) {
|
||||
$migratedTenants++;
|
||||
});
|
||||
|
||||
$databaseToDrop = $tenantWithoutDatabase->run(fn() => DB::connection()->getDatabaseName());
|
||||
|
||||
DB::statement("DROP DATABASE `$databaseToDrop`");
|
||||
|
||||
Artisan::call('tenants:migrate', [
|
||||
'--schema-path' => '"tests/Etc/tenant-schema.dump"',
|
||||
'--skip-failing' => true,
|
||||
'--tenants' => $tenants->pluck('id')->toArray(),
|
||||
]);
|
||||
|
||||
expect($migratedTenants)->toBe(2);
|
||||
});
|
||||
|
||||
test('dump command works', function () {
|
||||
$tenant = Tenant::create();
|
||||
$schemaPath = 'tests/Etc/tenant-schema-test.dump';
|
||||
|
||||
Artisan::call('tenants:migrate');
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
expect($schemaPath)->not()->toBeFile();
|
||||
|
||||
Artisan::call('tenants:dump');
|
||||
Artisan::call('tenants:dump ' . "--tenant='$tenant->id' --path='$schemaPath'");
|
||||
|
||||
expect($schemaPath)->toBeFile();
|
||||
});
|
||||
|
||||
test('migrate command uses the correct schema path by default', function () {
|
||||
test('dump command generates dump at the passed path', function() {
|
||||
$tenant = Tenant::create();
|
||||
|
||||
Artisan::call('tenants:migrate');
|
||||
|
||||
expect($schemaPath = 'tests/Etc/tenant-schema-test.dump')->not()->toBeFile();
|
||||
|
||||
Artisan::call("tenants:dump --tenant='$tenant->id' --path='$schemaPath'");
|
||||
|
||||
expect($schemaPath)->toBeFile();
|
||||
});
|
||||
|
||||
test('dump command generates dump at the path specified in the tenancy migration parameters config', function() {
|
||||
config(['tenancy.migration_parameters.--schema-path' => $schemaPath = 'tests/Etc/tenant-schema-test.dump']);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
Artisan::call('tenants:migrate');
|
||||
|
||||
expect($schemaPath)->not()->toBeFile();
|
||||
|
||||
Artisan::call("tenants:dump --tenant='$tenant->id'");
|
||||
|
||||
expect($schemaPath)->toBeFile();
|
||||
});
|
||||
|
||||
test('migrate command correctly uses the schema dump located at the configured schema path by default', function () {
|
||||
config(['tenancy.migration_parameters.--schema-path' => 'tests/Etc/tenant-schema.dump']);
|
||||
$tenant = Tenant::create();
|
||||
|
||||
|
|
@ -146,6 +204,7 @@ test('migrate command uses the correct schema path by default', function () {
|
|||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// schema_users is a table included in the tests/Etc/tenant-schema dump
|
||||
// Check for both tables to see if missing migrations also get executed
|
||||
expect(Schema::hasTable('schema_users'))->toBeTrue();
|
||||
expect(Schema::hasTable('users'))->toBeTrue();
|
||||
|
|
@ -163,17 +222,17 @@ test('rollback command works', function () {
|
|||
expect(Schema::hasTable('users'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('seed command works', function (){
|
||||
test('seed command works', function () {
|
||||
$tenant = Tenant::create();
|
||||
Artisan::call('tenants:migrate');
|
||||
|
||||
$tenant->run(function (){
|
||||
$tenant->run(function () {
|
||||
expect(DB::table('users')->count())->toBe(0);
|
||||
});
|
||||
|
||||
Artisan::call('tenants:seed', ['--class' => TestSeeder::class]);
|
||||
|
||||
$tenant->run(function (){
|
||||
$tenant->run(function () {
|
||||
$user = DB::table('users');
|
||||
expect($user->count())->toBe(1)
|
||||
->and($user->first()->email)->toBe('seeded@user');
|
||||
|
|
@ -355,7 +414,7 @@ function runCommandWorks(): void
|
|||
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
||||
|
||||
pest()->artisan("tenants:run --tenants=$id 'foo foo --b=bar --c=xyz' ")
|
||||
->expectsOutput("User's name is Test command")
|
||||
->expectsOutput("User's name is Test user")
|
||||
->expectsOutput('foo')
|
||||
->expectsOutput('xyz');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,68 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Stancl\Tenancy\Enums\LogMode;
|
||||
use Stancl\Tenancy\Events\EndingTenancy;
|
||||
use Stancl\Tenancy\Events\InitializingTenancy;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
test('tenancy can log events silently', function () {
|
||||
tenancy()->log(LogMode::SILENT);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
assertTenancyInitializedAndEnded(tenancy()->getLog(), $tenant);
|
||||
});
|
||||
|
||||
test('tenancy logs event silently by default', function () {
|
||||
tenancy()->log();
|
||||
|
||||
expect(tenancy()->logMode())->toBe(LogMode::SILENT);
|
||||
});
|
||||
|
||||
test('the log can be dumped', function (string $method) {
|
||||
tenancy()->log();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$output = [];
|
||||
tenancy()->$method(function ($data) use (&$output) {
|
||||
$output = $data;
|
||||
});
|
||||
|
||||
assertTenancyInitializedAndEnded($output, $tenant);
|
||||
})->with([
|
||||
'dump',
|
||||
'dd',
|
||||
]);
|
||||
|
||||
test('tenancy can log events immediately', function () {
|
||||
// todo implement
|
||||
pest()->markTestIncomplete();
|
||||
});
|
||||
|
||||
// todo test the different behavior of the methods in different contexts, or get rid of the logic and simplify it
|
||||
|
||||
function assertTenancyInitializedAndEnded(array $log, Tenant $tenant): void
|
||||
{
|
||||
expect($log)->toHaveCount(4);
|
||||
|
||||
expect($log[0]['event'])->toBe(InitializingTenancy::class);
|
||||
expect($log[0]['tenant'])->toBe($tenant);
|
||||
expect($log[1]['event'])->toBe(TenancyInitialized::class);
|
||||
expect($log[1]['tenant'])->toBe($tenant);
|
||||
|
||||
expect($log[2]['event'])->toBe(EndingTenancy::class);
|
||||
expect($log[2]['tenant'])->toBe($tenant);
|
||||
expect($log[3]['event'])->toBe(TenancyEnded::class);
|
||||
expect($log[3]['tenant'])->toBe($tenant);
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ beforeEach(function () {
|
|||
config(['tenancy.models.tenant' => DatabaseAndDomainTenant::class]);
|
||||
});
|
||||
|
||||
test('job delete domains successfully', function (){
|
||||
test('job delete domains successfully', function () {
|
||||
$tenant = DatabaseAndDomainTenant::create();
|
||||
|
||||
$tenant->domains()->create([
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ use Stancl\Tenancy\Database\Models;
|
|||
use Stancl\Tenancy\Database\Models\Domain;
|
||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||
use Stancl\Tenancy\Features\UniversalRoutes;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
|
||||
|
|
@ -95,7 +94,6 @@ test('throw correct exception when onFail is null and universal routes are enabl
|
|||
|
||||
// Enable UniversalRoute feature
|
||||
Route::middlewareGroup('universal', []);
|
||||
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||
|
||||
$this->withoutExceptionHandling()->get('http://foo.localhost/foo/abc/xyz');
|
||||
})->throws(TenantCouldNotBeIdentifiedOnDomainException::class);;
|
||||
|
|
|
|||
104
tests/EarlyIdentificationTest.php
Normal file
104
tests/EarlyIdentificationTest.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
|
||||
use Stancl\Tenancy\Tests\Etc\EarlyIdentification\Controller;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
beforeEach(function () {
|
||||
config()->set([
|
||||
'tenancy.token' => 'central-abc123',
|
||||
]);
|
||||
|
||||
Event::listen(TenancyInitialized::class, function (TenancyInitialized $event) {
|
||||
config()->set([
|
||||
'tenancy.token' => $event->tenancy->tenant->getTenantKey() . '-abc123',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
test('early identification works with path identification', function () {
|
||||
app(Kernel::class)->pushMiddleware(InitializeTenancyByPath::class);
|
||||
|
||||
Route::group([
|
||||
'prefix' => '/{tenant}',
|
||||
], function () {
|
||||
Route::get('/foo', [Controller::class, 'index'])->name('foo');
|
||||
});
|
||||
|
||||
Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$response = pest()->get('/acme/foo')->assertOk();
|
||||
|
||||
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
||||
|
||||
// check if default parameter feature is working fine by asserting that the route WITHOUT the tenant parameter
|
||||
// matches the route WITH the tenant parameter
|
||||
expect(route('foo'))->toBe(route('foo', ['tenant' => 'acme']));
|
||||
});
|
||||
|
||||
test('early identification works with request data identification', function (string $type) {
|
||||
app(Kernel::class)->pushMiddleware(InitializeTenancyByRequestData::class);
|
||||
|
||||
Route::get('/foo', [Controller::class, 'index'])->name('foo');
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
if ($type === 'header') {
|
||||
$response = pest()->get('/foo', ['X-Tenant' => $tenant->id])->assertOk();
|
||||
} elseif ($type === 'queryParameter') {
|
||||
$response = pest()->get("/foo?tenant=$tenant->id")->assertOk();
|
||||
}
|
||||
|
||||
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
||||
})->with([
|
||||
'using request header parameter' => 'header',
|
||||
'using request query parameter' => 'queryParameter'
|
||||
]);
|
||||
|
||||
// The name of this test is suffixed by the dataset — domain / subdomain / domainOrSubdomain identification
|
||||
test('early identification works', function (string $middleware, string $domain, string $url) {
|
||||
app(Kernel::class)->pushMiddleware($middleware);
|
||||
|
||||
config(['tenancy.tenant_model' => Tenant::class]);
|
||||
|
||||
Route::get('/foo', [Controller::class, 'index'])
|
||||
->middleware(PreventAccessFromUnwantedDomains::class)
|
||||
->name('foo');
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => $domain,
|
||||
]);
|
||||
|
||||
$response = pest()->get($url)->assertOk();
|
||||
|
||||
assertTenancyInitializedInEarlyIdentificationRequest($response->getContent());
|
||||
})->with([
|
||||
'domain identification' => ['middleware' => InitializeTenancyByDomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
|
||||
'subdomain identification' => ['middleware' => InitializeTenancyBySubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
|
||||
'domainOrSubdomain identification using domain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo.test', 'url' => 'http://foo.test/foo'],
|
||||
'domainOrSubdomain identification using subdomain' => ['middleware' => InitializeTenancyByDomainOrSubdomain::class, 'domain' => 'foo', 'url' => 'http://foo.localhost/foo'],
|
||||
]);
|
||||
|
||||
function assertTenancyInitializedInEarlyIdentificationRequest(string|false $string): void
|
||||
{
|
||||
expect($string)->toBe(tenant()->getTenantKey() . '-abc123'); // Assert that the service class returns tenant value
|
||||
expect(app()->make('additionalMiddlewareRunsInTenantContext'))->toBeTrue(); // Assert that middleware added in the controller constructor runs in tenant context
|
||||
expect(app()->make('controllerRunsInTenantContext'))->toBeTrue(); // Assert that tenancy is initialized in the controller constructor
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Tests\Etc\Console;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ExampleCommand extends Command
|
||||
|
|
@ -22,14 +23,13 @@ class ExampleCommand extends Command
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
User::create([
|
||||
'id' => 999,
|
||||
'name' => 'Test command',
|
||||
'email' => 'test@command.com',
|
||||
$id = User::create([
|
||||
'name' => 'Test user',
|
||||
'email' => Str::random(8) . '@example.com',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
])->id;
|
||||
|
||||
$this->line("User's name is " . User::find(999)->name);
|
||||
$this->line("User's name is " . User::find($id)->name);
|
||||
$this->line($this->argument('a'));
|
||||
$this->line($this->option('c'));
|
||||
}
|
||||
|
|
|
|||
16
tests/Etc/EarlyIdentification/AdditionalMiddleware.php
Normal file
16
tests/Etc/EarlyIdentification/AdditionalMiddleware.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AdditionalMiddleware
|
||||
{
|
||||
public function handle(Request $request, Closure $next): mixed
|
||||
{
|
||||
app()->instance('additionalMiddlewareRunsInTenantContext', tenancy()->initialized);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
19
tests/Etc/EarlyIdentification/Controller.php
Normal file
19
tests/Etc/EarlyIdentification/Controller.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
|
||||
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
public function __construct(public Service $service)
|
||||
{
|
||||
app()->instance('controllerRunsInTenantContext', tenancy()->initialized);
|
||||
$this->middleware(AdditionalMiddleware::class);
|
||||
}
|
||||
|
||||
public function index(): string
|
||||
{
|
||||
return $this->service->token;
|
||||
}
|
||||
}
|
||||
15
tests/Etc/EarlyIdentification/Service.php
Normal file
15
tests/Etc/EarlyIdentification/Service.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc\EarlyIdentification;
|
||||
|
||||
class Service
|
||||
{
|
||||
public string $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->token = config('tenancy.token');
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ class HttpKernel extends Kernel
|
|||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
\Orchestra\Testbench\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
// \Illuminate\Session\Middleware\AuthenticateSession::class,
|
||||
|
|
|
|||
25
tests/Etc/TestingBroadcaster.php
Normal file
25
tests/Etc/TestingBroadcaster.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc;
|
||||
|
||||
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||
|
||||
class TestingBroadcaster extends Broadcaster {
|
||||
public function __construct(
|
||||
public string $message
|
||||
) {}
|
||||
|
||||
public function auth($request)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validAuthenticationResponse($request, $result)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function broadcast(array $channels, $event, array $payload = [])
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSessionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('sessions', function (Blueprint $table) {
|
||||
$table->string('id')->primary();
|
||||
$table->foreignId('user_id')->nullable()->index();
|
||||
$table->string('ip_address', 45)->nullable();
|
||||
$table->text('user_agent')->nullable();
|
||||
$table->text('payload');
|
||||
$table->integer('last_activity')->index();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('sessions');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class TestCreateCompaniesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('companies', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('global_id')->unique();
|
||||
$table->string('name');
|
||||
$table->string('email');
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('companies');
|
||||
}
|
||||
}
|
||||
|
|
@ -79,7 +79,7 @@ test('ing events can be used to cancel db creation', function () {
|
|||
});
|
||||
|
||||
$tenant = Tenant::create();
|
||||
dispatch_now(new CreateDatabase($tenant));
|
||||
dispatch_sync(new CreateDatabase($tenant));
|
||||
|
||||
pest()->assertFalse($tenant->database()->manager()->databaseExists(
|
||||
$tenant->database()->getName()
|
||||
|
|
@ -171,12 +171,13 @@ test('database is not migrated if creation is disabled', function () {
|
|||
})->toListener()
|
||||
);
|
||||
|
||||
Tenant::create([
|
||||
$tenant = Tenant::create([
|
||||
'tenancy_create_database' => false,
|
||||
'tenancy_db_name' => 'already_created',
|
||||
]);
|
||||
|
||||
expect(pest()->hasFailed())->toBeFalse();
|
||||
// assert test didn't fail
|
||||
$this->assertTrue($tenant->exists());
|
||||
});
|
||||
|
||||
class FooListener extends QueueableListener
|
||||
|
|
|
|||
29
tests/Features/ViteBundlerTest.php
Normal file
29
tests/Features/ViteBundlerTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Foundation\Vite;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Stancl\Tenancy\Vite as StanclVite;
|
||||
use Stancl\Tenancy\Features\ViteBundler;
|
||||
|
||||
test('vite helper uses our custom class', function() {
|
||||
$vite = app(Vite::class);
|
||||
|
||||
expect($vite)->toBeInstanceOf(Vite::class);
|
||||
expect($vite)->not()->toBeInstanceOf(StanclVite::class);
|
||||
|
||||
config([
|
||||
'tenancy.features' => [ViteBundler::class],
|
||||
]);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
app()->forgetInstance(Vite::class);
|
||||
|
||||
$vite = app(Vite::class);
|
||||
|
||||
expect($vite)->toBeInstanceOf(StanclVite::class);
|
||||
});
|
||||
76
tests/MailTest.php
Normal file
76
tests/MailTest.php
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
|
||||
beforeEach(function() {
|
||||
config(['mail.default' => 'smtp']);
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password
|
||||
function assertMailerTransportUsesPassword(string|null $password) {
|
||||
$manager = app(MailManager::class);
|
||||
$mailer = invade($manager)->get('smtp');
|
||||
$mailerPassword = invade($mailer->getSymfonyTransport())->password;
|
||||
|
||||
expect($mailerPassword)->toBe((string) $password);
|
||||
};
|
||||
|
||||
test('mailer transport uses the correct credentials', function() {
|
||||
withTenantDatabases();
|
||||
|
||||
config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']);
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
assertMailerTransportUsesPassword($defaultPassword); // $tenant->smtp_password is not set, so the default password should be used
|
||||
tenancy()->end();
|
||||
|
||||
// Assert mailer uses the updated password
|
||||
$tenant->update(['smtp_password' => $newPassword = 'changed']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
assertMailerTransportUsesPassword($newPassword);
|
||||
tenancy()->end();
|
||||
|
||||
// Assert mailer uses the correct password after switching to a different tenant
|
||||
tenancy()->initialize(Tenant::create(['smtp_password' => $newTenantPassword = 'updated']));
|
||||
assertMailerTransportUsesPassword($newTenantPassword);
|
||||
tenancy()->end();
|
||||
|
||||
// Assert mailer uses the default password after tenancy ends
|
||||
assertMailerTransportUsesPassword($defaultPassword);
|
||||
});
|
||||
|
||||
|
||||
test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() {
|
||||
withTenantDatabases();
|
||||
|
||||
$mailers = fn() => invade(app(MailManager::class))->mailers;
|
||||
|
||||
app(MailManager::class)->mailer('smtp');
|
||||
|
||||
expect($mailers())->toHaveCount(1);
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
expect($mailers())->toHaveCount(0);
|
||||
|
||||
app(MailManager::class)->mailer('smtp');
|
||||
|
||||
expect($mailers())->toHaveCount(1);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect($mailers())->toHaveCount(0);
|
||||
});
|
||||
45
tests/ManualModeTest.php
Normal file
45
tests/ManualModeTest.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Listeners\CreateTenantConnection;
|
||||
use Stancl\Tenancy\Listeners\UseCentralConnection;
|
||||
use Stancl\Tenancy\Listeners\UseTenantConnection;
|
||||
use \Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
test('manual tenancy initialization works', function () {
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
|
||||
Event::listen(TenancyInitialized::class, CreateTenantConnection::class);
|
||||
Event::listen(TenancyInitialized::class, UseTenantConnection::class);
|
||||
Event::listen(TenancyEnded::class, UseCentralConnection::class);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
expect(app('db')->getDefaultConnection())->toBe('central');
|
||||
expect(array_keys(app('db')->getConnections()))->toBe(['central', 'tenant_host_connection']);
|
||||
pest()->assertArrayNotHasKey('tenant', config('database.connections'));
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// Trigger creation of the tenant connection
|
||||
createUsersTable();
|
||||
|
||||
expect(app('db')->getDefaultConnection())->toBe('tenant');
|
||||
expect(array_keys(app('db')->getConnections()))->toBe(['central', 'tenant']);
|
||||
pest()->assertArrayHasKey('tenant', config('database.connections'));
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(array_keys(app('db')->getConnections()))->toBe(['central']);
|
||||
expect(config('database.connections.tenant'))->toBeNull();
|
||||
expect(app('db')->getDefaultConnection())->toBe(config('tenancy.database.central_connection'));
|
||||
});
|
||||
|
|
@ -67,23 +67,6 @@ test('CreatePendingTenants command cannot run with both time constraints', funct
|
|||
->assertFailed();
|
||||
});
|
||||
|
||||
test('CreatePendingTenants commands all option overrides any config constraints', function () {
|
||||
Tenant::createPending();
|
||||
Tenant::createPending();
|
||||
|
||||
tenancy()->model()->query()->onlyPending()->first()->update([
|
||||
'pending_since' => now()->subDays(10)
|
||||
]);
|
||||
|
||||
config(['tenancy.pending.older_than_days' => 4]);
|
||||
|
||||
Artisan::call(ClearPendingTenants::class, [
|
||||
'--all' => true
|
||||
]);
|
||||
|
||||
expect(Tenant::onlyPending()->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('tenancy can check if there are any pending tenants', function () {
|
||||
expect(Tenant::onlyPending()->exists())->toBeFalse();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
<?php
|
||||
|
||||
use Stancl\Tenancy\Tests\TestCase;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
|
||||
uses(TestCase::class)->in(...filesAndFoldersExcluding(['WithoutTenancy'])); // todo move all tests to a separate folder
|
||||
|
||||
|
|
@ -14,4 +18,9 @@ function filesAndFoldersExcluding(array $exclude = []): array
|
|||
$dirs = scandir(__DIR__);
|
||||
|
||||
return array_filter($dirs, fn($dir) => ! in_array($dir, array_merge(['.', '..'], $exclude) , true));
|
||||
function withTenantDatabases()
|
||||
{
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,23 +3,23 @@
|
|||
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 Spatie\Valuestore\Valuestore;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Tests\Etc\User;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Queue\Events\JobProcessed;
|
||||
use Illuminate\Queue\Events\JobProcessing;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
|
|
@ -48,6 +48,8 @@ afterEach(function () {
|
|||
});
|
||||
|
||||
test('tenant id is passed to tenant queues', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
config(['queue.default' => 'sync']);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
|
@ -64,6 +66,8 @@ test('tenant id is passed to tenant queues', function () {
|
|||
});
|
||||
|
||||
test('tenant id is not passed to central queues', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
|
@ -156,6 +160,8 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
|||
})->with([true, false]);
|
||||
|
||||
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant1 = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
|
@ -217,13 +223,6 @@ function withUsers()
|
|||
});
|
||||
}
|
||||
|
||||
function withTenantDatabases()
|
||||
{
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
}
|
||||
|
||||
class TestJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
|
|
|||
|
|
@ -832,7 +832,7 @@ function migrateUsersTableForTenants(): void
|
|||
// Tenant model used for resource syncing setup
|
||||
class ResourceTenant extends Tenant
|
||||
{
|
||||
public function users()
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
|
|
|
|||
398
tests/ResourceSyncingUsingPolymorphicTest.php
Normal file
398
tests/ResourceSyncingUsingPolymorphicTest.php
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
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\DatabaseConfig;
|
||||
use Stancl\Tenancy\Database\Models\TenantMorphPivot;
|
||||
use Stancl\Tenancy\Database\Models\TenantPivot;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Listeners\UpdateSyncedResource;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'tenancy.bootstrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
],
|
||||
'tenancy.models.tenant' => ResourceTenantUsingPolymorphic::class,
|
||||
]);
|
||||
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
|
||||
DatabaseConfig::generateDatabaseNamesUsing(function () {
|
||||
return 'db' . Str::random(16);
|
||||
});
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
|
||||
UpdateSyncedResource::$shouldQueue = false; // Global state cleanup
|
||||
Event::listen(SyncedResourceSaved::class, UpdateSyncedResource::class);
|
||||
|
||||
// Run migrations on central connection
|
||||
pest()->artisan('migrate', [
|
||||
'--path' => [
|
||||
__DIR__ . '/../assets/resource-syncing-migrations',
|
||||
__DIR__ . '/Etc/synced_resource_migrations/users',
|
||||
__DIR__ . '/Etc/synced_resource_migrations/companies',
|
||||
],
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
});
|
||||
|
||||
test('resource syncing works using a single pivot table for multiple models when syncing from central to tenant', function () {
|
||||
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
$centralUser = CentralUserUsingPolymorphic::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$tenant1->run(function () {
|
||||
expect(TenantUserUsingPolymorphic::all())->toHaveCount(0);
|
||||
});
|
||||
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
// Assert `tenants` are accessible
|
||||
expect($centralUser->tenants->pluck('id')->toArray())->toBe(['t1']);
|
||||
|
||||
// Users are accessible from tenant
|
||||
expect($tenant1->users()->pluck('email')->toArray())->toBe(['john@localhost']);
|
||||
|
||||
// Assert User resource is synced
|
||||
$tenant1->run(function () use ($centralUser) {
|
||||
$tenantUser = TenantUserUsingPolymorphic::first()->toArray();
|
||||
$centralUser = $centralUser->withoutRelations()->toArray();
|
||||
unset($centralUser['id'], $tenantUser['id']);
|
||||
|
||||
expect($tenantUser)->toBe($centralUser);
|
||||
});
|
||||
|
||||
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
|
||||
migrateCompaniesTableForTenants();
|
||||
|
||||
$centralCompany = CentralCompanyUsingPolymorphic::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'ArchTech',
|
||||
'email' => 'archtech@localhost',
|
||||
]);
|
||||
|
||||
$tenant2->run(function () {
|
||||
expect(TenantCompanyUsingPolymorphic::all())->toHaveCount(0);
|
||||
});
|
||||
|
||||
$centralCompany->tenants()->attach('t2');
|
||||
|
||||
// Assert `tenants` are accessible
|
||||
expect($centralCompany->tenants->pluck('id')->toArray())->toBe(['t2']);
|
||||
|
||||
// Companies are accessible from tenant
|
||||
expect($tenant2->companies()->pluck('email')->toArray())->toBe(['archtech@localhost']);
|
||||
|
||||
// Assert Company resource is synced
|
||||
$tenant2->run(function () use ($centralCompany) {
|
||||
$tenantCompany = TenantCompanyUsingPolymorphic::first()->toArray();
|
||||
$centralCompany = $centralCompany->withoutRelations()->toArray();
|
||||
|
||||
unset($centralCompany['id'], $tenantCompany['id']);
|
||||
|
||||
expect($tenantCompany)->toBe($centralCompany);
|
||||
});
|
||||
});
|
||||
|
||||
test('resource syncing works using a single pivot table for multiple models when syncing from tenant to central', function () {
|
||||
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
$tenantUser = TenantUserUsingPolymorphic::create([
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Assert User resource is synced
|
||||
$centralUser = CentralUserUsingPolymorphic::first();
|
||||
|
||||
// Assert `tenants` are accessible
|
||||
expect($centralUser->tenants->pluck('id')->toArray())->toBe(['t1']);
|
||||
|
||||
// Users are accessible from tenant
|
||||
expect($tenant1->users()->pluck('email')->toArray())->toBe(['john@localhost']);
|
||||
|
||||
$centralUser = $centralUser->withoutRelations()->toArray();
|
||||
$tenantUser = $tenantUser->toArray();
|
||||
unset($centralUser['id'], $tenantUser['id']);
|
||||
|
||||
// array keys use a different order here
|
||||
expect($tenantUser)->toEqualCanonicalizing($centralUser);
|
||||
|
||||
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
|
||||
migrateCompaniesTableForTenants();
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
$tenantCompany = TenantCompanyUsingPolymorphic::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'tenant comp',
|
||||
'email' => 'company@localhost',
|
||||
]);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Assert Company resource is synced
|
||||
$centralCompany = CentralCompanyUsingPolymorphic::first();
|
||||
|
||||
// Assert `tenants` are accessible
|
||||
expect($centralCompany->tenants->pluck('id')->toArray())->toBe(['t2']);
|
||||
|
||||
// Companies are accessible from tenant
|
||||
expect($tenant2->companies()->pluck('email')->toArray())->toBe(['company@localhost']);
|
||||
|
||||
$centralCompany = $centralCompany->withoutRelations()->toArray();
|
||||
$tenantCompany = $tenantCompany->toArray();
|
||||
unset($centralCompany['id'], $tenantCompany['id']);
|
||||
|
||||
expect($tenantCompany)->toBe($centralCompany);
|
||||
});
|
||||
|
||||
test('right resources are accessible from the tenant', function () {
|
||||
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
|
||||
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
$user1 = CentralUserUsingPolymorphic::create([
|
||||
'global_id' => 'user1',
|
||||
'name' => 'user1',
|
||||
'email' => 'user1@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$user2 = CentralUserUsingPolymorphic::create([
|
||||
'global_id' => 'user2',
|
||||
'name' => 'user2',
|
||||
'email' => 'user2@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$user3 = CentralUserUsingPolymorphic::create([
|
||||
'global_id' => 'user3',
|
||||
'name' => 'user3',
|
||||
'email' => 'user3@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$user1->tenants()->attach('t1');
|
||||
$user2->tenants()->attach('t1');
|
||||
$user3->tenants()->attach('t2');
|
||||
|
||||
expect($tenant1->users()->pluck('email')->toArray())->toBe([$user1->email, $user2->email]);
|
||||
expect($tenant2->users()->pluck('email')->toArray())->toBe([$user3->email]);
|
||||
});
|
||||
|
||||
function migrateCompaniesTableForTenants(): void
|
||||
{
|
||||
pest()->artisan('tenants:migrate', [
|
||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/companies',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
}
|
||||
|
||||
// Tenant model used for resource syncing setup
|
||||
class ResourceTenantUsingPolymorphic extends Tenant
|
||||
{
|
||||
public function users(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(CentralUserUsingPolymorphic::class, 'tenant_resources', 'tenant_resources', 'tenant_id', 'resource_global_id', 'id', 'global_id')
|
||||
->using(TenantMorphPivot::class);
|
||||
}
|
||||
|
||||
public function companies(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(CentralCompanyUsingPolymorphic::class, 'tenant_resources', 'tenant_resources', 'tenant_id', 'resource_global_id', 'id', 'global_id')
|
||||
->using(TenantMorphPivot::class);
|
||||
}
|
||||
}
|
||||
|
||||
class CentralUserUsingPolymorphic extends Model implements SyncMaster
|
||||
{
|
||||
use ResourceSyncing, CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public $table = 'users';
|
||||
|
||||
public function getTenantModelName(): string
|
||||
{
|
||||
return TenantUserUsingPolymorphic::class;
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKey(): string|int
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'global_id',
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class TenantUserUsingPolymorphic extends Model implements Syncable
|
||||
{
|
||||
use ResourceSyncing;
|
||||
|
||||
protected $table = 'users';
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public function getGlobalIdentifierKey(): string|int
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return CentralUserUsingPolymorphic::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'global_id',
|
||||
'name',
|
||||
'password',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class CentralCompanyUsingPolymorphic extends Model implements SyncMaster
|
||||
{
|
||||
use ResourceSyncing, CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public $table = 'companies';
|
||||
|
||||
public function getTenantModelName(): string
|
||||
{
|
||||
return TenantCompanyUsingPolymorphic::class;
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKey(): string|int
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'global_id',
|
||||
'name',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class TenantCompanyUsingPolymorphic extends Model implements Syncable
|
||||
{
|
||||
use ResourceSyncing;
|
||||
|
||||
protected $table = 'companies';
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public function getGlobalIdentifierKey(): string|int
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKeyName(): string
|
||||
{
|
||||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getCentralModelName(): string
|
||||
{
|
||||
return CentralCompanyUsingPolymorphic::class;
|
||||
}
|
||||
|
||||
public function getSyncedAttributeNames(): array
|
||||
{
|
||||
return [
|
||||
'global_id',
|
||||
'name',
|
||||
'email',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
145
tests/SessionBootstrapperTest.php
Normal file
145
tests/SessionBootstrapperTest.php
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Listeners;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
/**
|
||||
* This collection of regression tests verifies that SessionTenancyBootstrapper
|
||||
* fully fixes the issue described here https://github.com/archtechx/tenancy/issues/547
|
||||
*
|
||||
* This means: using the DB session driver and:
|
||||
* 1) switching to the central context from tenant requests, OR
|
||||
* 2) switching to the tenant context from central requests
|
||||
*/
|
||||
|
||||
beforeEach(function () {
|
||||
config(['session.driver' => 'database']);
|
||||
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
|
||||
|
||||
Event::listen(
|
||||
TenantCreated::class,
|
||||
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener()
|
||||
);
|
||||
|
||||
Event::listen(Events\TenancyInitialized::class, Listeners\BootstrapTenancy::class);
|
||||
Event::listen(Events\TenancyEnded::class, Listeners\RevertToCentralContext::class);
|
||||
|
||||
// Sessions table for central database
|
||||
pest()->artisan('migrate', [
|
||||
'--path' => __DIR__ . '/Etc/session_migrations',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
});
|
||||
|
||||
test('central helper can be used in tenant requests', function (bool $enabled, bool $shouldThrow) {
|
||||
if ($enabled) {
|
||||
config()->set(
|
||||
'tenancy.bootstrappers',
|
||||
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
||||
);
|
||||
}
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$tenant->domains()->create(['domain' => 'foo.localhost']);
|
||||
|
||||
// run for tenants
|
||||
pest()->artisan('tenants:migrate', [
|
||||
'--path' => __DIR__ . '/Etc/session_migrations',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
Route::middleware(['web', InitializeTenancyByDomain::class])->get('/bar', function () {
|
||||
session(['message' => 'tenant session']);
|
||||
|
||||
tenancy()->central(function () {
|
||||
return 'central results';
|
||||
});
|
||||
|
||||
return session('message');
|
||||
});
|
||||
|
||||
// We initialize tenancy before making the request, since sessions work a bit differently in tests
|
||||
// and we need the DB session handler to use the tenant connection (as it does in a real app on tenant requests).
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
try {
|
||||
$this->withoutExceptionHandling()
|
||||
->get('http://foo.localhost/bar')
|
||||
->assertOk()
|
||||
->assertSee('tenant session');
|
||||
|
||||
if ($shouldThrow) {
|
||||
pest()->fail('Exception not thrown');
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
if ($shouldThrow) {
|
||||
pest()->assertTrue(true); // empty assertion to make the test pass
|
||||
} else {
|
||||
pest()->fail('Exception thrown: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
})->with([
|
||||
['enabled' => false, 'shouldThrow' => true],
|
||||
['enabled' => true, 'shouldThrow' => false],
|
||||
]);
|
||||
|
||||
test('tenant run helper can be used on central requests', function (bool $enabled, bool $shouldThrow) {
|
||||
if ($enabled) {
|
||||
config()->set(
|
||||
'tenancy.bootstrappers',
|
||||
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
||||
);
|
||||
}
|
||||
|
||||
Tenant::create();
|
||||
|
||||
// run for tenants
|
||||
pest()->artisan('tenants:migrate', [
|
||||
'--path' => __DIR__ . '/Etc/session_migrations',
|
||||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
Route::middleware(['web'])->get('/bar', function () {
|
||||
session(['message' => 'central session']);
|
||||
|
||||
Tenant::first()->run(function () {
|
||||
return 'tenant results';
|
||||
});
|
||||
|
||||
return session('message');
|
||||
});
|
||||
|
||||
try {
|
||||
$this->withoutExceptionHandling()
|
||||
->get('http://localhost/bar')
|
||||
->assertOk()
|
||||
->assertSee('central session');
|
||||
|
||||
if ($shouldThrow) {
|
||||
pest()->fail('Exception not thrown');
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
if ($shouldThrow) {
|
||||
pest()->assertTrue(true); // empty assertion to make the test pass
|
||||
} else {
|
||||
pest()->fail('Exception thrown: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
})->with([
|
||||
['enabled' => false, 'shouldThrow' => true],
|
||||
['enabled' => true, 'shouldThrow' => false],
|
||||
]);
|
||||
|
|
@ -52,12 +52,13 @@ test('onfail logic can be customized', function () {
|
|||
->assertSee('foo');
|
||||
});
|
||||
|
||||
test('localhost is not a valid subdomain', function () {
|
||||
test('archte.ch is not a valid subdomain', function () {
|
||||
pest()->expectException(NotASubdomainException::class);
|
||||
|
||||
// This gets routed to the app, but with a request domain of 'archte.ch'
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('http://localhost/foo/abc/xyz');
|
||||
->get('http://archte.ch/foo/abc/xyz');
|
||||
});
|
||||
|
||||
test('ip address is not a valid subdomain', function () {
|
||||
|
|
@ -65,7 +66,7 @@ test('ip address is not a valid subdomain', function () {
|
|||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('http://127.0.0.1/foo/abc/xyz');
|
||||
->get('http://127.0.0.2/foo/abc/xyz');
|
||||
});
|
||||
|
||||
test('oninvalidsubdomain logic can be customized', function () {
|
||||
|
|
@ -81,7 +82,7 @@ test('oninvalidsubdomain logic can be customized', function () {
|
|||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('http://127.0.0.1/foo/abc/xyz')
|
||||
->get('http://127.0.0.2/foo/abc/xyz')
|
||||
->assertSee('foo custom invalid subdomain handler');
|
||||
});
|
||||
|
||||
|
|
@ -106,26 +107,6 @@ test('we cant use a subdomain that doesnt belong to our central domains', functi
|
|||
->get('http://foo.localhost/foo/abc/xyz');
|
||||
});
|
||||
|
||||
test('central domain is not a subdomain', function () {
|
||||
config(['tenancy.central_domains' => [
|
||||
'localhost',
|
||||
]]);
|
||||
|
||||
$tenant = SubdomainTenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'acme',
|
||||
]);
|
||||
|
||||
pest()->expectException(NotASubdomainException::class);
|
||||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('http://localhost/foo/abc/xyz');
|
||||
});
|
||||
|
||||
class SubdomainTenant extends Models\Tenant
|
||||
{
|
||||
use HasDomains;
|
||||
|
|
|
|||
|
|
@ -302,7 +302,7 @@ test('database credentials can be provided to PermissionControlledMySQLDatabaseM
|
|||
$mysql2DB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';");
|
||||
$mysql2DB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;");
|
||||
$mysql2DB->statement("FLUSH PRIVILEGES;");
|
||||
|
||||
|
||||
DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time
|
||||
|
||||
config(['database.connections.mysql2.username' => $username]);
|
||||
|
|
@ -347,7 +347,7 @@ test('tenant database can be created by using the username and password from ten
|
|||
$mysqlDB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';");
|
||||
$mysqlDB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;");
|
||||
$mysqlDB->statement("FLUSH PRIVILEGES;");
|
||||
|
||||
|
||||
DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time
|
||||
|
||||
// Remove `mysql` credentials to make sure we will be using the credentials from the tenant config
|
||||
|
|
@ -390,6 +390,81 @@ test('path used by sqlite manager can be customized', function () {
|
|||
expect(file_exists($customPath . '/' . $name))->toBeTrue();
|
||||
});
|
||||
|
||||
test('the tenant connection template can be specified either by name or as a connection array', function () {
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
|
||||
config([
|
||||
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||
'tenancy.database.template_tenant_connection' => 'mysql',
|
||||
]);
|
||||
|
||||
$name = 'foo' . Str::random(8);
|
||||
$tenant = Tenant::create([
|
||||
'tenancy_db_name' => $name,
|
||||
]);
|
||||
|
||||
/** @var MySQLDatabaseManager $manager */
|
||||
$manager = $tenant->database()->manager();
|
||||
expect($manager->databaseExists($name))->toBeTrue();
|
||||
expect($manager->database()->getConfig('host'))->toBe('mysql');
|
||||
|
||||
config([
|
||||
'tenancy.database.template_tenant_connection' => [
|
||||
'driver' => 'mysql',
|
||||
'url' => null,
|
||||
'host' => 'mysql2',
|
||||
'port' => '3306',
|
||||
'database' => 'main',
|
||||
'username' => 'root',
|
||||
'password' => 'password',
|
||||
'unix_socket' => '',
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => '',
|
||||
'prefix_indexes' => true,
|
||||
'strict' => true,
|
||||
'engine' => null,
|
||||
'options' => [],
|
||||
],
|
||||
]);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'tenancy_db_name' => $name,
|
||||
]);
|
||||
|
||||
/** @var MySQLDatabaseManager $manager */
|
||||
$manager = $tenant->database()->manager();
|
||||
expect($manager->databaseExists($name))->toBeTrue(); // tenant connection works
|
||||
expect($manager->database()->getConfig('host'))->toBe('mysql2');
|
||||
});
|
||||
|
||||
test('partial tenant connection templates get merged into the central connection template', function () {
|
||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||
return $event->tenant;
|
||||
})->toListener());
|
||||
|
||||
config([
|
||||
'database.connections.central.url' => 'example.com',
|
||||
'tenancy.database.template_tenant_connection' => [
|
||||
'url' => null,
|
||||
'host' => 'mysql2',
|
||||
],
|
||||
]);
|
||||
|
||||
$name = 'foo' . Str::random(8);
|
||||
$tenant = Tenant::create([
|
||||
'tenancy_db_name' => $name,
|
||||
]);
|
||||
|
||||
/** @var MySQLDatabaseManager $manager */
|
||||
$manager = $tenant->database()->manager();
|
||||
expect($manager->databaseExists($name))->toBeTrue(); // tenant connection works
|
||||
expect($manager->database()->getConfig('host'))->toBe('mysql2');
|
||||
expect($manager->database()->getConfig('url'))->toBeNull();
|
||||
});
|
||||
|
||||
// Datasets
|
||||
dataset('database_managers', [
|
||||
['mysql', MySQLDatabaseManager::class],
|
||||
|
|
|
|||
|
|
@ -83,6 +83,19 @@ test('tenant user can be impersonated on a tenant domain', function () {
|
|||
pest()->get('http://foo.localhost/dashboard')
|
||||
->assertSuccessful()
|
||||
->assertSee('You are logged in as Joe');
|
||||
|
||||
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
||||
expect(session('tenancy_impersonating'))->toBeTrue();
|
||||
|
||||
// Leave impersonation
|
||||
UserImpersonation::leave();
|
||||
|
||||
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||
expect(session('tenancy_impersonating'))->toBeNull();
|
||||
|
||||
// Assert can't access the tenant dashboard
|
||||
pest()->get('http://foo.localhost/dashboard')
|
||||
->assertRedirect('http://foo.localhost/login');
|
||||
});
|
||||
|
||||
test('tenant user can be impersonated on a tenant path', function () {
|
||||
|
|
@ -116,6 +129,19 @@ test('tenant user can be impersonated on a tenant path', function () {
|
|||
pest()->get('/acme/dashboard')
|
||||
->assertSuccessful()
|
||||
->assertSee('You are logged in as Joe');
|
||||
|
||||
expect(UserImpersonation::isImpersonating())->toBeTrue();
|
||||
expect(session('tenancy_impersonating'))->toBeTrue();
|
||||
|
||||
// Leave impersonation
|
||||
UserImpersonation::leave();
|
||||
|
||||
expect(UserImpersonation::isImpersonating())->toBeFalse();
|
||||
expect(session('tenancy_impersonating'))->toBeNull();
|
||||
|
||||
// Assert can't access the tenant dashboard
|
||||
pest()->get('/acme/dashboard')
|
||||
->assertRedirect('/login');
|
||||
});
|
||||
|
||||
test('tokens have a limited ttl', function () {
|
||||
|
|
|
|||
|
|
@ -4,16 +4,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use PDO;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Facades\GlobalCache;
|
||||
use Dotenv\Dotenv;
|
||||
use Stancl\Tenancy\Facades\Tenancy;
|
||||
use Stancl\Tenancy\Features\CrossDomainRedirect;
|
||||
use Stancl\Tenancy\TenancyServiceProvider;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Facades\GlobalCache;
|
||||
use Stancl\Tenancy\TenancyServiceProvider;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
|
|
@ -104,6 +106,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'--force' => true,
|
||||
],
|
||||
'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that
|
||||
'tenancy.bootstrappers.broadcast' => BroadcastTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that
|
||||
'tenancy.bootstrappers.mail' => MailTenancyBootstrapper::class,
|
||||
'tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class,
|
||||
'queue.connections.central' => [
|
||||
'driver' => 'sync',
|
||||
'central' => true,
|
||||
|
|
@ -113,6 +118,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
]);
|
||||
|
||||
$app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
|
||||
$app->singleton(BroadcastTenancyBootstrapper::class);
|
||||
$app->singleton(MailTenancyBootstrapper::class);
|
||||
$app->singleton(UrlTenancyBootstrapper::class);
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
|
|
|
|||
|
|
@ -3,27 +3,24 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Features\UniversalRoutes;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Middleware\PreventAccessFromUnwantedDomains;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
|
||||
afterEach(function () {
|
||||
InitializeTenancyByDomain::$onFail = null;
|
||||
});
|
||||
test('a route can work in both central and tenant context', function (array $routeMiddleware, string|null $globalMiddleware) {
|
||||
if ($globalMiddleware) {
|
||||
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
||||
}
|
||||
|
||||
test('a route can work in both central and tenant context', function () {
|
||||
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]);
|
||||
|
||||
pest()->get('http://localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
})->middleware($routeMiddleware);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
|
|
@ -32,28 +29,33 @@ test('a route can work in both central and tenant context', function () {
|
|||
'domain' => 'acme.localhost',
|
||||
]);
|
||||
|
||||
pest()->get('http://acme.localhost/foo')
|
||||
pest()->get("http://localhost/foo")
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
|
||||
pest()->get("http://acme.localhost/foo")
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is initialized.');
|
||||
});
|
||||
})->with('identification types');
|
||||
|
||||
test('making one route universal doesnt make all routes universal', function () {
|
||||
Route::get('/bar', function () {
|
||||
return tenant('id');
|
||||
})->middleware(InitializeTenancyByDomain::class);
|
||||
test('making one route universal doesnt make all routes universal', function (array $routeMiddleware, string|null $globalMiddleware) {
|
||||
if ($globalMiddleware) {
|
||||
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
||||
}
|
||||
|
||||
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]);
|
||||
Route::middleware($routeMiddleware)->group(function () {
|
||||
Route::get('/nonuniversal', function () {
|
||||
return tenant('id');
|
||||
});
|
||||
|
||||
pest()->get('http://localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
Route::get('/universal', function () {
|
||||
return tenancy()->initialized
|
||||
? 'Tenancy is initialized.'
|
||||
: 'Tenancy is not initialized.';
|
||||
})->middleware('universal');
|
||||
});
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
|
|
@ -62,16 +64,57 @@ test('making one route universal doesnt make all routes universal', function ()
|
|||
'domain' => 'acme.localhost',
|
||||
]);
|
||||
|
||||
pest()->get('http://acme.localhost/foo')
|
||||
pest()->get("http://localhost/universal")
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
|
||||
pest()->get("http://acme.localhost/universal")
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is initialized.');
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
pest()->get('http://localhost/bar')
|
||||
->assertStatus(500);
|
||||
pest()->get('http://localhost/nonuniversal')
|
||||
->assertStatus(404);
|
||||
|
||||
pest()->get('http://acme.localhost/bar')
|
||||
pest()->get('http://acme.localhost/nonuniversal')
|
||||
->assertSuccessful()
|
||||
->assertSee('acme');
|
||||
});
|
||||
})->with([
|
||||
'early identification' => [
|
||||
'route_middleware' => [PreventAccessFromUnwantedDomains::class],
|
||||
'global_middleware' => InitializeTenancyByDomain::class,
|
||||
],
|
||||
'route-level identification' => [
|
||||
'route_middleware' => [PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
|
||||
'global_middleware' => null,
|
||||
]
|
||||
]);
|
||||
|
||||
test('it throws correct exception when route is universal and tenant does not exist', function (array $routeMiddleware, string|null $globalMiddleware) {
|
||||
if ($globalMiddleware) {
|
||||
app(Kernel::class)->pushMiddleware($globalMiddleware);
|
||||
}
|
||||
|
||||
Route::middlewareGroup('universal', []);
|
||||
|
||||
Route::get('/foo', function () {
|
||||
return tenancy()->initialized
|
||||
? 'Tenancy is initialized.'
|
||||
: 'Tenancy is not initialized.';
|
||||
})->middleware($routeMiddleware);
|
||||
|
||||
pest()->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
|
||||
$this->withoutExceptionHandling()->get('http://acme.localhost/foo');
|
||||
})->with('identification types');
|
||||
|
||||
dataset('identification types', [
|
||||
'early identification' => [
|
||||
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class],
|
||||
'global_middleware' => InitializeTenancyByDomain::class,
|
||||
],
|
||||
'route-level identification' => [
|
||||
'route_middleware' => ['universal', PreventAccessFromUnwantedDomains::class, InitializeTenancyByDomain::class],
|
||||
'global_middleware' => null,
|
||||
]
|
||||
]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue