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

Merge branch 'master' of https://github.com/archtechx/tenancy into add-skip-failing-options-to-migrate

This commit is contained in:
lukinovec 2022-09-28 15:07:12 +02:00
commit 5c7a2c26ad
24 changed files with 828 additions and 35 deletions

View file

@ -2,17 +2,19 @@
declare(strict_types=1);
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Tests\Etc\User;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Artisan;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Illuminate\Database\DatabaseManager;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\Etc\TestSeeder;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
@ -28,6 +30,15 @@ beforeEach(function () {
DatabaseTenancyBootstrapper::class,
]]);
config([
'tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
],
'tenancy.filesystem.suffix_base' => 'tenant-',
'tenancy.filesystem.root_override.public' => '%storage_path%/app/public/',
'tenancy.filesystem.url_override.public' => 'public-%tenant_id%'
]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
@ -42,9 +53,9 @@ afterEach(function () {
test('migrate command doesnt change the db connection', function () {
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');
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
$new_connection_name = app(DatabaseManager::class)->connection()->getName();
expect(Schema::hasTable('users'))->toBeFalse();
expect($new_connection_name)->toEqual($old_connection_name);
@ -131,8 +142,22 @@ test('rollback command works', function () {
expect(Schema::hasTable('users'))->toBeFalse();
});
// Incomplete test
test('seed command works');
test('seed command works', function (){
$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 () {
databaseConnectionSwitchedToDefault();
@ -195,6 +220,43 @@ test('run command with array of tenants works', function () {
->expectsOutput('Tenant: ' . $tenantId2);
});
test('link command works', function() {
$tenantId1 = Tenant::create()->getTenantKey();
$tenantId2 = Tenant::create()->getTenantKey();
pest()->artisan('tenants:link');
$this->assertDirectoryExists(storage_path("tenant-$tenantId1/app/public"));
$this->assertEquals(storage_path("tenant-$tenantId1/app/public/"), readlink(public_path("public-$tenantId1")));
$this->assertDirectoryExists(storage_path("tenant-$tenantId2/app/public"));
$this->assertEquals(storage_path("tenant-$tenantId2/app/public/"), readlink(public_path("public-$tenantId2")));
pest()->artisan('tenants:link', [
'--remove' => true,
]);
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId1"));
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId2"));
});
test('link command works with a specified tenant', function() {
$tenantKey = Tenant::create()->getTenantKey();
pest()->artisan('tenants:link', [
'--tenants' => [$tenantKey],
]);
$this->assertDirectoryExists(storage_path("tenant-$tenantKey/app/public"));
$this->assertEquals(storage_path("tenant-$tenantKey/app/public/"), readlink(public_path("public-$tenantKey")));
pest()->artisan('tenants:link', [
'--remove' => true,
'--tenants' => [$tenantKey],
]);
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
});
test('run command works when sub command asks questions and accepts arguments', function () {
$tenant = Tenant::create();
$id = $tenant->getTenantKey();