From 0887790ce42cba6b21b84c36e0116ea78ec8e870 Mon Sep 17 00:00:00 2001 From: Nuradiyana Date: Thu, 27 Feb 2020 00:45:06 +0700 Subject: [PATCH] Fixing testing --- src/DatabaseManager.php | 10 +++++++-- .../PostgreSQLSchemaManager.php | 22 ++++++++++++++----- tests/DatabaseSchemaManagerTest.php | 6 +++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index e00f6ab9..455fed89 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy; use Closure; use Illuminate\Contracts\Queue\ShouldQueue; +use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; use Illuminate\Foundation\Application; use Stancl\Tenancy\Contracts\Future\CanSetConnection; @@ -110,9 +111,9 @@ class DatabaseManager * Get the name of the connection that $connectionName should be based on. * * @param string $connectionName - * @return string + * @return string|null */ - public function getBaseConnection(string $connectionName): string + public function getBaseConnection(string $connectionName): ?string { return ($connectionName !== 'tenant' ? $connectionName : null) // 'tenant' is not a specific connection, it's the default ?? $this->app['config']['tenancy.database.based_on'] @@ -149,6 +150,8 @@ class DatabaseManager * @param Tenant $tenant * @return void * @throws TenantCannotBeCreatedException + * @throws DatabaseManagerNotRegisteredException + * @throws TenantDatabaseAlreadyExistsException */ public function ensureTenantCanBeCreated(Tenant $tenant): void { @@ -163,6 +166,7 @@ class DatabaseManager * @param Tenant $tenant * @param ShouldQueue[]|callable[] $afterCreating * @return void + * @throws DatabaseManagerNotRegisteredException */ public function createDatabase(Tenant $tenant, array $afterCreating = []) { @@ -204,6 +208,7 @@ class DatabaseManager * * @param Tenant $tenant * @return void + * @throws DatabaseManagerNotRegisteredException */ public function deleteDatabase(Tenant $tenant) { @@ -226,6 +231,7 @@ class DatabaseManager * * @param Tenant $tenant * @return TenantDatabaseManager + * @throws DatabaseManagerNotRegisteredException */ public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager { diff --git a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php index fe753304..d1e99faa 100644 --- a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php +++ b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php @@ -4,18 +4,25 @@ declare(strict_types=1); namespace Stancl\Tenancy\TenantDatabaseManagers; +use Illuminate\Database\Connection; use Illuminate\Contracts\Config\Repository; -use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager; +use Stancl\Tenancy\Contracts\Future\CanSetConnection; +use Illuminate\Database\DatabaseManager; use Stancl\Tenancy\Contracts\TenantDatabaseManager; -class PostgreSQLSchemaManager implements TenantDatabaseManager +class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection { - /** @var \Illuminate\Database\Connection */ + /** @var Connection */ protected $database; - public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager) + /** @var string */ + protected $connection; + + public function __construct(Repository $config, DatabaseManager $databaseManager) { - $this->database = $databaseManager->connection($config['tenancy.database_manager_connections.pgsql']); + $this->connection = $config['tenancy.database_manager_connections.pgsql']; + + $this->database = $databaseManager->connection($this->connection); } public function createDatabase(string $name): bool @@ -32,4 +39,9 @@ class PostgreSQLSchemaManager implements TenantDatabaseManager { return (bool) $this->database->select("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$name'"); } + + public function setConnection(string $connection): void + { + $this->connection = $connection; + } } diff --git a/tests/DatabaseSchemaManagerTest.php b/tests/DatabaseSchemaManagerTest.php index aaaa0b91..2d37d29f 100644 --- a/tests/DatabaseSchemaManagerTest.php +++ b/tests/DatabaseSchemaManagerTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; use Stancl\Tenancy\Tenant; +use Illuminate\Support\Str; class DatabaseSchemaManagerTest extends TestCase { @@ -21,6 +22,7 @@ class DatabaseSchemaManagerTest extends TestCase 'tenancy.database.based_on' => null, 'tenancy.database.suffix' => '', 'tenancy.database.separate_by' => 'schema', + 'tenancy.database_managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, ]); } @@ -88,7 +90,7 @@ class DatabaseSchemaManagerTest extends TestCase } /** @test */ - public function databases_are_separated() + public function schemas_are_separated() { // copied from DataSeparationTest @@ -135,7 +137,7 @@ class DatabaseSchemaManagerTest extends TestCase $this->assertSame(null, User::first()); tenancy()->init('tenant1.localhost'); - DB::table('users')->where('id', 1)->update(['name' => 'xxx']); + \DB::table('users')->where('id', 1)->update(['name' => 'xxx']); $this->assertSame('xxx', User::first()->name); } }