mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
Merge branch 'master' of github.com:archtechx/tenancy
This commit is contained in:
commit
8a00a105d0
4 changed files with 33 additions and 18 deletions
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
|
|
@ -103,3 +103,12 @@ jobs:
|
||||||
author_email: "phpcsfixer@example.com"
|
author_email: "phpcsfixer@example.com"
|
||||||
message: Fix code style (php-cs-fixer)
|
message: Fix code style (php-cs-fixer)
|
||||||
|
|
||||||
|
phpstan:
|
||||||
|
name: Static analysis (PHPStan)
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Install composer dependencies
|
||||||
|
run: composer install
|
||||||
|
- name: Run phpstan
|
||||||
|
run: vendor/bin/phpstan analyse
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,8 @@
|
||||||
"docker-rebuild": "PHP_VERSION=8.1 docker-compose up -d --no-deps --build",
|
"docker-rebuild": "PHP_VERSION=8.1 docker-compose up -d --no-deps --build",
|
||||||
"docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml",
|
"docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml",
|
||||||
"coverage": "open coverage/phpunit/html/index.html",
|
"coverage": "open coverage/phpunit/html/index.html",
|
||||||
"phpstan": "vendor/bin/phpstan --pro",
|
"phpstan": "vendor/bin/phpstan",
|
||||||
|
"phpstan-pro": "vendor/bin/phpstan --pro",
|
||||||
"cs": "php-cs-fixer fix --config=.php-cs-fixer.php",
|
"cs": "php-cs-fixer fix --config=.php-cs-fixer.php",
|
||||||
"test": "PHP_VERSION=8.1 ./test --no-coverage",
|
"test": "PHP_VERSION=8.1 ./test --no-coverage",
|
||||||
"test-full": "PHP_VERSION=8.1 ./test"
|
"test-full": "PHP_VERSION=8.1 ./test"
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,10 @@ class PathTenantResolver extends Contracts\CachedTenantResolver
|
||||||
/** @var Route $route */
|
/** @var Route $route */
|
||||||
$route = $args[0];
|
$route = $args[0];
|
||||||
|
|
||||||
if ($id = (string) $route->parameter(static::tenantParameterName())) {
|
/** @var string $id */
|
||||||
|
$id = $route->parameter(static::tenantParameterName());
|
||||||
|
|
||||||
|
if ($id) {
|
||||||
$route->forgetParameter(static::tenantParameterName());
|
$route->forgetParameter(static::tenantParameterName());
|
||||||
|
|
||||||
if ($tenant = tenancy()->find($id)) {
|
if ($tenant = tenancy()->find($id)) {
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ test('only the synced columns are updated in the central db', function () {
|
||||||
// This tests attribute list on the central side, and default values on the tenant side
|
// This tests attribute list on the central side, and default values on the tenant side
|
||||||
// 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 attributes and resource model provides default values', function () {
|
test('sync resource creation works when central model provides attributes and tenant model provides default values', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
addExtraColumnToCentralDB();
|
addExtraColumnToCentralDB();
|
||||||
|
|
@ -145,14 +145,14 @@ test('sync resource creation works when central model provides attributes and re
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
expect(ResourceUserProvidingDefaultValues::all())->toHaveCount(0);
|
expect(TenantUserProvidingDefaultValues::all())->toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
|
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
$resourceUser = ResourceUserProvidingDefaultValues::all();
|
$resourceUser = TenantUserProvidingDefaultValues::all();
|
||||||
expect($resourceUser)->toHaveCount(1);
|
expect($resourceUser)->toHaveCount(1);
|
||||||
expect($resourceUser->first()->global_id)->toBe('acme');
|
expect($resourceUser->first()->global_id)->toBe('acme');
|
||||||
expect($resourceUser->first()->email)->toBe('john@localhost');
|
expect($resourceUser->first()->email)->toBe('john@localhost');
|
||||||
|
|
@ -163,7 +163,7 @@ test('sync resource creation works when central model provides attributes and re
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
// When resource model provides the list of default values, central model will be created from the provided list of default values
|
// When resource model provides the list of default values, central model will be created from the provided list of default values
|
||||||
ResourceUserProvidingDefaultValues::create([
|
TenantUserProvidingDefaultValues::create([
|
||||||
'global_id' => 'asdf',
|
'global_id' => 'asdf',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
'email' => 'john@localhost',
|
'email' => 'john@localhost',
|
||||||
|
|
@ -186,7 +186,7 @@ test('sync resource creation works when central model provides attributes and re
|
||||||
// This tests default values on the central side, and attribute list on the tenant side
|
// This tests default values on the central side, and attribute list on the tenant side
|
||||||
// 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 default values and resource model provides attributes', function () {
|
test('sync resource creation works when central model provides default values and tenant model provides attributes', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
addExtraColumnToCentralDB();
|
addExtraColumnToCentralDB();
|
||||||
|
|
@ -201,7 +201,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
expect(ResourceUserProvidingDefaultValues::all())->toHaveCount(0);
|
expect(TenantUserProvidingDefaultValues::all())->toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides the list of default values, resource model will be created from the provided list of default values
|
// When central model provides the list of default values, resource model will be created from the provided list of default values
|
||||||
|
|
@ -209,7 +209,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
// Assert resource user was created using the list of default values
|
// Assert resource user was created using the list of default values
|
||||||
$resourceUser = ResourceUserProvidingDefaultValues::first();
|
$resourceUser = TenantUserProvidingDefaultValues::first();
|
||||||
expect($resourceUser)->not()->toBeNull();
|
expect($resourceUser)->not()->toBeNull();
|
||||||
expect($resourceUser->global_id)->toBe('acme');
|
expect($resourceUser->global_id)->toBe('acme');
|
||||||
expect($resourceUser->email)->toBe('default@localhost');
|
expect($resourceUser->email)->toBe('default@localhost');
|
||||||
|
|
@ -220,7 +220,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
// When resource model provides the list of attributes, central model will be created from the provided list of attributes' values
|
// When resource model provides the list of attributes, central model will be created from the provided list of attributes' values
|
||||||
ResourceUserProvidingAttributeNames::create([
|
TenantUserProvidingAttributeNames::create([
|
||||||
'global_id' => 'asdf',
|
'global_id' => 'asdf',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
'email' => 'john@localhost',
|
'email' => 'john@localhost',
|
||||||
|
|
@ -241,7 +241,7 @@ test('sync resource creation works when central model provides default values an
|
||||||
// This tests mixed attribute list/defaults on the central side, and no specified attributes on the tenant side
|
// This tests mixed attribute list/defaults on the central side, and no specified attributes on the tenant side
|
||||||
// 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 tenant model provides nothing', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
$centralUser = CentralUserProvidingMixture::create([
|
$centralUser = CentralUserProvidingMixture::create([
|
||||||
|
|
@ -299,7 +299,7 @@ test('sync resource creation works when central model provides mixture and resou
|
||||||
// This tests no specified attributes on the central side, and mixed attribute list/defaults on the tenant side
|
// This tests no specified attributes on the central side, and mixed attribute list/defaults on the tenant side
|
||||||
// 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 tenant model provides mixture', function () {
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
|
|
@ -311,7 +311,7 @@ test('sync resource creation works when central model provides nothing and resou
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant1->run(function () {
|
$tenant1->run(function () {
|
||||||
expect(ResourceUserProvidingMixture::all())->toHaveCount(0);
|
expect(TenantUserProvidingMixture::all())->toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||||
|
|
@ -319,7 +319,7 @@ test('sync resource creation works when central model provides nothing and resou
|
||||||
|
|
||||||
expect($centralUser->getSyncedCreationAttributes())->toBeNull();
|
expect($centralUser->getSyncedCreationAttributes())->toBeNull();
|
||||||
$tenant1->run(function () use ($centralUser) {
|
$tenant1->run(function () use ($centralUser) {
|
||||||
$resourceUser = ResourceUserProvidingMixture::first();
|
$resourceUser = TenantUserProvidingMixture::first();
|
||||||
expect($resourceUser)->not()->toBeNull();
|
expect($resourceUser)->not()->toBeNull();
|
||||||
$resourceUser = $resourceUser->toArray();
|
$resourceUser = $resourceUser->toArray();
|
||||||
$centralUser = $centralUser->withoutRelations()->toArray();
|
$centralUser = $centralUser->withoutRelations()->toArray();
|
||||||
|
|
@ -332,7 +332,7 @@ test('sync resource creation works when central model provides nothing and resou
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
// When resource model provides the list of a mixture (attributes and default values), central model will be created from the provided list of mixture (attributes and default values)
|
// When resource model provides the list of a mixture (attributes and default values), central model will be created from the provided list of mixture (attributes and default values)
|
||||||
ResourceUserProvidingMixture::create([
|
TenantUserProvidingMixture::create([
|
||||||
'global_id' => 'absd',
|
'global_id' => 'absd',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
'email' => 'john@localhost',
|
'email' => 'john@localhost',
|
||||||
|
|
@ -829,6 +829,7 @@ function migrateUsersTableForTenants(): void
|
||||||
])->assertExitCode(0);
|
])->assertExitCode(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tenant model used for resource syncing setup
|
||||||
class ResourceTenant extends Tenant
|
class ResourceTenant extends Tenant
|
||||||
{
|
{
|
||||||
public function users()
|
public function users()
|
||||||
|
|
@ -885,6 +886,7 @@ class CentralUser extends Model implements SyncMaster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tenant users
|
||||||
class ResourceUser extends Model implements Syncable
|
class ResourceUser extends Model implements Syncable
|
||||||
{
|
{
|
||||||
use ResourceSyncing;
|
use ResourceSyncing;
|
||||||
|
|
@ -922,7 +924,7 @@ class ResourceUser extends Model implements Syncable
|
||||||
}
|
}
|
||||||
|
|
||||||
// override method in ResourceUser class to return default attribute values
|
// override method in ResourceUser class to return default attribute values
|
||||||
class ResourceUserProvidingDefaultValues extends ResourceUser
|
class TenantUserProvidingDefaultValues extends ResourceUser
|
||||||
{
|
{
|
||||||
public function getSyncedCreationAttributes(): array
|
public function getSyncedCreationAttributes(): array
|
||||||
{
|
{
|
||||||
|
|
@ -939,7 +941,7 @@ class ResourceUserProvidingDefaultValues extends ResourceUser
|
||||||
}
|
}
|
||||||
|
|
||||||
// override method in ResourceUser class to return attribute names
|
// override method in ResourceUser class to return attribute names
|
||||||
class ResourceUserProvidingAttributeNames extends ResourceUser
|
class TenantUserProvidingAttributeNames extends ResourceUser
|
||||||
{
|
{
|
||||||
public function getSyncedCreationAttributes(): array
|
public function getSyncedCreationAttributes(): array
|
||||||
{
|
{
|
||||||
|
|
@ -1004,7 +1006,7 @@ class CentralUserProvidingMixture extends CentralUser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ResourceUserProvidingMixture extends ResourceUser
|
class TenantUserProvidingMixture extends ResourceUser
|
||||||
{
|
{
|
||||||
public function getSyncedCreationAttributes(): array
|
public function getSyncedCreationAttributes(): array
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue