|
5 | 5 | namespace Stancl\Tenancy\Tests;
|
6 | 6 |
|
7 | 7 | use Illuminate\Support\Facades\DB;
|
| 8 | +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; |
8 | 9 | use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
9 | 10 | use Stancl\Tenancy\Tests\Etc\Tenant;
|
10 | 11 |
|
@@ -80,6 +81,33 @@ public function cache_is_invalidated_when_the_tenant_is_updated()
|
80 | 81 | $this->assertNotEmpty(DB::getQueryLog()); // not empty
|
81 | 82 | }
|
82 | 83 |
|
| 84 | + /** @test */ |
| 85 | + public function cache_is_invalidated_when_the_tenant_is_deleted() |
| 86 | + { |
| 87 | + $tenant = Tenant::create(); |
| 88 | + $tenant->createDomain([ |
| 89 | + 'domain' => 'acme', |
| 90 | + ]); |
| 91 | + |
| 92 | + DB::enableQueryLog(); |
| 93 | + |
| 94 | + DomainTenantResolver::$shouldCache = true; |
| 95 | + |
| 96 | + $this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); |
| 97 | + DB::flushQueryLog(); |
| 98 | + $this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); |
| 99 | + $this->assertEmpty(DB::getQueryLog()); // empty |
| 100 | + |
| 101 | + $tenant->delete(); |
| 102 | + DB::flushQueryLog(); |
| 103 | + |
| 104 | + $this->assertThrows(function () { |
| 105 | + app(DomainTenantResolver::class)->resolve('acme'); |
| 106 | + }, TenantCouldNotBeIdentifiedOnDomainException::class); |
| 107 | + |
| 108 | + $this->assertNotEmpty(DB::getQueryLog()); // not empty - cache cleared so the DB was queried |
| 109 | + } |
| 110 | + |
83 | 111 | /** @test */
|
84 | 112 | public function cache_is_invalidated_when_a_tenants_domain_is_changed()
|
85 | 113 | {
|
@@ -109,4 +137,31 @@ public function cache_is_invalidated_when_a_tenants_domain_is_changed()
|
109 | 137 | $this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('bar')));
|
110 | 138 | $this->assertNotEmpty(DB::getQueryLog()); // not empty
|
111 | 139 | }
|
| 140 | + |
| 141 | + /** @test */ |
| 142 | + public function cache_is_invalidated_when_a_tenants_domain_is_deleted() |
| 143 | + { |
| 144 | + $tenant = Tenant::create(); |
| 145 | + $tenant->createDomain([ |
| 146 | + 'domain' => 'acme', |
| 147 | + ]); |
| 148 | + |
| 149 | + DB::enableQueryLog(); |
| 150 | + |
| 151 | + DomainTenantResolver::$shouldCache = true; |
| 152 | + |
| 153 | + $this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); |
| 154 | + DB::flushQueryLog(); |
| 155 | + $this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); |
| 156 | + $this->assertEmpty(DB::getQueryLog()); // empty |
| 157 | + |
| 158 | + $tenant->domains->first()->delete(); |
| 159 | + DB::flushQueryLog(); |
| 160 | + |
| 161 | + $this->assertThrows(function () { |
| 162 | + app(DomainTenantResolver::class)->resolve('acme'); |
| 163 | + }, TenantCouldNotBeIdentifiedOnDomainException::class); |
| 164 | + |
| 165 | + $this->assertNotEmpty(DB::getQueryLog()); // not empty - cache cleared so the DB was queried |
| 166 | + } |
112 | 167 | }
|
0 commit comments