mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 15:54:03 +00:00
Merge branch 'master' into resource-syncing-improvements
# Conflicts: # tests/ResourceSyncingTest.php
This commit is contained in:
commit
8d92f8643f
6 changed files with 82 additions and 3 deletions
|
|
@ -10,6 +10,8 @@ Run `composer docker-up` to start the containers. Then run `composer test` to ru
|
|||
|
||||
If you need to pass additional flags to phpunit, use `./test --foo` instead of `composer test --foo`. Composer scripts unfortunately don't pass CLI arguments.
|
||||
|
||||
If you want to run a specific test (or test file), you can also use `./t 'name of the test'`. This is equivalent to `./test --no-coverage --filter 'name of the test'`.
|
||||
|
||||
When you're done testing, run `composer docker-down` to shut down the containers.
|
||||
|
||||
### Docker on M1
|
||||
|
|
|
|||
|
|
@ -18,4 +18,6 @@ interface Syncable
|
|||
|
||||
/** Get the attributes used for creating the *other* model (i.e. tenant if this is the central one, and central if this is the tenant one). */
|
||||
public function getSyncedCreationAttributes(): array|null; // todo come up with a better name
|
||||
|
||||
public function shouldSync(): bool;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,9 @@ trait ResourceSyncing
|
|||
public static function bootResourceSyncing(): void
|
||||
{
|
||||
static::saved(function (Syncable $model) {
|
||||
/** @var ResourceSyncing $model */
|
||||
$model->triggerSyncEvent();
|
||||
if ($model->shouldSync()) {
|
||||
$model->triggerSyncEvent();
|
||||
}
|
||||
});
|
||||
|
||||
static::creating(function (self $model) {
|
||||
|
|
@ -37,4 +38,9 @@ trait ResourceSyncing
|
|||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function shouldSync(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class TenantPivot extends Pivot
|
|||
static::saved(function (self $pivot) {
|
||||
$parent = $pivot->pivotParent;
|
||||
|
||||
if ($parent instanceof Syncable) {
|
||||
if ($parent instanceof Syncable && $parent->shouldSync()) {
|
||||
$parent->triggerSyncEvent();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
3
t
Executable file
3
t
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
docker-compose exec -T test vendor/bin/pest --no-coverage --filter "$@"
|
||||
|
|
@ -763,6 +763,43 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
|||
expect(ResourceUser::first()->role)->toBe('commenter');
|
||||
}
|
||||
|
||||
test('resources are synced only when sync is enabled', function (bool $enabled) {
|
||||
app()->instance('_tenancy_test_shouldSync', $enabled);
|
||||
|
||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
TenantUserWithConditionalSync::create([
|
||||
'global_id' => 'absd',
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(CentralUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
|
||||
expect(CentralUserWithConditionalSync::whereGlobalId('absd')->exists())->toBe($enabled);
|
||||
|
||||
$centralUser = CentralUserWithConditionalSync::create([
|
||||
'global_id' => 'acme',
|
||||
'name' => 'John Doe',
|
||||
'email' => 'john@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$centralUser->tenants()->attach('t2');
|
||||
|
||||
$tenant2->run(function () use ($enabled) {
|
||||
expect(TenantUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
|
||||
expect(TenantUserWithConditionalSync::whereGlobalId('acme')->exists())->toBe($enabled);
|
||||
});
|
||||
})->with([[true], [false]]);
|
||||
|
||||
/**
|
||||
* Create two tenants and run migrations for those tenants.
|
||||
*/
|
||||
|
|
@ -967,3 +1004,32 @@ class CentralUserProvidingMixture extends CentralUser
|
|||
];
|
||||
}
|
||||
}
|
||||
|
||||
class ResourceUserProvidingMixture extends ResourceUser
|
||||
{
|
||||
public function getSyncedCreationAttributes(): array
|
||||
{
|
||||
return [
|
||||
'name',
|
||||
'email',
|
||||
'role' => 'admin',
|
||||
'password' => 'secret',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class CentralUserWithConditionalSync extends CentralUser
|
||||
{
|
||||
public function shouldSync(): bool
|
||||
{
|
||||
return app('_tenancy_test_shouldSync');
|
||||
}
|
||||
}
|
||||
|
||||
class TenantUserWithConditionalSync extends ResourceUser
|
||||
{
|
||||
public function shouldSync(): bool
|
||||
{
|
||||
return app('_tenancy_test_shouldSync');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue