mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +00:00
* Initial draft * Apply fixes from StyleCI * Use CI on master branch too * Pass correct argument to queued DB creators/deleters * Apply fixes from StyleCI * Remove new interface from MySQLDBManager * Make phpunit run * Apply fixes from StyleCI * Fix static property * Default databaseName * Use database transactions for creating users & granting permissions * Apply fixes from StyleCI * Get old tests to pass * Apply fixes from StyleCI * Add tests for PermissionControlledMySQLDatabaseManager * Apply fixes from StyleCI * Write test for extra config, fix bug with extra config * Apply fixes from StyleCI
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Tests;
|
|
|
|
use Stancl\Tenancy\DatabaseManager;
|
|
use Stancl\Tenancy\Tenant;
|
|
|
|
class DatabaseManagerTest extends TestCase
|
|
{
|
|
/** @test */
|
|
public function reconnect_method_works()
|
|
{
|
|
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
|
$this->createTenant();
|
|
$this->initTenancy();
|
|
app(\Stancl\Tenancy\DatabaseManager::class)->reconnect();
|
|
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
|
|
|
$this->assertSame($old_connection_name, $new_connection_name);
|
|
$this->assertNotEquals('tenant', $new_connection_name);
|
|
}
|
|
|
|
/** @test */
|
|
public function db_name_is_prefixed_with_db_path_when_sqlite_is_used()
|
|
{
|
|
if (file_exists(database_path('foodb'))) {
|
|
unlink(database_path('foodb')); // cleanup
|
|
}
|
|
config(['database.connections.fooconn.driver' => 'sqlite']);
|
|
$tenant = Tenant::new()->withData([
|
|
'_tenancy_db_name' => 'foodb',
|
|
'_tenancy_db_connection' => 'fooconn',
|
|
])->save();
|
|
app(DatabaseManager::class)->createTenantConnection($tenant);
|
|
|
|
$this->assertSame(config('database.connections.tenant.database'), database_path('foodb'));
|
|
}
|
|
|
|
/** @test */
|
|
public function the_default_db_is_used_when_template_connection_is_null()
|
|
{
|
|
$this->assertSame('central', config('database.default'));
|
|
config([
|
|
'database.connections.central.foo' => 'bar',
|
|
'tenancy.database.template_connection' => null,
|
|
]);
|
|
|
|
$this->createTenant();
|
|
$this->initTenancy();
|
|
|
|
$this->assertSame('tenant', config('database.default'));
|
|
$this->assertSame('bar', config('database.connections.' . config('database.default') . '.foo'));
|
|
}
|
|
|
|
/** @test */
|
|
public function ending_tenancy_doesnt_purge_the_central_connection()
|
|
{
|
|
$this->markTestIncomplete('Seems like this only happens on MySQL?');
|
|
|
|
// regression test for https://github.com/stancl/tenancy/pull/189
|
|
// config(['tenancy.migrate_after_creation' => true]);
|
|
|
|
tenancy()->create(['foo.localhost']);
|
|
tenancy()->init('foo.localhost');
|
|
tenancy()->end();
|
|
|
|
$this->assertNotEmpty(tenancy()->all());
|
|
|
|
tenancy()->all()->each->delete();
|
|
}
|
|
}
|