diff --git a/assets/config.php b/assets/config.php index 4b8a8f5e..4547bed6 100644 --- a/assets/config.php +++ b/assets/config.php @@ -100,7 +100,7 @@ return [ */ 'resolvers' => [ Resolvers\DomainTenantResolver::class => [ - 'cache' => false, // todo@tests find why some tests fail when these are set to true + 'cache' => false, 'cache_ttl' => 3600, // seconds 'cache_store' => null, // null = default ], diff --git a/composer.json b/composer.json index 794787e0..ff5e531a 100644 --- a/composer.json +++ b/composer.json @@ -65,6 +65,7 @@ "scripts": { "docker-up": "docker-compose up -d", "docker-down": "docker-compose down", + "docker-restart": "docker-compose down && docker-compose up -d", "docker-rebuild": "PHP_VERSION=8.3 docker-compose up -d --no-deps --build", "docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml", "coverage": "open coverage/phpunit/html/index.html", diff --git a/src/Jobs/DeleteDomains.php b/src/Jobs/DeleteDomains.php index 15fff779..81013d2e 100644 --- a/src/Jobs/DeleteDomains.php +++ b/src/Jobs/DeleteDomains.php @@ -15,12 +15,9 @@ class DeleteDomains { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - protected TenantWithDatabase&Model $tenant; - - public function __construct(TenantWithDatabase&Model $tenant) - { - $this->tenant = $tenant; - } + public function __construct( + protected TenantWithDatabase&Model $tenant, + ) {} public function handle(): void { diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 6675972b..61920b14 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -4,13 +4,15 @@ declare(strict_types=1); namespace Stancl\Tenancy\Resolvers; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Model; -use Stancl\Tenancy\Contracts\Domain; -use Stancl\Tenancy\Contracts\SingleDomainTenant; -use Stancl\Tenancy\Contracts\Tenant; -use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; use Stancl\Tenancy\Tenancy; +use Stancl\Tenancy\Contracts\Domain; +use Stancl\Tenancy\Contracts\Tenant; +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use Stancl\Tenancy\Contracts\SingleDomainTenant; +use Illuminate\Database\Eloquent\Relations\Relation; +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; class DomainTenantResolver extends Contracts\CachedTenantResolver { @@ -69,12 +71,14 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver public function getPossibleCacheKeys(Tenant&Model $tenant): array { + $domains = []; + if ($tenant instanceof SingleDomainTenant) { $domains = array_filter([ $tenant->getOriginal('domain'), // Previous domain $tenant->domain, // Current domain ]); - } else { + } elseif (method_exists($tenant, 'domains') && $tenant->domains() instanceof Relation) { /** @var Tenant&Model $tenant */ $tenant->unsetRelation('domains'); diff --git a/tests/Bootstrappers/BroadcastChannelPrefixBootstrapperTest.php b/tests/Bootstrappers/BroadcastChannelPrefixBootstrapperTest.php index c10aa70f..29a80cbe 100644 --- a/tests/Bootstrappers/BroadcastChannelPrefixBootstrapperTest.php +++ b/tests/Bootstrappers/BroadcastChannelPrefixBootstrapperTest.php @@ -1,19 +1,20 @@ driver())->toBe($broadcaster); expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqual($channelNames); }); - diff --git a/tests/Bootstrappers/BroadcastingConfigBootstrapperTest.php b/tests/Bootstrappers/BroadcastingConfigBootstrapperTest.php index 23efac3c..5eb987db 100644 --- a/tests/Bootstrappers/BroadcastingConfigBootstrapperTest.php +++ b/tests/Bootstrappers/BroadcastingConfigBootstrapperTest.php @@ -1,14 +1,15 @@ driver())->message)->toBe($defaultMessage); }); - diff --git a/tests/Bootstrappers/CacheTenancyBootstrapperTest.php b/tests/Bootstrappers/CacheTenancyBootstrapperTest.php index 1f28e20b..9e44cf88 100644 --- a/tests/Bootstrappers/CacheTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/CacheTenancyBootstrapperTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Cache\CacheManager; +use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Tests\Etc\CacheService; diff --git a/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php b/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php index 1f9c018a..06aad296 100644 --- a/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php @@ -1,20 +1,21 @@ toBe($baseUrl); expect(config('app.url'))->toBe($baseUrl); }); - diff --git a/tests/Bootstrappers/UrlGeneratorBootstrapperTest.php b/tests/Bootstrappers/UrlGeneratorBootstrapperTest.php index 4ef8dce7..8ef3169d 100644 --- a/tests/Bootstrappers/UrlGeneratorBootstrapperTest.php +++ b/tests/Bootstrappers/UrlGeneratorBootstrapperTest.php @@ -1,18 +1,19 @@ DatabaseAndDomainTenant::class]); + config(['tenancy.models.tenant' => Tenant::class]); }); test('job deletes domains successfully', function () { - $tenant = DatabaseAndDomainTenant::create(); + $tenant = Tenant::create(); $tenant->domains()->create([ 'domain' => 'foo.localhost', @@ -21,12 +21,7 @@ test('job deletes domains successfully', function () { expect($tenant->domains()->count())->toBe(2); - (new DeleteDomains($tenant))->handle(); + (new DeleteDomains($tenant->refresh()))->handle(); expect($tenant->refresh()->domains()->count())->toBe(0); }); - -class DatabaseAndDomainTenant extends \Stancl\Tenancy\Tests\Etc\Tenant -{ - use HasDomains; -} diff --git a/tests/MailTest.php b/tests/MailTest.php index 6d9dbae5..c41b5578 100644 --- a/tests/MailTest.php +++ b/tests/MailTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Mail\MailManager; +use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; diff --git a/tests/PreventAccessFromUnwantedDomainsTest.php b/tests/PreventAccessFromUnwantedDomainsTest.php index 2cd293f2..18c0daaa 100644 --- a/tests/PreventAccessFromUnwantedDomainsTest.php +++ b/tests/PreventAccessFromUnwantedDomainsTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Stancl\Tenancy\Enums\RouteMode; +use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Contracts\Http\Kernel; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; diff --git a/tests/SingleDatabaseTenancyTest.php b/tests/SingleDatabaseTenancyTest.php index 8d807523..e51be060 100644 --- a/tests/SingleDatabaseTenancyTest.php +++ b/tests/SingleDatabaseTenancyTest.php @@ -2,15 +2,15 @@ declare(strict_types=1); +use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\QueryException; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Validator; -use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel; +use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Database\Concerns\BelongsToTenant; +use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel; use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules; -use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant; beforeEach(function () { Schema::create('posts', function (Blueprint $table) { @@ -31,12 +31,12 @@ beforeEach(function () { $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade'); }); - config(['tenancy.models.tenant' => Tenant::class]); + config(['tenancy.models.tenant' => SingleDatabaseTenant::class]); }); test('primary models are scoped to the current tenant', function () { // acme context - tenancy()->initialize($acme = Tenant::create([ + tenancy()->initialize($acme = SingleDatabaseTenant::create([ 'id' => 'acme', ])); @@ -52,7 +52,7 @@ test('primary models are scoped to the current tenant', function () { // ====================================== // foobar context - tenancy()->initialize(Tenant::create([ + tenancy()->initialize(SingleDatabaseTenant::create([ 'id' => 'foobar', ])); @@ -85,7 +85,7 @@ test('primary models are scoped to the current tenant', function () { }); test('secondary models ARE scoped to the current tenant when accessed directly and parent relationship trait is used', function () { - $acme = Tenant::create([ + $acme = SingleDatabaseTenant::create([ 'id' => 'acme', ]); @@ -97,7 +97,7 @@ test('secondary models ARE scoped to the current tenant when accessed directly a expect(ScopedComment::count())->toBe(1); }); - $foobar = Tenant::create([ + $foobar = SingleDatabaseTenant::create([ 'id' => 'foobar', ]); @@ -119,7 +119,7 @@ test('secondary models ARE scoped to the current tenant when accessed directly a test('secondary models are scoped correctly', function () { // Secondary models are scoped to the current tenant when accessed via primary model // acme context - tenancy()->initialize($acme = Tenant::create([ + tenancy()->initialize($acme = SingleDatabaseTenant::create([ 'id' => 'acme', ])); @@ -128,7 +128,7 @@ test('secondary models are scoped correctly', function () { // ================ // foobar context - tenancy()->initialize(Tenant::create([ + tenancy()->initialize(SingleDatabaseTenant::create([ 'id' => 'foobar', ])); @@ -161,7 +161,7 @@ test('global models are not scoped at all', function () { GlobalResource::create(['text' => 'First']); GlobalResource::create(['text' => 'Second']); - $acme = Tenant::create([ + $acme = SingleDatabaseTenant::create([ 'id' => 'acme', ]); @@ -176,7 +176,7 @@ test('global models are not scoped at all', function () { }); test('tenant id and relationship is auto added when creating primary resources in tenant context', function () { - tenancy()->initialize($acme = Tenant::create([ + tenancy()->initialize($acme = SingleDatabaseTenant::create([ 'id' => 'acme', ])); @@ -208,9 +208,11 @@ test('tenant id column name can be customized', function () { $table->foreign('team_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); }); - tenancy()->initialize($acme = Tenant::create([ + $acme = SingleDatabaseTenant::create([ 'id' => 'acme', - ])); + ]); + + tenancy()->initialize($acme); $post = Post::create(['text' => 'Foo']); @@ -218,7 +220,7 @@ test('tenant id column name can be customized', function () { // ====================================== // foobar context - tenancy()->initialize($foobar = Tenant::create([ + tenancy()->initialize($foobar = SingleDatabaseTenant::create([ 'id' => 'foobar', ])); @@ -248,7 +250,7 @@ test('the model returned by the tenant helper has unique and exists validation r $table->unique(['tenant_id', 'slug']); }); - tenancy()->initialize($acme = Tenant::create([ + tenancy()->initialize($acme = SingleDatabaseTenant::create([ 'id' => 'acme', ])); @@ -278,7 +280,7 @@ test('the model returned by the tenant helper has unique and exists validation r expect($existsFails)->toBeFalse(); }); -class Tenant extends TestTenant +class SingleDatabaseTenant extends Tenant { use HasScopedValidationRules; } diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 67b25a78..ca3c4902 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -64,8 +64,8 @@ test('autoincrement ids are supported', function () { unset(app()[UniqueIdentifierGenerator::class]); - $tenant1 = Tenant::create(); - $tenant2 = Tenant::create(); + $tenant1 = MyTenant::create(); + $tenant2 = MyTenant::create(); expect($tenant1->id)->toBe(1); expect($tenant2->id)->toBe(2); @@ -200,8 +200,7 @@ test('currentOrFail method throws an exception if there is no currently initiali expect(fn() => Tenant::currentOrFail())->toThrow(TenancyNotInitializedException::class); }); - -class MyTenant extends Tenant +class MyTenant extends Stancl\Tenancy\Database\Models\Tenant { protected $table = 'tenants'; }