mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
[4.x] Finish incomplete and missing tests (#947)
* complete test sqlite manager customize path * complete test seed command works * complete uniqe exists test * Update SingleDatabaseTenancyTest.php * refactor the ternary into if condition * custom path * simplify if condition * random dir name * Update SingleDatabaseTenancyTest.php * Update CommandsTest.php * prefix random DB name with custom_ Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
ab5fa7a247
commit
8e3b74f9d1
4 changed files with 60 additions and 12 deletions
|
|
@ -10,10 +10,15 @@ use Throwable;
|
||||||
|
|
||||||
class SQLiteDatabaseManager implements TenantDatabaseManager
|
class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* SQLite Database path without ending slash.
|
||||||
|
*/
|
||||||
|
public static string|null $path = null;
|
||||||
|
|
||||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return file_put_contents(database_path($tenant->database()->getName()), '');
|
return (bool) file_put_contents($this->getPath($tenant->database()->getName()), '');
|
||||||
} catch (Throwable) {
|
} catch (Throwable) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +27,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return unlink(database_path($tenant->database()->getName()));
|
return unlink($this->getPath($tenant->database()->getName()));
|
||||||
} catch (Throwable) {
|
} catch (Throwable) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +35,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
|
|
||||||
public function databaseExists(string $name): bool
|
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
|
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||||
|
|
@ -44,4 +49,13 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPath(string $name): string
|
||||||
|
{
|
||||||
|
if (static::$path) {
|
||||||
|
return static::$path . DIRECTORY_SEPARATOR . $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return database_path($name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\DatabaseManager;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
|
@ -16,6 +17,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
use Stancl\Tenancy\Tests\Etc\User;
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
|
@ -41,9 +43,9 @@ afterEach(function () {
|
||||||
test('migrate command doesnt change the db connection', function () {
|
test('migrate command doesnt change the db connection', function () {
|
||||||
expect(Schema::hasTable('users'))->toBeFalse();
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
|
|
||||||
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
$old_connection_name = app(DatabaseManager::class)->connection()->getName();
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
$new_connection_name = app(DatabaseManager::class)->connection()->getName();
|
||||||
|
|
||||||
expect(Schema::hasTable('users'))->toBeFalse();
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
expect($new_connection_name)->toEqual($old_connection_name);
|
expect($new_connection_name)->toEqual($old_connection_name);
|
||||||
|
|
@ -116,8 +118,22 @@ test('rollback command works', function () {
|
||||||
expect(Schema::hasTable('users'))->toBeFalse();
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Incomplete test
|
test('seed command works', function (){
|
||||||
test('seed command works');
|
$tenant = Tenant::create();
|
||||||
|
Artisan::call('tenants:migrate');
|
||||||
|
|
||||||
|
$tenant->run(function (){
|
||||||
|
expect(DB::table('users')->count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
Artisan::call('tenants:seed', ['--class' => TestSeeder::class]);
|
||||||
|
|
||||||
|
$tenant->run(function (){
|
||||||
|
$user = DB::table('users');
|
||||||
|
expect($user->count())->toBe(1)
|
||||||
|
->and($user->first()->email)->toBe('seeded@user');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('database connection is switched to default', function () {
|
test('database connection is switched to default', function () {
|
||||||
databaseConnectionSwitchedToDefault();
|
databaseConnectionSwitchedToDefault();
|
||||||
|
|
|
||||||
|
|
@ -207,13 +207,13 @@ test('the model returned by the tenant helper has unique and exists validation r
|
||||||
$uniqueFails = Validator::make($data, [
|
$uniqueFails = Validator::make($data, [
|
||||||
'slug' => 'unique:posts',
|
'slug' => 'unique:posts',
|
||||||
])->fails();
|
])->fails();
|
||||||
$existsFails = Validator::make($data, [
|
$existsPass = Validator::make($data, [
|
||||||
'slug' => 'exists:posts',
|
'slug' => 'exists:posts',
|
||||||
])->fails();
|
])->passes();
|
||||||
|
|
||||||
// Assert that 'unique' and 'exists' aren't scoped by default
|
// Assert that 'unique' and 'exists' aren't scoped by default
|
||||||
// pest()->assertFalse($uniqueFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
expect($uniqueFails)->toBeTrue(); // Expect unique rule failed to pass because slug 'foo' already exists
|
||||||
// pest()->assertTrue($existsFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
expect($existsPass)->toBeTrue(); // Expect exists rule pass because slug 'foo' exists
|
||||||
|
|
||||||
$uniqueFails = Validator::make($data, [
|
$uniqueFails = Validator::make($data, [
|
||||||
'slug' => tenant()->unique('posts'),
|
'slug' => tenant()->unique('posts'),
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,25 @@ test('tenant database can be created on a foreign server', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('path used by sqlite manager can be customized', 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 = database_path('custom_' . Str::random(8));
|
||||||
|
|
||||||
|
if (! is_dir($customPath)) {
|
||||||
|
// Create custom directory
|
||||||
|
mkdir($customPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
$name = Str::random(8). '.sqlite';
|
||||||
|
Tenant::create([
|
||||||
|
'tenancy_db_name' => $name,
|
||||||
|
'tenancy_db_connection' => 'sqlite',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(file_exists( $customPath . '/' . $name))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Datasets
|
// Datasets
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue