mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 19:54:04 +00:00
Merge branch 'master' of https://github.com/archtechx/tenancy into stein-j-readied-tenant
This commit is contained in:
commit
e172408576
28 changed files with 409 additions and 117 deletions
|
|
@ -8,12 +8,16 @@ use Stancl\JobPipeline\JobPipeline;
|
|||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Stancl\Tenancy\Jobs\DeleteDomains;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Jobs\DeleteDatabase;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||
use Stancl\Tenancy\Events\DeletingTenant;
|
||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
|
|
@ -162,7 +166,9 @@ test('install command works', function () {
|
|||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
pest()->artisan('tenancy:install');
|
||||
pest()->artisan('tenancy:install')
|
||||
->expectsConfirmation('Would you like to show your support by starring the project on GitHub?', 'no')
|
||||
->assertExitCode(0);
|
||||
expect(base_path('routes/tenant.php'))->toBeFile();
|
||||
expect(base_path('config/tenancy.php'))->toBeFile();
|
||||
expect(app_path('Providers/TenancyServiceProvider.php'))->toBeFile();
|
||||
|
|
@ -205,7 +211,8 @@ test('run command with array of tenants works', function () {
|
|||
test('link command works', function() {
|
||||
$tenantId1 = Tenant::create()->getTenantKey();
|
||||
$tenantId2 = Tenant::create()->getTenantKey();
|
||||
pest()->artisan('tenants:link');
|
||||
pest()->artisan('tenants:link')
|
||||
->assertExitCode(0);
|
||||
|
||||
$this->assertDirectoryExists(storage_path("tenant-$tenantId1/app/public"));
|
||||
$this->assertEquals(storage_path("tenant-$tenantId1/app/public/"), readlink(public_path("public-$tenantId1")));
|
||||
|
|
@ -215,7 +222,7 @@ test('link command works', function() {
|
|||
|
||||
pest()->artisan('tenants:link', [
|
||||
'--remove' => true,
|
||||
]);
|
||||
])->assertExitCode(0);
|
||||
|
||||
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId1"));
|
||||
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId2"));
|
||||
|
|
@ -247,8 +254,9 @@ test('run command works when sub command asks questions and accepts arguments',
|
|||
|
||||
pest()->artisan("tenants:run --tenants=$id 'user:addwithname Abrar' ")
|
||||
->expectsQuestion('What is your email?', 'email@localhost')
|
||||
->expectsOutput("Tenant: $id")
|
||||
->expectsOutput("User created: Abrar(email@localhost)");
|
||||
->expectsOutputToContain("Tenant: $id.")
|
||||
->expectsOutput("User created: Abrar(email@localhost)")
|
||||
->assertExitCode(0);
|
||||
|
||||
// Assert we are in central context
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
|
|
@ -262,6 +270,47 @@ test('run command works when sub command asks questions and accepts arguments',
|
|||
expect($user->email)->toBe('email@localhost');
|
||||
});
|
||||
|
||||
test('migrate fresh command only deletes tenant databases if drop_tenant_databases_on_migrate_fresh is true', function (bool $dropTenantDBsOnMigrateFresh) {
|
||||
Event::listen(DeletingTenant::class,
|
||||
JobPipeline::make([DeleteDomains::class])->send(function (DeletingTenant $event) {
|
||||
return $event->tenant;
|
||||
})->shouldBeQueued(false)->toListener()
|
||||
);
|
||||
|
||||
Event::listen(
|
||||
TenantDeleted::class,
|
||||
JobPipeline::make([DeleteDatabase::class])->send(function (TenantDeleted $event) {
|
||||
return $event->tenant;
|
||||
})->shouldBeQueued(false)->toListener()
|
||||
);
|
||||
|
||||
config(['tenancy.database.drop_tenant_databases_on_migrate_fresh' => $dropTenantDBsOnMigrateFresh]);
|
||||
$shouldHaveDBAfterMigrateFresh = ! $dropTenantDBsOnMigrateFresh;
|
||||
|
||||
/** @var Tenant[] $tenants */
|
||||
$tenants = [
|
||||
Tenant::create(),
|
||||
Tenant::create(),
|
||||
Tenant::create(),
|
||||
];
|
||||
|
||||
$tenantHasDatabase = fn (Tenant $tenant) => $tenant->database()->manager()->databaseExists($tenant->database()->getName());
|
||||
|
||||
foreach ($tenants as $tenant) {
|
||||
expect($tenantHasDatabase($tenant))->toBeTrue();
|
||||
}
|
||||
|
||||
pest()->artisan('migrate:fresh', [
|
||||
'--force' => true,
|
||||
'--path' => __DIR__ . '/../assets/migrations',
|
||||
'--realpath' => true,
|
||||
]);
|
||||
|
||||
foreach ($tenants as $tenant) {
|
||||
expect($tenantHasDatabase($tenant))->toBe($shouldHaveDBAfterMigrateFresh);
|
||||
}
|
||||
})->with([true, false]);
|
||||
|
||||
// todo@tests
|
||||
function runCommandWorks(): void
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
|
||||
|
|
@ -32,6 +33,20 @@ test('tenants can be in maintenance mode', function () {
|
|||
pest()->get('http://acme.localhost/foo')->assertStatus(200);
|
||||
});
|
||||
|
||||
test('maintenance mode events are fired', function () {
|
||||
$tenant = MaintenanceTenant::create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$tenant->putDownForMaintenance();
|
||||
|
||||
Event::assertDispatched(\Stancl\Tenancy\Events\TenantMaintenanceModeEnabled::class);
|
||||
|
||||
$tenant->bringUpFromMaintenance();
|
||||
|
||||
Event::assertDispatched(\Stancl\Tenancy\Events\TenantMaintenanceModeDisabled::class);
|
||||
});
|
||||
|
||||
test('tenants can be put into maintenance mode using artisan commands', function() {
|
||||
Route::get('/foo', function () {
|
||||
return 'bar';
|
||||
|
|
@ -44,12 +59,18 @@ test('tenants can be put into maintenance mode using artisan commands', function
|
|||
|
||||
pest()->get('http://acme.localhost/foo')->assertStatus(200);
|
||||
|
||||
pest()->artisan('tenants:down')
|
||||
->expectsOutputToContain('Tenants are now in maintenance mode.')
|
||||
->assertExitCode(0);
|
||||
|
||||
Artisan::call('tenants:down');
|
||||
|
||||
tenancy()->end(); // End tenancy before making a request
|
||||
pest()->get('http://acme.localhost/foo')->assertStatus(503);
|
||||
|
||||
Artisan::call('tenants:up');
|
||||
pest()->artisan('tenants:up')
|
||||
->expectsOutputToContain('Tenants are now out of maintenance mode.')
|
||||
->assertExitCode(0);
|
||||
|
||||
tenancy()->end(); // End tenancy before making a request
|
||||
pest()->get('http://acme.localhost/foo')->assertStatus(200);
|
||||
|
|
|
|||
|
|
@ -20,19 +20,18 @@ beforeEach(function () {
|
|||
|
||||
afterEach(function () {
|
||||
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
||||
InitializeTenancyByRequestData::$cookie = 'X-Tenant';
|
||||
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
||||
});
|
||||
|
||||
test('header identification works', function () {
|
||||
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
||||
$tenant = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('test', [
|
||||
'X-Tenant' => $tenant->id,
|
||||
])
|
||||
->withHeader('X-Tenant', $tenant->id)
|
||||
->get('test')
|
||||
->assertSee($tenant->id);
|
||||
});
|
||||
|
||||
|
|
@ -40,10 +39,20 @@ test('query parameter identification works', function () {
|
|||
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
||||
|
||||
$tenant = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->get('test?tenant=' . $tenant->id)
|
||||
->assertSee($tenant->id);
|
||||
});
|
||||
|
||||
test('cookie identification works', function () {
|
||||
InitializeTenancyByRequestData::$cookie = 'X-Tenant';
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$this
|
||||
->withoutExceptionHandling()
|
||||
->withUnencryptedCookie('X-Tenant', $tenant->id)
|
||||
->get('test',)
|
||||
->assertSee($tenant->id);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ use Stancl\Tenancy\Jobs\CreateDatabase;
|
|||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Stancl\Tenancy\UUIDGenerator;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
|
||||
test('created event is dispatched', function () {
|
||||
Event::fake([TenantCreated::class]);
|
||||
|
|
@ -141,6 +142,31 @@ test('a command can be run on a collection of tenants', function () {
|
|||
expect(Tenant::find('t2')->foo)->toBe('xyz');
|
||||
});
|
||||
|
||||
test('the current method returns the currently initialized tenant', function() {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
expect(Tenant::current())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('the current method returns null if there is no currently initialized tenant', function() {
|
||||
tenancy()->end();
|
||||
|
||||
expect(Tenant::current())->toBeNull();
|
||||
});
|
||||
|
||||
test('currentOrFail method returns the currently initialized tenant', function() {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
expect(Tenant::currentOrFail())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('currentOrFail method throws an exception if there is no currently initialized tenant', function() {
|
||||
tenancy()->end();
|
||||
|
||||
expect(fn() => Tenant::currentOrFail())->toThrow(TenancyNotInitializedException::class);
|
||||
});
|
||||
|
||||
|
||||
class MyTenant extends Tenant
|
||||
{
|
||||
protected $table = 'tenants';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue