1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 08:14:02 +00:00

Fixing testing

This commit is contained in:
Nuradiyana 2020-02-27 00:45:06 +07:00
parent f5f03f8097
commit 0887790ce4
3 changed files with 29 additions and 9 deletions

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy;
use Closure; use Closure;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager; use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Future\CanSetConnection; use Stancl\Tenancy\Contracts\Future\CanSetConnection;
@ -110,9 +111,9 @@ class DatabaseManager
* Get the name of the connection that $connectionName should be based on. * Get the name of the connection that $connectionName should be based on.
* *
* @param string $connectionName * @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 return ($connectionName !== 'tenant' ? $connectionName : null) // 'tenant' is not a specific connection, it's the default
?? $this->app['config']['tenancy.database.based_on'] ?? $this->app['config']['tenancy.database.based_on']
@ -149,6 +150,8 @@ class DatabaseManager
* @param Tenant $tenant * @param Tenant $tenant
* @return void * @return void
* @throws TenantCannotBeCreatedException * @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException
*/ */
public function ensureTenantCanBeCreated(Tenant $tenant): void public function ensureTenantCanBeCreated(Tenant $tenant): void
{ {
@ -163,6 +166,7 @@ class DatabaseManager
* @param Tenant $tenant * @param Tenant $tenant
* @param ShouldQueue[]|callable[] $afterCreating * @param ShouldQueue[]|callable[] $afterCreating
* @return void * @return void
* @throws DatabaseManagerNotRegisteredException
*/ */
public function createDatabase(Tenant $tenant, array $afterCreating = []) public function createDatabase(Tenant $tenant, array $afterCreating = [])
{ {
@ -204,6 +208,7 @@ class DatabaseManager
* *
* @param Tenant $tenant * @param Tenant $tenant
* @return void * @return void
* @throws DatabaseManagerNotRegisteredException
*/ */
public function deleteDatabase(Tenant $tenant) public function deleteDatabase(Tenant $tenant)
{ {
@ -226,6 +231,7 @@ class DatabaseManager
* *
* @param Tenant $tenant * @param Tenant $tenant
* @return TenantDatabaseManager * @return TenantDatabaseManager
* @throws DatabaseManagerNotRegisteredException
*/ */
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{ {

View file

@ -4,18 +4,25 @@ declare(strict_types=1);
namespace Stancl\Tenancy\TenantDatabaseManagers; namespace Stancl\Tenancy\TenantDatabaseManagers;
use Illuminate\Database\Connection;
use Illuminate\Contracts\Config\Repository; 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; use Stancl\Tenancy\Contracts\TenantDatabaseManager;
class PostgreSQLSchemaManager implements TenantDatabaseManager class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
{ {
/** @var \Illuminate\Database\Connection */ /** @var Connection */
protected $database; 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 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'"); 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;
}
} }

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
use Illuminate\Support\Str;
class DatabaseSchemaManagerTest extends TestCase class DatabaseSchemaManagerTest extends TestCase
{ {
@ -21,6 +22,7 @@ class DatabaseSchemaManagerTest extends TestCase
'tenancy.database.based_on' => null, 'tenancy.database.based_on' => null,
'tenancy.database.suffix' => '', 'tenancy.database.suffix' => '',
'tenancy.database.separate_by' => 'schema', 'tenancy.database.separate_by' => 'schema',
'tenancy.database_managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
]); ]);
} }
@ -88,7 +90,7 @@ class DatabaseSchemaManagerTest extends TestCase
} }
/** @test */ /** @test */
public function databases_are_separated() public function schemas_are_separated()
{ {
// copied from DataSeparationTest // copied from DataSeparationTest
@ -135,7 +137,7 @@ class DatabaseSchemaManagerTest extends TestCase
$this->assertSame(null, User::first()); $this->assertSame(null, User::first());
tenancy()->init('tenant1.localhost'); 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); $this->assertSame('xxx', User::first()->name);
} }
} }