From a9c37d15358e2eef181be07f2c476e5c758c6f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 16 Sep 2019 18:09:09 +0200 Subject: [PATCH] wip --- src/DatabaseManager.php | 12 +++++------- src/Features/TelescopeTags.php | 2 +- .../QueueTenancyBootstrapper.php | 4 ++-- src/TenantManager.php | 3 ++- tests/DatabaseManagerTest.php | 6 +++--- tests/ReidentificationTest.php | 4 ++-- tests/TenantManagerEventsTest.php | 12 ++++++------ tests/TenantManagerTest.php | 6 +++--- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 5b0db28d..9599a228 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -37,7 +37,7 @@ class DatabaseManager */ public function connect(Tenant $tenant) { - $this->createTenantConnection($tenant); + $this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName()); $this->switchConnection($tenant->getConnectionName()); } @@ -54,14 +54,12 @@ class DatabaseManager /** * Create the tenant database connection. * - * @param Tenant $tenant + * @param string $databaseName + * @param string $connectionName * @return void */ - public function createTenantConnection(Tenant $tenant) + public function createTenantConnection($databaseName, $connectionName) { - $databaseName = $tenant->getDatabaseName(); - $connectionName = $tenant->getConnectionName(); - // Create the database connection. $based_on = $this->app['config']['tenancy.database.based_on'] ?? $this->originalDefaultConnectionName; $this->app['config']["database.connections.$connectionName"] = $this->app['config']['database.connections.' . $based_on]; @@ -129,7 +127,7 @@ class DatabaseManager protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager { - $this->createTenantConnection($tenant); + $this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName()); $driver = $this->getDriver($tenant->getConnectionName()); $databaseManagers = $this->app['config']['tenancy.database_managers']; diff --git a/src/Features/TelescopeTags.php b/src/Features/TelescopeTags.php index 160b34e0..2ac4eed6 100644 --- a/src/Features/TelescopeTags.php +++ b/src/Features/TelescopeTags.php @@ -26,7 +26,7 @@ class TelescopeTags implements Feature if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) { $tags = array_merge($tags, [ 'tenant:' . tenant('id'), - 'domain:' . tenant('domain'), + // 'domain:' . tenant('domain'), todo? ]); } diff --git a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php index e6b41241..4006e373 100644 --- a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php +++ b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php @@ -44,13 +44,13 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper return []; } - [$id, $domain] = tenant()->get(['id', 'domain']); + $id = tenant()->get('id'); return [ 'tenant_id' => $id, 'tags' => [ "tenant:$id", - "domain:$domain", + // todo domain? ], ]; } diff --git a/src/TenantManager.php b/src/TenantManager.php index 615ac712..cbe1f84d 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -97,8 +97,9 @@ class TenantManager return $this; } - public function init(string $domain): self + public function init(string $domain = null): self { + $domain = $domain ?? request()->getHost(); $this->initializeTenancy($this->findByDomain($domain)); return $this; diff --git a/tests/DatabaseManagerTest.php b/tests/DatabaseManagerTest.php index 2312f3d5..1cf33ec4 100644 --- a/tests/DatabaseManagerTest.php +++ b/tests/DatabaseManagerTest.php @@ -11,11 +11,11 @@ class DatabaseManagerTest extends TestCase public $autoInitTenancy = false; /** @test */ - public function disconnect_method_works() + public function reconnect_method_works() { $old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); - tenancy()->init(); - tenancy()->disconnectDatabase(); + tenancy()->init('test.localhost'); + app(\Stancl\Tenancy\DatabaseManager::class)->reconnect(); $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); $this->assertSame($old_connection_name, $new_connection_name); diff --git a/tests/ReidentificationTest.php b/tests/ReidentificationTest.php index a7696cf5..74c21492 100644 --- a/tests/ReidentificationTest.php +++ b/tests/ReidentificationTest.php @@ -22,7 +22,7 @@ class ReidentificationTest extends TestCase $originals[$disk] = config("filesystems.disks.{$disk}.root"); } - tenancy()->init('localhost'); + tenancy()->init('test.localhost'); Tenant::new()->withDomains(['second.localhost'])->save(); tenancy()->init('second.localhost'); @@ -49,7 +49,7 @@ class ReidentificationTest extends TestCase { $original = storage_path(); - tenancy()->init('localhost'); + tenancy()->init('test.localhost'); Tenant::new()->withDomains(['second.localhost'])->save(); tenancy()->init('second.localhost'); diff --git a/tests/TenantManagerEventsTest.php b/tests/TenantManagerEventsTest.php index b0af0698..0e8a5b47 100644 --- a/tests/TenantManagerEventsTest.php +++ b/tests/TenantManagerEventsTest.php @@ -14,7 +14,7 @@ class TenantManagerEventsTest extends TestCase { $id = Tenant::new()->withDomains(['foo.localhost'])->save()['id']; - Tenancy::bootstrapping(function ($tenantManager) use ($id) { + Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) { if ($tenantManager->tenant['id'] === $id) { config(['tenancy.foo' => 'bar']); } @@ -30,7 +30,7 @@ class TenantManagerEventsTest extends TestCase { $id = Tenant::new()->withDomains(['foo.localhost'])->save()['id']; - Tenancy::bootstrapped(function ($tenantManager) use ($id) { + Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) { if ($tenantManager->tenant['id'] === $id) { config(['tenancy.foo' => 'bar']); } @@ -46,7 +46,7 @@ class TenantManagerEventsTest extends TestCase { $id = Tenant::new()->withDomains(['foo.localhost'])->save()['id']; - Tenancy::ending(function ($tenantManager) use ($id) { + Tenancy::eventListener('ending', function ($tenantManager) use ($id) { if ($tenantManager->tenant['id'] === $id) { config(['tenancy.foo' => 'bar']); } @@ -64,7 +64,7 @@ class TenantManagerEventsTest extends TestCase { $id = Tenant::new()->withDomains(['foo.localhost'])->save()['id']; - Tenancy::ended(function ($tenantManager) use ($id) { + Tenancy::eventListener('ended', function ($tenantManager) use ($id) { if ($tenantManager->tenant['id'] === $id) { config(['tenancy.foo' => 'bar']); } @@ -102,7 +102,7 @@ class TenantManagerEventsTest extends TestCase $id = Tenant::create('abc.localhost')['id']; - Tenancy::bootstrapping(function ($tenancy) use ($id) { + Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) { if ($tenancy->tenant['id'] === $id) { $tenancy->database->useConnection('tenantabc'); @@ -125,7 +125,7 @@ class TenantManagerEventsTest extends TestCase $id = Tenant::create('abc.localhost')['id']; - Tenancy::bootstrapping(function ($tenancy) use ($id) { + Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) { if ($tenancy->tenant['id'] === $id) { $tenancy->database->useConnection('tenantabc'); // return ['database']; diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index bf26f19b..3723bc81 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -14,13 +14,13 @@ class TenantManagerTest extends TestCase public $autoInitTenancy = false; /** @test */ - public function current_tenant_is_stored_in_the_tenant_property() + public function current_tenant_can_be_retrieved_using_getTenant() { $tenant = Tenant::new()->withDomains(['localhost'])->save(); - tenancy()->init('localhost'); + tenancy()->init('test.localhost'); - $this->assertSame($tenant, tenancy()->tenant); + $this->assertSame($tenant, tenancy()->getTenant()); } /** @test */