1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 07:34:03 +00:00

use deleting instead of deleted event to trigger cache invalidation

This commit is contained in:
Johannes 2025-03-13 13:12:10 +01:00
parent 1bcc215966
commit e4ebccfa5f
3 changed files with 14 additions and 4 deletions

View file

@ -19,7 +19,7 @@ trait InvalidatesResolverCache
public static function bootInvalidatesResolverCache()
{
static::saved(fn(Tenant $tenant) => static::invalidateTenantCache($tenant));
static::deleted(fn(Tenant $tenant) => static::invalidateTenantCache($tenant));
static::deleting(fn(Tenant $tenant) => static::invalidateTenantCache($tenant));
}
private static function invalidateTenantCache(Tenant $tenant): void

View file

@ -22,7 +22,7 @@ trait InvalidatesTenantsResolverCache
public static function bootInvalidatesTenantsResolverCache()
{
static::saved(fn(Model $model) => static::invalidateTenantCache($model));
static::deleted(fn(Model $model) => static::invalidateTenantCache($model));
static::deleting(fn(Model $model) => static::invalidateTenantCache($model));
}
private static function invalidateTenantCache(Model $model): void

View file

@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Exceptions;
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use Stancl\Tenancy\Tests\Etc\Tenant;
@ -113,6 +115,8 @@ class CachedTenantResolverTest extends TestCase
/** @test */
public function cache_is_invalidated_when_a_tenants_domain_is_deleted()
{
Exceptions::fake();
$tenant = Tenant::create();
$tenant->createDomain([
'domain' => 'acme',
@ -129,14 +133,18 @@ class CachedTenantResolverTest extends TestCase
$tenant->domains()->first()->delete();
$this->expectException(TenantCouldNotBeIdentifiedException::class); // tenant not found
DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
$this->assertEmpty(DB::getQueryLog()); // not empty
$this->assertNotEmpty(DB::getQueryLog()); // not emptyt empty
}
/** @test */
public function cache_is_invalidated_when_a_tenant_is_deleted()
{
Exceptions::fake();
$tenant = Tenant::create();
$tenant->createDomain([
'domain' => 'acme',
@ -153,8 +161,10 @@ class CachedTenantResolverTest extends TestCase
$tenant->delete();
$this->expectException(TenantCouldNotBeIdentifiedException::class); // tenant not found
DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
$this->assertEmpty(DB::getQueryLog()); // not empty
$this->assertNotEmpty(DB::getQueryLog()); // not empty
}
}