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

Filesystem logic refactor, improved defaults for cache tenancy (#42)

* refactor FilesystemTenancyBootstrapper

* clean up tests and improve coverage

* minor maintenance mode changes

* Improve tenants:migrate --skip-failing logic

* make tenants:migrate output consistently formatted

* minor RootUrlBootstrapper + misc changes

* cache bootstrapper-related improvements

* Fix code style (php-cs-fixer)

* misc refactor

* Fix code style (php-cs-fixer)

* add %original_storage_path% to fs bootstrapper, improve default config for cache

* rename method

* inject concrete implementations where needed instead of abstracts

* Fix code style (php-cs-fixer)

* refactor DealsWithTenantSymlinks

* remove obsolete phpstan ignore

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
Samuel Štancl 2024-04-02 04:26:10 +02:00 committed by GitHub
parent 4b6fa22aa7
commit a41ad69023
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 234 additions and 160 deletions

View file

@ -50,11 +50,15 @@ use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
use Stancl\Tenancy\Bootstrappers\Integrations\FortifyRouteBootstrapper;
// todo refactor this file -- too much stuff happening here
beforeEach(function () {
$this->mockConsoleOutput = false;
config(['cache.default' => $cacheDriver = 'redis']);
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
config([
'cache.default' => 'redis',
'tenancy.cache.stores' => ['redis'],
]);
// Reset static properties of classes used in this test file to their default values
BroadcastingConfigBootstrapper::$credentialsMap = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
@ -74,7 +78,6 @@ beforeEach(function () {
afterEach(function () {
// Reset static properties of classes used in this test file to their default values
RootUrlBootstrapper::$rootUrlOverride = null;
CacheTenancyBootstrapper::$tenantCacheStores = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
BroadcastingConfigBootstrapper::$credentialsMap = [];
TenancyUrlGenerator::$prefixRouteNames = false;

View file

@ -19,9 +19,9 @@ beforeEach(function () {
],
'cache.default' => $cacheDriver = 'redis',
'cache.stores.' . $secondCacheDriver = 'redis2' => config('cache.stores.redis'),
'tenancy.cache.stores' => [$cacheDriver, $secondCacheDriver],
]);
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
CacheTenancyBootstrapper::$prefixGenerator = null;
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
@ -29,7 +29,6 @@ beforeEach(function () {
});
afterEach(function () {
CacheTenancyBootstrapper::$tenantCacheStores = [];
CacheTenancyBootstrapper::$prefixGenerator = null;
});
@ -180,7 +179,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
CacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
config(['tenancy.cache.stores' => ['redis']]);
// Name of the non-default, central cache store that we'll use using cache()->store($cacheStore)
$cacheStore = 'redis2';
@ -217,7 +216,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
CacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
config(['tenancy.cache.stores' => [$prefixedStore = 'redis']]);
$centralValue = 'central-value';
$assertStoreIsNotPrefixed = function (string $unprefixedStore) use ($prefixedStore, $centralValue) {

View file

@ -24,6 +24,7 @@ use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
beforeEach(function () {
@ -298,6 +299,8 @@ test('run command with array of tenants works', function () {
});
test('link command works', function() {
config(['tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class]]);
$tenantId1 = Tenant::create()->getTenantKey();
$tenantId2 = Tenant::create()->getTenantKey();
pest()->artisan('tenants:link')
@ -318,6 +321,8 @@ test('link command works', function() {
});
test('link command works with a specified tenant', function() {
config(['tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class]]);
$tenantKey = Tenant::create()->getTenantKey();
pest()->artisan('tenants:link', [

View file

@ -8,7 +8,6 @@ use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
use Stancl\Tenancy\Database\Concerns\HasPending;
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
use Stancl\Tenancy\Database\Models;
/**
@ -18,7 +17,7 @@ class Tenant extends Models\Tenant implements TenantWithDatabase
{
public static array $extraCustomColumns = [];
use HasDatabase, HasDomains, HasPending, MaintenanceMode;
use HasDatabase, HasDomains, HasPending;
public static function getCustomColumns(): array
{

View file

@ -2,7 +2,6 @@
declare(strict_types=1);
use Stancl\Tenancy\Overrides\CacheManager;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded;
@ -14,17 +13,15 @@ use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
beforeEach(function () {
config(['cache.default' => $cacheDriver = 'redis']);
CacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
config([
'cache.default' => 'redis',
'tenancy.cache.stores' => ['redis'],
]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
afterEach(function () {
CacheTenancyBootstrapper::$tenantCacheStores = [];
});
test('global cache manager stores data in global cache', function (string $bootstrapper) {
config(['tenancy.bootstrappers' => [$bootstrapper]]);

View file

@ -10,6 +10,10 @@ use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
config(['tenancy.models.tenant' => MaintenanceTenant::class]);
});
test('tenants can be in maintenance mode', function () {
Route::get('/foo', function () {
return 'bar';

View file

@ -16,6 +16,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
use Stancl\Tenancy\Controllers\TenantAssetController;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
beforeEach(function () {
@ -32,6 +34,7 @@ beforeEach(function () {
$cloneAction->handle(Route::getRoutes()->getByName('stancl.tenancy.asset'));
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
test('asset can be accessed using the url returned by the tenant asset helper', function () {
@ -68,6 +71,9 @@ test('asset helper returns a link to tenant asset controller when asset url is n
tenancy()->initialize($tenant);
expect(asset('foo'))->toBe(route('stancl.tenancy.asset', ['path' => 'foo']));
tenancy()->end();
expect(asset('foo'))->toBe('http://localhost/foo');
});
test('asset helper returns a link to an external url when asset url is not null', function () {
@ -78,6 +84,9 @@ test('asset helper returns a link to an external url when asset url is not null'
tenancy()->initialize($tenant);
expect(asset('foo'))->toBe("https://an-s3-bucket/tenant{$tenant->id}/foo");
tenancy()->end();
expect(asset('foo'))->toBe('https://an-s3-bucket/foo');
});
test('asset helper works correctly with path identification', function (bool $kernelIdentification) {
@ -243,14 +252,3 @@ test('test asset controller returns a 404 when accessing a file outside the stor
'X-Tenant' => $tenant->id,
]);
});
function getEnvironmentSetUp($app)
{
$app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace(pest()->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}