mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 11:14:04 +00:00
Enable caching in initialization mw
This commit is contained in:
parent
d7c04d304d
commit
800e8d5a56
7 changed files with 116 additions and 3 deletions
|
|
@ -4,12 +4,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||
use Stancl\Tenancy\Resolvers\CachedTenantResolver;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class CachedTenantResolverTest extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
InitializeTenancyByDomain::$shouldCache = false;
|
||||
InitializeTenancyByRequestData::$shouldCache = false;
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenants_can_be_resolved_using_the_cached_resolver()
|
||||
{
|
||||
|
|
@ -38,4 +49,43 @@ class CachedTenantResolverTest extends TestCase
|
|||
|
||||
$this->assertTrue($tenant->is(app(CachedTenantResolver::class)->resolve(DomainTenantResolver::class, ['acme'])));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function caching_can_be_configured_selectively_on_initialization_middleware()
|
||||
{
|
||||
InitializeTenancyByDomain::$shouldCache = true;
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'acme.localhost',
|
||||
]);
|
||||
|
||||
Route::get('/foo', function () {
|
||||
return 'bar';
|
||||
})->middleware(InitializeTenancyByDomain::class);
|
||||
|
||||
// create cache
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSee('bar');
|
||||
|
||||
$this->mock(CachedTenantResolver::class, function ($mock) {
|
||||
return $mock->shouldReceive('resolve')->once(); // only once
|
||||
});
|
||||
|
||||
// use cache
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSee('bar');
|
||||
|
||||
Route::get('/abc', function () {
|
||||
return 'xyz';
|
||||
})->middleware(InitializeTenancyByRequestData::class);
|
||||
|
||||
$this->get('/abc?tenant=acme')
|
||||
->assertSee('xyz');
|
||||
|
||||
$this->get('/abc?tenant=acme')
|
||||
->assertSee('xyz');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue