mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:04:03 +00:00
Rename bootstrappers (#40)
* SessionTenancyBootstrapper -> DatabaseSessionBootstrapper * FortifyRouteTenancyBootstrapper -> FortifyRouteBootstrapper * BatchTenancyBootstrapper -> JobBatchBootstrapper * ScoutTenancyBootstrapper -> ScoutPrefixBootstrapper, also fix logic and remove todo * MailTenancyBootstrapper -> MailConfigBootstrapper * PrefixCacheTenancyBootstrapper -> CacheTenancyBootstrapper * remove todo * improve config file
This commit is contained in:
parent
0c11f29c19
commit
9f94505cb4
16 changed files with 82 additions and 82 deletions
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
use Stancl\Tenancy\Middleware;
|
||||
use Stancl\Tenancy\Resolvers;
|
||||
use Stancl\Tenancy\Bootstrappers;
|
||||
use Stancl\Tenancy\Enums\RouteMode;
|
||||
use Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
|
|
@ -129,27 +130,27 @@ return [
|
|||
*/
|
||||
'bootstrappers' => [
|
||||
// Basic Laravel features
|
||||
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
|
||||
Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper::class, // Alternative to PrefixCacheTenancyBootstrapper
|
||||
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
||||
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
||||
Bootstrappers\DatabaseTenancyBootstrapper::class,
|
||||
Bootstrappers\CacheTenancyBootstrapper::class,
|
||||
// Bootstrappers\CacheTagsBootstrapper::class, // Alternative to PrefixCacheTenancyBootstrapper
|
||||
Bootstrappers\FilesystemTenancyBootstrapper::class,
|
||||
Bootstrappers\QueueTenancyBootstrapper::class,
|
||||
// Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
||||
|
||||
// Support for edge cases
|
||||
Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper::class,
|
||||
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
||||
Bootstrappers\DatabaseSessionBootstrapper::class,
|
||||
Bootstrappers\JobBatchBootstrapper::class,
|
||||
|
||||
// Configurable bootstrappers
|
||||
// Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Note: Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
|
||||
// Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\BroadcastingChannelPrefixBootstrapper::class,
|
||||
// Bootstrappers\RootUrlBootstrapper::class,
|
||||
// Bootstrappers\UrlGeneratorBootstrapper::class,
|
||||
// Bootstrappers\MailTenancyBootstrapper::class, // Note: Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
|
||||
// Bootstrappers\BroadcastingConfigBootstrapper::class,
|
||||
// Bootstrappers\BroadcastingChannelPrefixBootstrapper::class,
|
||||
|
||||
// Integration bootstrappers
|
||||
// Stancl\Tenancy\Bootstrappers\Integrations\FortifyRouteTenancyBootstrapper::class,
|
||||
// Stancl\Tenancy\Bootstrappers\Integrations\ScoutTenancyBootstrapper::class,
|
||||
// Bootstrappers\Integrations\FortifyRouteTenancyBootstrapper::class,
|
||||
// Bootstrappers\Integrations\ScoutTenancyBootstrapper::class,
|
||||
],
|
||||
|
||||
/**
|
||||
|
|
@ -295,7 +296,7 @@ return [
|
|||
'redis' => [
|
||||
'prefix_base' => 'tenant', // Each key in Redis will be prepended by this prefix_base, followed by the tenant id.
|
||||
'prefixed_connections' => [ // Redis connections whose keys are prefixed, to separate one tenant's keys from another.
|
||||
// 'default',
|
||||
'default',
|
||||
],
|
||||
],
|
||||
|
||||
|
|
|
|||
|
|
@ -10,14 +10,11 @@ use Illuminate\Support\Facades\Cache;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
// todo@rename some bootstrappers end in TenancyBootstrapper and others don't - make this consistent or make the difference clear
|
||||
|
||||
/**
|
||||
* todo@name rename?
|
||||
*
|
||||
* Separate tenant cache using tagging.
|
||||
*
|
||||
* This is the legacy approach. Some things, like dependency injection, won't work properly with this bootstrapper.
|
||||
* PrefixCacheTenancyBootstrapper is the recommended bootstrapper for cache separation.
|
||||
* CacheTenancyBootstrapper is the recommended bootstrapper for cache separation.
|
||||
*/
|
||||
class CacheTagsBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@ use Illuminate\Support\Facades\Cache;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||
/**
|
||||
* Makes cache tenant-aware by applying a prefix.
|
||||
*/
|
||||
class CacheTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
protected string|null $originalPrefix = null;
|
||||
public static array $tenantCacheStores = []; // E.g. ['redis']
|
||||
|
|
@ -11,15 +11,13 @@ use Illuminate\Session\SessionManager;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
// todo@rename this should be DB-specific
|
||||
|
||||
/**
|
||||
* This resets the database connection used by the database session driver.
|
||||
*
|
||||
* It runs each time tenancy is initialized or ended.
|
||||
* That way the session driver always uses the current DB connection.
|
||||
*/
|
||||
class SessionTenancyBootstrapper implements TenancyBootstrapper
|
||||
class DatabaseSessionBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository $config,
|
||||
|
|
@ -16,7 +16,7 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
|||
*
|
||||
* Works with path and query string identification.
|
||||
*/
|
||||
class FortifyRouteTenancyBootstrapper implements TenancyBootstrapper
|
||||
class FortifyRouteBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* Make Fortify actions redirect to custom routes.
|
||||
|
|
@ -8,7 +8,7 @@ use Illuminate\Contracts\Config\Repository;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class ScoutTenancyBootstrapper implements TenancyBootstrapper
|
||||
class ScoutPrefixBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
protected ?string $originalScoutPrefix = null;
|
||||
|
||||
|
|
@ -19,7 +19,7 @@ class ScoutTenancyBootstrapper implements TenancyBootstrapper
|
|||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
if ($this->originalScoutPrefix !== null) {
|
||||
if ($this->originalScoutPrefix === null) {
|
||||
$this->originalScoutPrefix = $this->config->get('scout.prefix');
|
||||
}
|
||||
|
||||
|
|
@ -10,8 +10,10 @@ use Illuminate\Database\DatabaseManager;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
// todo add docblock
|
||||
class BatchTenancyBootstrapper implements TenancyBootstrapper
|
||||
/**
|
||||
* Adds support for running queued tenant jobs in batches.
|
||||
*/
|
||||
class JobBatchBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* The previous database connection instance.
|
||||
|
|
@ -9,7 +9,7 @@ use Illuminate\Foundation\Application;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class MailTenancyBootstrapper implements TenancyBootstrapper
|
||||
class MailConfigBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* Tenant properties to be mapped to config (similarly to the TenantConfig feature).
|
||||
|
|
@ -44,7 +44,6 @@ class Tenancy
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO: Remove this (so that runForMultiple() is still performant) and make the FS bootstrapper work either way
|
||||
if ($this->initialized) {
|
||||
$this->end();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use Illuminate\Bus\BatchRepository;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\JobBatchBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
|
|
@ -14,7 +14,7 @@ beforeEach(function () {
|
|||
config([
|
||||
'tenancy.bootstrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
BatchTenancyBootstrapper::class,
|
||||
JobBatchBootstrapper::class,
|
||||
],
|
||||
]);
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
|||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
|
||||
use Illuminate\Routing\Exceptions\UrlGenerationException;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\MailConfigBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
|
|
@ -46,15 +46,15 @@ use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
|||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\Integrations\FortifyRouteTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\Integrations\FortifyRouteBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->mockConsoleOutput = false;
|
||||
|
||||
config(['cache.default' => $cacheDriver = 'redis']);
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
// Reset static properties of classes used in this test file to their default values
|
||||
BroadcastingConfigBootstrapper::$credentialsMap = [];
|
||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
||||
|
|
@ -74,7 +74,7 @@ beforeEach(function () {
|
|||
afterEach(function () {
|
||||
// Reset static properties of classes used in this test file to their default values
|
||||
RootUrlBootstrapper::$rootUrlOverride = null;
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
||||
BroadcastingConfigBootstrapper::$credentialsMap = [];
|
||||
TenancyUrlGenerator::$prefixRouteNames = false;
|
||||
|
|
@ -148,7 +148,7 @@ test('cache data is separated', function (string $bootstrapper) {
|
|||
expect(Cache::get('foo'))->toBe('central');
|
||||
})->with([
|
||||
CacheTagsBootstrapper::class,
|
||||
PrefixCacheTenancyBootstrapper::class,
|
||||
CacheTenancyBootstrapper::class,
|
||||
]);
|
||||
|
||||
test('redis data is separated', function () {
|
||||
|
|
@ -453,7 +453,7 @@ test('BroadcastingConfigBootstrapper makes the app use broadcasters with the cor
|
|||
});
|
||||
|
||||
test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = [
|
||||
MailConfigBootstrapper::$credentialsMap = [
|
||||
'mail.mailers.smtp.username' => 'smtp_username',
|
||||
'mail.mailers.smtp.password' => 'smtp_password'
|
||||
];
|
||||
|
|
@ -462,7 +462,7 @@ test('MailTenancyBootstrapper maps tenant mail credentials to config as specifie
|
|||
'mail.default' => 'smtp',
|
||||
'mail.mailers.smtp.username' => $defaultUsername = 'default username',
|
||||
'mail.mailers.smtp.password' => 'no password',
|
||||
'tenancy.bootstrappers' => [MailTenancyBootstrapper::class],
|
||||
'tenancy.bootstrappers' => [MailConfigBootstrapper::class],
|
||||
]);
|
||||
|
||||
$tenant = Tenant::create(['smtp_password' => $password = 'testing password']);
|
||||
|
|
@ -479,11 +479,11 @@ test('MailTenancyBootstrapper maps tenant mail credentials to config as specifie
|
|||
});
|
||||
|
||||
test('MailTenancyBootstrapper reverts the config and mailer credentials to default when tenancy ends', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
MailConfigBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
config([
|
||||
'mail.default' => 'smtp',
|
||||
'mail.mailers.smtp.password' => $defaultPassword = 'no password',
|
||||
'tenancy.bootstrappers' => [MailTenancyBootstrapper::class],
|
||||
'tenancy.bootstrappers' => [MailConfigBootstrapper::class],
|
||||
]);
|
||||
|
||||
tenancy()->initialize(Tenant::create(['smtp_password' => $tenantPassword = 'testing password']));
|
||||
|
|
@ -676,7 +676,7 @@ test('url generator bootstrapper can make route helper generate links with the t
|
|||
});
|
||||
|
||||
test('fortify route tenancy bootstrapper updates fortify config correctly', function() {
|
||||
config(['tenancy.bootstrappers' => [FortifyRouteTenancyBootstrapper::class]]);
|
||||
config(['tenancy.bootstrappers' => [FortifyRouteBootstrapper::class]]);
|
||||
|
||||
$originalFortifyHome = config('fortify.home');
|
||||
$originalFortifyRedirects = config('fortify.redirects');
|
||||
|
|
@ -697,10 +697,10 @@ test('fortify route tenancy bootstrapper updates fortify config correctly', func
|
|||
return true;
|
||||
})->name($pathIdWelcomeRouteName = 'path.welcome');
|
||||
|
||||
FortifyRouteTenancyBootstrapper::$fortifyHome = $homeRouteName;
|
||||
FortifyRouteBootstrapper::$fortifyHome = $homeRouteName;
|
||||
|
||||
// Make login redirect to the central welcome route
|
||||
FortifyRouteTenancyBootstrapper::$fortifyRedirectMap['login'] = [
|
||||
FortifyRouteBootstrapper::$fortifyRedirectMap['login'] = [
|
||||
'route_name' => $welcomeRouteName,
|
||||
'context' => Context::CENTRAL,
|
||||
];
|
||||
|
|
@ -717,7 +717,7 @@ test('fortify route tenancy bootstrapper updates fortify config correctly', func
|
|||
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
|
||||
|
||||
// Making a route's context will pass the tenant parameter to the route
|
||||
FortifyRouteTenancyBootstrapper::$fortifyRedirectMap['login']['context'] = Context::TENANT;
|
||||
FortifyRouteBootstrapper::$fortifyRedirectMap['login']['context'] = Context::TENANT;
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
|
|
@ -725,8 +725,8 @@ test('fortify route tenancy bootstrapper updates fortify config correctly', func
|
|||
|
||||
// Make the home and login route accept the tenant as a route parameter
|
||||
// To confirm that tenant route parameter gets filled automatically too (path identification works as well as query string)
|
||||
FortifyRouteTenancyBootstrapper::$fortifyHome = $pathIdHomeRouteName;
|
||||
FortifyRouteTenancyBootstrapper::$fortifyRedirectMap['login']['route_name'] = $pathIdWelcomeRouteName;
|
||||
FortifyRouteBootstrapper::$fortifyHome = $pathIdHomeRouteName;
|
||||
FortifyRouteBootstrapper::$fortifyRedirectMap['login']['route_name'] = $pathIdWelcomeRouteName;
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ 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\Bootstrappers\DatabaseSessionBootstrapper;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
|
|
@ -48,7 +48,7 @@ test('central helper can be used in tenant requests', function (bool $enabled, b
|
|||
if ($enabled) {
|
||||
config()->set(
|
||||
'tenancy.bootstrappers',
|
||||
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
||||
array_merge(config('tenancy.bootstrappers'), [DatabaseSessionBootstrapper::class]),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ test('tenant run helper can be used on central requests', function (bool $enable
|
|||
if ($enabled) {
|
||||
config()->set(
|
||||
'tenancy.bootstrappers',
|
||||
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
||||
array_merge(config('tenancy.bootstrappers'), [DatabaseSessionBootstrapper::class]),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -11,18 +11,18 @@ use Stancl\Tenancy\Events\TenancyInitialized;
|
|||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
config(['cache.default' => $cacheDriver = 'redis']);
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
});
|
||||
|
||||
test('global cache manager stores data in global cache', function (string $bootstrapper) {
|
||||
|
|
@ -57,7 +57,7 @@ test('global cache manager stores data in global cache', function (string $boots
|
|||
expect(cache('def'))->toBe('ghi');
|
||||
})->with([
|
||||
CacheTagsBootstrapper::class,
|
||||
PrefixCacheTenancyBootstrapper::class,
|
||||
CacheTenancyBootstrapper::class,
|
||||
]);
|
||||
|
||||
test('the global_cache helper supports the same syntax as the cache helper', function (string $bootstrapper) {
|
||||
|
|
@ -77,5 +77,5 @@ test('the global_cache helper supports the same syntax as the cache helper', fun
|
|||
expect(cache('foo'))->toBe(null); // tenant cache is not affected
|
||||
})->with([
|
||||
CacheTagsBootstrapper::class,
|
||||
PrefixCacheTenancyBootstrapper::class,
|
||||
CacheTenancyBootstrapper::class,
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -8,19 +8,19 @@ 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;
|
||||
use Stancl\Tenancy\Bootstrappers\MailConfigBootstrapper;
|
||||
|
||||
beforeEach(function() {
|
||||
config(['mail.default' => 'smtp']);
|
||||
config(['tenancy.bootstrappers' => [MailTenancyBootstrapper::class]]);
|
||||
MailTenancyBootstrapper::$credentialsMap = [];
|
||||
config(['tenancy.bootstrappers' => [MailConfigBootstrapper::class]]);
|
||||
MailConfigBootstrapper::$credentialsMap = [];
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
MailTenancyBootstrapper::$credentialsMap = [];
|
||||
MailConfigBootstrapper::$credentialsMap = [];
|
||||
});
|
||||
|
||||
// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password
|
||||
|
|
@ -36,7 +36,7 @@ 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'];
|
||||
MailConfigBootstrapper::$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
|
||||
|
|
@ -61,7 +61,7 @@ test('mailer transport uses the correct credentials', function() {
|
|||
'--realpath' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'mail.smtp_password'];
|
||||
MailConfigBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'mail.smtp_password'];
|
||||
tenancy()->initialize($tenant = Tenant::create(['mail' => ['smtp_password' => $nestedTenantPassword = 'nested']]));
|
||||
assertMailerTransportUsesPassword($nestedTenantPassword);
|
||||
tenancy()->end();
|
||||
|
|
|
|||
|
|
@ -10,34 +10,34 @@ use Stancl\Tenancy\Events\TenancyInitialized;
|
|||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Tests\Etc\SpecificCacheStoreService;
|
||||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'tenancy.bootstrappers' => [
|
||||
PrefixCacheTenancyBootstrapper::class
|
||||
CacheTenancyBootstrapper::class
|
||||
],
|
||||
'cache.default' => $cacheDriver = 'redis',
|
||||
'cache.stores.' . $secondCacheDriver = 'redis2' => config('cache.stores.redis'),
|
||||
]);
|
||||
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
|
||||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
|
||||
CacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||
CacheTenancyBootstrapper::$prefixGenerator = null;
|
||||
});
|
||||
|
||||
test('correct cache prefix is used in all contexts', function () {
|
||||
$originalPrefix = config('cache.prefix');
|
||||
$prefixBase = config('tenancy.cache.prefix_base');
|
||||
$getDefaultPrefixForTenant = fn (Tenant $tenant) => $originalPrefix . $prefixBase . $tenant->getTenantKey();
|
||||
$bootstrapper = app(PrefixCacheTenancyBootstrapper::class);
|
||||
$bootstrapper = app(CacheTenancyBootstrapper::class);
|
||||
|
||||
$expectCachePrefixToBe = function (string $prefix) {
|
||||
expect($prefix)
|
||||
|
|
@ -180,7 +180,7 @@ test('cache is prefixed correctly when using a repository injected in a singleto
|
|||
|
||||
test('specific central cache store can be used inside a service', function () {
|
||||
// Make sure 'redis' (the default store) is the only prefixed store
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
|
||||
// Name of the non-default, central cache store that we'll use using cache()->store($cacheStore)
|
||||
$cacheStore = 'redis2';
|
||||
|
||||
|
|
@ -217,7 +217,7 @@ test('specific central cache store can be used inside a service', function () {
|
|||
|
||||
test('only the stores specified in tenantCacheStores get prefixed', function () {
|
||||
// Make sure the currently used store ('redis') is the only store in $tenantCacheStores
|
||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
|
||||
CacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
|
||||
|
||||
$centralValue = 'central-value';
|
||||
$assertStoreIsNotPrefixed = function (string $unprefixedStore) use ($prefixedStore, $centralValue) {
|
||||
|
|
@ -266,7 +266,7 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
|
|||
|
||||
$tenant = Tenant::create();
|
||||
$defaultPrefix = cache()->store()->getPrefix();
|
||||
$bootstrapper = app(PrefixCacheTenancyBootstrapper::class);
|
||||
$bootstrapper = app(CacheTenancyBootstrapper::class);
|
||||
|
||||
// The prefix is the same for both drivers in the central context
|
||||
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
|
||||
|
|
@ -284,12 +284,12 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
|
|||
|
||||
test('cache store prefix generation can be customized', function() {
|
||||
// Use custom prefix generator
|
||||
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
|
||||
CacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
|
||||
return 'redis_tenant_cache_' . $tenant->getTenantKey();
|
||||
});
|
||||
|
||||
expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator);
|
||||
expect(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant = Tenant::create()))
|
||||
expect(CacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator);
|
||||
expect(app(CacheTenancyBootstrapper::class)->generatePrefix($tenant = Tenant::create()))
|
||||
->toBe($customPrefixGenerator($tenant));
|
||||
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
|
@ -316,7 +316,7 @@ test('stores get prefixed using the default way if no prefix generator is specif
|
|||
|
||||
// All stores use the default way of generating the prefix when the prefix generator isn't specified
|
||||
expect($defaultPrefix)
|
||||
->toBe(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant))
|
||||
->toBe(app(CacheTenancyBootstrapper::class)->generatePrefix($tenant))
|
||||
->toBe(cache()->getPrefix()) // Get prefix of the default store ('redis')
|
||||
->toBe(cache()->store('redis2')->getPrefix());
|
||||
|
||||
|
|
|
|||
|
|
@ -15,13 +15,13 @@ use Stancl\Tenancy\Facades\GlobalCache;
|
|||
use Stancl\Tenancy\TenancyServiceProvider;
|
||||
use Stancl\Tenancy\Facades\Tenancy as TenancyFacade;
|
||||
use Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\MailConfigBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
|
|
@ -129,10 +129,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
]);
|
||||
|
||||
$app->singleton(RedisTenancyBootstrapper::class); // todo@samuel use proper approach eg config for singleton registration
|
||||
$app->singleton(PrefixCacheTenancyBootstrapper::class); // todo@samuel use proper approach eg config for singleton registration
|
||||
$app->singleton(CacheTenancyBootstrapper::class); // todo@samuel use proper approach eg config for singleton registration
|
||||
$app->singleton(BroadcastingConfigBootstrapper::class);
|
||||
$app->singleton(BroadcastChannelPrefixBootstrapper::class);
|
||||
$app->singleton(MailTenancyBootstrapper::class);
|
||||
$app->singleton(MailConfigBootstrapper::class);
|
||||
$app->singleton(RootUrlBootstrapper::class);
|
||||
$app->singleton(UrlGeneratorBootstrapper::class);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue