1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 18:04:03 +00:00
tenancy/tests/CacheManagerTest.php
Samuel Štancl 869ac32983
[2.2.0] [WIP] Add functionality (#206)
* TenantDatabaseDoesNotExistException

* Apply fixes from StyleCI

* User post-creation callbacks

* Rename method

* postCreationActions

* pass $tenant as parameter

* pass $tenant to async actions

* WIP findBy()

* findBy\* ForwardsCalls

* Apply fixes from StyleCI

* findBy DB storage driver

* Redis  SD TODO message

* Apply fixes from StyleCI

* Fix chained jobs

* WIP event system

* import str

* instanceof closure check

* findBy instead of find

* Tenant -> Tenants

* dots

* Use DB hooks instead of a SC key

* Don't allow  callables for queue chain

* CanDeleteKeys interface

* Apply fixes from StyleCI

* CanFindByAnyKey interface

* Apply fixes from StyleCI

* Ditch models for custom repositories

* Resolve circular dependency

* Apply fixes from StyleCI

* Fix tests

* Apply fixes from StyleCI

* FutureTest

* Prefix tenant events with 'tenant.'

* Event listener arguments test
2019-10-27 21:10:41 +01:00

119 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant;
class CacheManagerTest extends TestCase
{
public $autoInitTenancy = false;
/** @test */
public function default_tag_is_automatically_applied()
{
$this->initTenancy();
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
}
/** @test */
public function tags_are_merged_when_array_is_passed()
{
$this->initTenancy();
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
}
/** @test */
public function tags_are_merged_when_string_is_passed()
{
$this->initTenancy();
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
}
/** @test */
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
{
$this->initTenancy();
$this->expectException(\Exception::class);
cache()->tags();
}
/** @test */
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
{
$this->initTenancy();
$this->expectException(\Exception::class);
cache()->tags(1, 2);
}
/** @test */
public function tags_separate_cache_well_enough()
{
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache()->put('foo', 'bar', 1);
$this->assertSame('bar', cache()->get('foo'));
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$this->assertNotSame('bar', cache()->get('foo'));
cache()->put('foo', 'xyz', 1);
$this->assertSame('xyz', cache()->get('foo'));
}
/** @test */
public function invoking_the_cache_helper_works()
{
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 1);
$this->assertSame('bar', cache('foo'));
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('bar.localhost');
$this->assertNotSame('bar', cache('foo'));
cache(['foo' => 'xyz'], 1);
$this->assertSame('xyz', cache('foo'));
}
/** @test */
public function cache_is_persisted()
{
Tenant::new()->withDomains(['foo.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));
}
/** @test */
public function cache_is_persisted_when_reidentification_is_used()
{
Tenant::new()->withDomains(['foo.localhost'])->save();
Tenant::new()->withDomains(['bar.localhost'])->save();
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->init('bar.localhost');
tenancy()->endTenancy();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));
}
}