1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +00:00

Manual mode improvements (use correct event type, add new listeners) (#1013)

* fix issue and add test

* Update CreateTenantConnection.php

* add purge call

* Update ManualModeTest.php

* use tenant connection and central connection listener

* Update ManualModeTest.php

* fix test

* improvements

* Update ManualModeTest.php

* add comment

* simplify comment

Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
Abrar Ahmad 2022-11-25 07:09:31 +05:00 committed by GitHub
parent ea19117870
commit 73c5655bc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 4 deletions

45
tests/ManualModeTest.php Normal file
View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Event;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Listeners\CreateTenantConnection;
use Stancl\Tenancy\Listeners\UseCentralConnection;
use Stancl\Tenancy\Listeners\UseTenantConnection;
use \Stancl\Tenancy\Tests\Etc\Tenant;
test('manual tenancy initialization works', function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
Event::listen(TenancyInitialized::class, CreateTenantConnection::class);
Event::listen(TenancyInitialized::class, UseTenantConnection::class);
Event::listen(TenancyEnded::class, UseCentralConnection::class);
$tenant = Tenant::create();
expect(app('db')->getDefaultConnection())->toBe('central');
expect(array_keys(app('db')->getConnections()))->toBe(['central', 'tenant_host_connection']);
pest()->assertArrayNotHasKey('tenant', config('database.connections'));
tenancy()->initialize($tenant);
// Trigger creation of the tenant connection
createUsersTable();
expect(app('db')->getDefaultConnection())->toBe('tenant');
expect(array_keys(app('db')->getConnections()))->toBe(['central', 'tenant']);
pest()->assertArrayHasKey('tenant', config('database.connections'));
tenancy()->end();
expect(array_keys(app('db')->getConnections()))->toBe(['central']);
expect(config('database.connections.tenant'))->toBeNull();
expect(app('db')->getDefaultConnection())->toBe(config('tenancy.database.central_connection'));
});