mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 14:54:03 +00:00
refactor helpers for creating tenants
This commit is contained in:
parent
2bb26c9f28
commit
8e664b1ba1
1 changed files with 25 additions and 25 deletions
|
|
@ -84,7 +84,7 @@ test('only the synced columns are updated in the central db', function () {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
$tenant = ResourceTenant::create();
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
|
@ -133,6 +133,8 @@ test('only the synced columns are updated in the central db', function () {
|
||||||
test('sync resource creation works when central model provides attributes and resource model provides default values', function () {
|
test('sync resource creation works when central model provides attributes and resource model provides default values', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
|
addExtraColumnToCentralDB();
|
||||||
|
|
||||||
$centralUser = CentralUserProvidingAttributeNames::create([
|
$centralUser = CentralUserProvidingAttributeNames::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -187,6 +189,8 @@ test('sync resource creation works when central model provides attributes and re
|
||||||
test('sync resource creation works when central model provides default values and resource model provides attributes', function () {
|
test('sync resource creation works when central model provides default values and resource model provides attributes', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
|
addExtraColumnToCentralDB();
|
||||||
|
|
||||||
$centralUser = CentralUserProvidingDefaultValues::create([
|
$centralUser = CentralUserProvidingDefaultValues::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -238,9 +242,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
// Those two don't depend on each other, we're just testing having each option on each side
|
// Those two don't depend on each other, we're just testing having each option on each side
|
||||||
// using tests that combine the two, to avoid having an excessively long and complex test suite
|
// using tests that combine the two, to avoid having an excessively long and complex test suite
|
||||||
test('sync resource creation works when central model provides mixture and resource model provides nothing', function () {
|
test('sync resource creation works when central model provides mixture and resource model provides nothing', function () {
|
||||||
[$tenant1, $tenant2] = [ResourceTenant::create(['id' => 't1']), ResourceTenant::create(['id' => 't2'])];
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
migrateTenantsResource();
|
|
||||||
|
|
||||||
$centralUser = CentralUserProvidingMixture::create([
|
$centralUser = CentralUserProvidingMixture::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -298,9 +300,7 @@ test('sync resource creation works when central model provides mixture and resou
|
||||||
// Those two don't depend on each other, we're just testing having each option on each side
|
// Those two don't depend on each other, we're just testing having each option on each side
|
||||||
// using tests that combine the two, to avoid having an excessively long and complex test suite
|
// using tests that combine the two, to avoid having an excessively long and complex test suite
|
||||||
test('sync resource creation works when central model provides nothing and resource model provides mixture', function () {
|
test('sync resource creation works when central model provides nothing and resource model provides mixture', function () {
|
||||||
[$tenant1, $tenant2] = [ResourceTenant::create(['id' => 't1']), ResourceTenant::create(['id' => 't2'])];
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
migrateTenantsResource();
|
|
||||||
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -378,7 +378,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
@ -403,7 +403,7 @@ test('attaching users to tenants does not do anything', function () {
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
@ -438,7 +438,7 @@ test('resources are synced only to workspaces that have the resource', function
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
$centralUser->tenants()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
|
|
@ -476,7 +476,7 @@ test('when a resource exists in other tenant dbs but is created in a tenant db t
|
||||||
$t2 = ResourceTenant::create([
|
$t2 = ResourceTenant::create([
|
||||||
'id' => 't2',
|
'id' => 't2',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -524,7 +524,7 @@ test('the synced columns are updated in other tenant dbs where the resource exis
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -579,7 +579,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
|
|
@ -593,7 +593,7 @@ test('when the resource doesnt exist in the central db non synced columns will b
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
ResourceUser::create([
|
ResourceUser::create([
|
||||||
|
|
@ -615,7 +615,7 @@ test('the listener can be queued', function () {
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
Queue::assertNothingPushed();
|
Queue::assertNothingPushed();
|
||||||
|
|
||||||
|
|
@ -654,7 +654,7 @@ test('an event is fired for all touched resources', function () {
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -735,7 +735,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
$tenant = ResourceTenant::create();
|
||||||
migrateTenantsResource();
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
|
@ -765,26 +765,26 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create two tenants and run migrations for those tenants.
|
* Create two tenants and run migrations for those tenants.
|
||||||
* Also, add an extra column "foo" in central DB.
|
|
||||||
*/
|
*/
|
||||||
function createTenantsAndRunMigrations(): array
|
function createTenantsAndRunMigrations(): array
|
||||||
{
|
{
|
||||||
[$tenant1, $tenant2] = [ResourceTenant::create(['id' => 't1']), ResourceTenant::create(['id' => 't2'])];
|
[$tenant1, $tenant2] = [ResourceTenant::create(['id' => 't1']), ResourceTenant::create(['id' => 't2'])];
|
||||||
|
|
||||||
|
migrateUsersTableForTenants();
|
||||||
|
|
||||||
|
return [$tenant1, $tenant2];
|
||||||
|
}
|
||||||
|
|
||||||
|
function addExtraColumnToCentralDB(): void
|
||||||
|
{
|
||||||
// migrate extra column "foo" in central DB
|
// migrate extra column "foo" in central DB
|
||||||
pest()->artisan('migrate', [
|
pest()->artisan('migrate', [
|
||||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users_extra',
|
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users_extra',
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
])->assertExitCode(0);
|
])->assertExitCode(0);
|
||||||
migrateTenantsResource();
|
|
||||||
|
|
||||||
return [$tenant1, $tenant2];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function migrateUsersTableForTenants(): void
|
||||||
* Run tenant migrations using the tenants:migrate command.
|
|
||||||
*/
|
|
||||||
function migrateTenantsResource(): void
|
|
||||||
{
|
{
|
||||||
pest()->artisan('tenants:migrate', [
|
pest()->artisan('tenants:migrate', [
|
||||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue