From a602cec940006a19f555565ab978f34315352652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 9 May 2020 02:49:08 +0200 Subject: [PATCH] Fix event listener tests, improve domain logic --- ...2019_09_15_000020_create_domains_table.php | 1 + .../Models/Concerns/HasADataColumn.php | 2 + src/Database/Models/Concerns/HasDomains.php | 11 ++++++ .../Models/Concerns/PrimaryDomain.php | 17 +++++++++ .../Models/Concerns/TenantConnection.php | 11 ++++++ src/Database/Models/Domain.php | 3 ++ src/Events/Listeners/QueueableListener.php | 14 ++++++- src/helpers.php | 6 +-- tests/v3/AutomaticModeTest.php | 32 ++++++++++++++++ tests/v3/BootstrapperTest.php | 38 +++++++++++++++++++ tests/v3/DomainTest.php | 7 +--- tests/v3/EventListenerTest.php | 8 ++-- tests/v3/TenantModelTest.php | 12 ++++++ 13 files changed, 148 insertions(+), 14 deletions(-) create mode 100644 src/Database/Models/Concerns/HasDomains.php create mode 100644 src/Database/Models/Concerns/PrimaryDomain.php create mode 100644 src/Database/Models/Concerns/TenantConnection.php create mode 100644 tests/v3/AutomaticModeTest.php create mode 100644 tests/v3/BootstrapperTest.php diff --git a/assets/migrations/2019_09_15_000020_create_domains_table.php b/assets/migrations/2019_09_15_000020_create_domains_table.php index aad343ee..b9bb6b07 100644 --- a/assets/migrations/2019_09_15_000020_create_domains_table.php +++ b/assets/migrations/2019_09_15_000020_create_domains_table.php @@ -19,6 +19,7 @@ class CreateDomainsTable extends Migration $table->increments('id'); $table->string('domain', 255)->unique(); $table->string('tenant_id', 36); + $table->unsignedTinyInteger('is_primary')->default(false); $table->timestamps(); $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); diff --git a/src/Database/Models/Concerns/HasADataColumn.php b/src/Database/Models/Concerns/HasADataColumn.php index 8f10a407..19f6c53c 100644 --- a/src/Database/Models/Concerns/HasADataColumn.php +++ b/src/Database/Models/Concerns/HasADataColumn.php @@ -1,7 +1,9 @@ hasMany(config('tenancy.domain_model')); + } +} \ No newline at end of file diff --git a/src/Database/Models/Concerns/PrimaryDomain.php b/src/Database/Models/Concerns/PrimaryDomain.php new file mode 100644 index 00000000..aa965484 --- /dev/null +++ b/src/Database/Models/Concerns/PrimaryDomain.php @@ -0,0 +1,17 @@ +primary_domain_hostname, $route, $parameters, $absolute); + } +} diff --git a/src/Database/Models/Concerns/TenantConnection.php b/src/Database/Models/Concerns/TenantConnection.php new file mode 100644 index 00000000..0dc11c7a --- /dev/null +++ b/src/Database/Models/Concerns/TenantConnection.php @@ -0,0 +1,11 @@ + 'bool', + ]; public static function booted() { diff --git a/src/Events/Listeners/QueueableListener.php b/src/Events/Listeners/QueueableListener.php index 75bc2a91..ab23ddf0 100644 --- a/src/Events/Listeners/QueueableListener.php +++ b/src/Events/Listeners/QueueableListener.php @@ -8,8 +8,18 @@ abstract class QueueableListener implements ShouldQueue { public static $shouldQueue = false; - public function shouldQueue() + abstract public function handle(); + + public function shouldQueue($event) { - return static::$shouldQueue; + if (static::$shouldQueue) { + return true; + } else { + // The listener is not queued so we manually + // pass the event to the handle() method. + $this->handle($event); + + return false; + } } } diff --git a/src/helpers.php b/src/helpers.php index 80f94c18..70007826 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -53,12 +53,10 @@ if (! function_exists('global_cache')) { } if (! function_exists('tenant_route')) { - function tenant_route($route, $parameters = [], string $domain = null) + function tenant_route(string $domain, $route, $parameters = [], $absolute = true) { - $domain = $domain ?? request()->getHost(); - // replace first occurance of hostname fragment with $domain - $url = route($route, $parameters); + $url = route($route, $parameters, $absolute); $hostname = parse_url($url, PHP_URL_HOST); $position = strpos($url, $hostname); diff --git a/tests/v3/AutomaticModeTest.php b/tests/v3/AutomaticModeTest.php new file mode 100644 index 00000000..b273bb06 --- /dev/null +++ b/tests/v3/AutomaticModeTest.php @@ -0,0 +1,32 @@ +hasMany(Domain::class); - } + use HasDomains; } diff --git a/tests/v3/EventListenerTest.php b/tests/v3/EventListenerTest.php index 62951226..9da703d9 100644 --- a/tests/v3/EventListenerTest.php +++ b/tests/v3/EventListenerTest.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy\Tests\v3; +use Illuminate\Events\CallQueuedListener; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Queue; use Stancl\Tenancy\Database\Models\Tenant; @@ -9,7 +10,6 @@ use Stancl\Tenancy\Events\Listeners\QueueableListener; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Tests\TestCase; -// todo these tests do not pass: https://github.com/laravel/framework/issues/32722 class EventListenerTest extends TestCase { /** @test */ @@ -30,12 +30,14 @@ class EventListenerTest extends TestCase { Queue::fake(); - FooListener::$shouldQueue = true; Event::listen(TenantCreated::class, FooListener::class); + FooListener::$shouldQueue = true; Tenant::create(); - Queue::assertPushed(FooListener::class); + Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job) { + return $job->class === FooListener::class; + }); $this->assertFalse(app()->bound('foo')); } diff --git a/tests/v3/TenantModelTest.php b/tests/v3/TenantModelTest.php index 97ec074f..774a73cf 100644 --- a/tests/v3/TenantModelTest.php +++ b/tests/v3/TenantModelTest.php @@ -106,4 +106,16 @@ class TenantModelTest extends TestCase $this->assertSame(1, $tenant1->id); $this->assertSame(2, $tenant2->id); } + + /** @test */ + public function custom_tenant_model_can_be_used() + { + + } + + /** @test */ + public function custom_tenant_model_that_doesnt_extend_vendor_Tenant_model_can_be_used() + { + + } }