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

complete test sqlite manager customize path

This commit is contained in:
Abrar Ahmad 2022-09-14 14:19:55 +05:00
parent abd17f83a1
commit 13fed332cd
2 changed files with 25 additions and 4 deletions

View file

@ -10,10 +10,12 @@ use Throwable;
class SQLiteDatabaseManager implements TenantDatabaseManager
{
public static string|null $path = null;
public function createDatabase(TenantWithDatabase $tenant): bool
{
try {
return file_put_contents(database_path($tenant->database()->getName()), '');
return (bool) file_put_contents($this->getPath($tenant->database()->getName()), '');
} catch (Throwable) {
return false;
}
@ -22,7 +24,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public function deleteDatabase(TenantWithDatabase $tenant): bool
{
try {
return unlink(database_path($tenant->database()->getName()));
return unlink($this->getPath($tenant->database()->getName()));
} catch (Throwable) {
return false;
}
@ -30,7 +32,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public function databaseExists(string $name): bool
{
return file_exists(database_path($name));
return file_exists($this->getPath($name));
}
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
@ -44,4 +46,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
{
//
}
public function getPath(string $name): string
{
return static::$path ? static::$path . '/' . $name : database_path($name);
}
}

View file

@ -225,7 +225,21 @@ test('tenant database can be created on a foreign server', function () {
});
test('path used by sqlite manager can be customized', function () {
pest()->markTestIncomplete();
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
// Set custom path for SQLite file
SQLiteDatabaseManager::$path = $customPath = storage_path();
$name = Str::random(8). '.sqlite';
Tenant::create([
'tenancy_db_name' => $name,
'tenancy_db_connection' => 'sqlite',
]);
expect(file_exists( $customPath . '/' . $name))->toBeTrue();
unlink($customPath . '/' . $name); // clean up
});
// Datasets