From 1df2b9d66d58f7a6dfc9084ec09aeaa7971a03cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 4 Oct 2019 21:09:19 +0200 Subject: [PATCH] Automigration test, config key, QueueTenancyBootstrapper support for QueueFake --- assets/config.php | 1 + .../QueueTenancyBootstrapper.php | 9 +++++--- tests/TenantManagerTest.php | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/assets/config.php b/assets/config.php index e425b806..8f0c4941 100644 --- a/assets/config.php +++ b/assets/config.php @@ -90,6 +90,7 @@ return [ ], 'home_url' => '/app', 'migrate_after_creation' => false, // run migrations after creating a tenant + 'queue_automatic_migration' => false, // queue the automatic post-tenant-creation migrations 'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant 'queue_database_creation' => false, 'queue_database_deletion' => false, diff --git a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php index e97702ef..5060a9fc 100644 --- a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php +++ b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\TenancyBootstrappers; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Support\Testing\Fakes\QueueFake; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Tenant; @@ -21,9 +22,11 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper $this->app = $app; $bootstrapper = &$this; - $this->app['queue']->createPayloadUsing(function () use (&$bootstrapper) { - return $bootstrapper->getPayload(); - }); + if (! $queue = $this->app['queue'] instanceof QueueFake) { + $queue->createPayloadUsing(function () use (&$bootstrapper) { + return $bootstrapper->getPayload(); + }); + } } public function start(Tenant $tenant) diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 18723920..c44b6237 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -5,9 +5,11 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; +use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator; use Stancl\Tenancy\Tenant; use Stancl\Tenancy\TenantManager; @@ -244,4 +246,24 @@ class TenantManagerTest extends TestCase $this->expectException(TenantWithThisIdAlreadyExistsException::class); Tenant::create(['bar.localhost'], ['id' => $id]); } + + /** @test */ + public function automigration_can_be_queued() + { + Queue::fake(); + + config([ + 'tenancy.migrate_after_creation' => true, + 'tenancy.queue_automatic_migration' => true, + ]); + + $tenant = Tenant::new()->save(); + tenancy()->initialize($tenant); + + Queue::assertPushed(QueuedTenantDatabaseMigrator::class); + + $this->assertFalse(\Schema::hasTable('users')); + (new QueuedTenantDatabaseMigrator($tenant))->handle(); + $this->assertTrue(\Schema::hasTable('users')); + } }