Skip to content

Commit 2b45a01

Browse files
committed
Move triggerRestoredEvent(), test restoring Syncables
Update comments in DeleteResourceMapping to explain the detach() behavior. Move triggerRestoredEvent() from SyncMaster to Syncable interface, test restoring tenant resources.
1 parent f53fcb2 commit 2b45a01

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/ResourceSyncing/Listeners/DeleteResourceMapping.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function handle(SyncedResourceDeleted $event): void
3434
// or the central resource was deleted using forceDelete()
3535
if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
3636
Pivot::withoutEvents(function () use ($centralResource, $event) {
37-
// $event->tenant is null when the deleted resource is a SyncMaster - all mappings are deleted in that case
38-
// When $event->tenant is not null (= a Syncable was deleted), only delete the mapping for that tenant
37+
// If detach() is called with null -- if $event->tenant is null -- this means a central resource was deleted and detaches all tenants.
38+
// If detach() is called with a specific tenant, it means the resource was deleted in that tenant, and we only delete that single mapping.
3939
$centralResource->tenants()->detach($event->tenant);
4040
});
4141
}

src/ResourceSyncing/SyncMaster.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ public function getTenantModelName(): string;
2424
public function triggerDetachEvent(TenantWithDatabase&Model $tenant): void;
2525

2626
public function triggerAttachEvent(TenantWithDatabase&Model $tenant): void;
27-
28-
public function triggerRestoredEvent(): void;
2927
}

src/ResourceSyncing/Syncable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function triggerSyncEvent(): void;
1818

1919
public function triggerDeleteEvent(bool $forceDelete = false): void;
2020

21+
public function triggerRestoredEvent(): void;
22+
2123
/**
2224
* 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).
2325
*

tests/ResourceSyncingTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,16 @@
11021102
CentralUserWithSoftDeletes::withTrashed()->first()->restore();
11031103

11041104
tenancy()->runForMultiple([$tenant1, $tenant2], function () {
1105-
expect(TenantUserWithSoftDeletes::withTrashed()->first()->trashed())->toBeFalse();
1105+
$tenantResource = TenantUserWithSoftDeletes::withTrashed()->first();
1106+
1107+
expect($tenantResource->trashed())->toBeFalse();
1108+
1109+
$tenantResource->delete();
1110+
1111+
// Restoring a tenant resource works as expected
1112+
$tenantResource->restore();
1113+
1114+
expect($tenantResource->trashed())->toBeFalse();
11061115
});
11071116
});
11081117

0 commit comments

Comments
 (0)