Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Cache prefix mode for separating tenant caches #1014

Merged
merged 163 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 160 commits
Commits
Show all changes
163 commits
Select commit Hold shift + click to select a range
898d6c5
cache prefix
abrardev99 Nov 17, 2022
d6da626
prefix cache bootstrapper and tests
abrardev99 Nov 21, 2022
c59a514
remove comment
abrardev99 Nov 22, 2022
90c54ac
DI app
abrardev99 Nov 22, 2022
e5658fa
cache prefix base from config
abrardev99 Nov 22, 2022
c4f9324
Create PrefixCacheBootstrapperTest.php
abrardev99 Nov 22, 2022
e970c16
remove `null` check
abrardev99 Nov 22, 2022
c5f7de4
fix phpstan error
abrardev99 Nov 22, 2022
2302571
Update PrefixCacheTenancyBootstrapper.php
abrardev99 Nov 22, 2022
2c0a8f4
Update PrefixCacheBootstrapperTest.php
abrardev99 Nov 23, 2022
1e44ef2
add comments
abrardev99 Nov 24, 2022
e550c7d
Update PrefixCacheTenancyBootstrapper.php
abrardev99 Nov 24, 2022
3cff2e3
Update PrefixCacheBootstrapperTest.php
abrardev99 Nov 24, 2022
41a6f5b
Update config.php
abrardev99 Nov 28, 2022
eef58bd
test names grammar
abrardev99 Nov 28, 2022
d6b4d9b
user `getTenantKey` method
abrardev99 Nov 28, 2022
b920a29
assert tenants' data is accessible using the prefix from the central …
abrardev99 Nov 29, 2022
2901d01
remove unused line
abrardev99 Nov 29, 2022
387a10b
use proper DI
abrardev99 Nov 30, 2022
169a7f7
build prefix using original prefix
abrardev99 Nov 30, 2022
3407faf
fix prefix test according to prefix changes
abrardev99 Nov 30, 2022
70ee83b
fix test
abrardev99 Nov 30, 2022
222686e
CacheManager dependency injection test
abrardev99 Dec 1, 2022
6c955ac
CacheService class as singleton
abrardev99 Dec 1, 2022
2f91309
introduce second tenant in test
abrardev99 Dec 1, 2022
19255fd
use Repository in service class DI
abrardev99 Dec 7, 2022
974414a
Update CacheAction.php
abrardev99 Dec 7, 2022
4c33df1
Rename CacheAction to CacheService
lukinovec Dec 7, 2022
825a565
Update prefix bootstrapper and test (`setStore()` in CacheManager and…
lukinovec Dec 8, 2022
50d4613
Add macro
lukinovec Dec 9, 2022
012a39f
Fix code style (php-cs-fixer)
Dec 9, 2022
dc9c8fc
Simplify cache store refreshing
lukinovec Dec 12, 2022
9ec0b6d
Make Tenancy override CacheManager
lukinovec Dec 12, 2022
d364568
Update CacheManager, add refreshStore()
lukinovec Dec 12, 2022
3cadefd
Fix code style (php-cs-fixer)
Dec 12, 2022
b41d8d1
Uncomment cache tagging
lukinovec Dec 12, 2022
d3c7fef
Revert condition in CacheManager to avoid excessive nesting
lukinovec Dec 12, 2022
35a3412
Move `Cache::macro()` to a slightly more appropriate place
lukinovec Dec 12, 2022
bc70a2f
Fix code style (php-cs-fixer)
Dec 12, 2022
5c6226b
Use better class for the macro
lukinovec Dec 12, 2022
87efdd2
Toggle cache tags
lukinovec Dec 13, 2022
7092f4c
Make CacheManager::$addTags default to `true`
lukinovec Dec 14, 2022
fa00fc7
Add changes to PR to Laravel
lukinovec Dec 16, 2022
c9ba569
Fix code style (php-cs-fixer)
Dec 16, 2022
8a54e19
Revert changes, add comment
lukinovec Dec 22, 2022
a59d5a1
Add test
lukinovec Jan 4, 2023
1c21c66
Make `$cache` non-nullable
lukinovec Jan 5, 2023
4b0df42
Add and test `nonTenantCacheDrivers`
lukinovec Jan 5, 2023
86c388a
Add nonTenantCacheDrivers check
lukinovec Jan 5, 2023
ffefe1b
Test that the prefix stays the same
lukinovec Jan 5, 2023
f8f0e1e
Merge branch 'master' into cache-prefix
lukinovec Jan 5, 2023
30d73d2
Change nonTenantCacheDrivers to tenantCacheStores
lukinovec Jan 5, 2023
42381d0
Remove redundant CacheManager extend() call
lukinovec Jan 5, 2023
7f10b9a
Make 'redis' the only tenant cache store in beforeEach, test that ten…
lukinovec Jan 5, 2023
b38aee0
Remove unused import, change word
lukinovec Jan 5, 2023
5ea5299
Make CacheService a singleton in a test
lukinovec Jan 5, 2023
507df55
Update test name
lukinovec Jan 5, 2023
52db2a0
Remove group('prefix')
lukinovec Jan 5, 2023
d945d1f
Rename CacheManagerService
lukinovec Jan 6, 2023
1104aba
Improve specific cache store in a service test
lukinovec Jan 6, 2023
caa0c87
Improve comment
lukinovec Jan 6, 2023
8deeef2
Improve tests
lukinovec Jan 6, 2023
62a2ed1
Use my Laravel fork
lukinovec Jan 31, 2023
a4aa499
Fix code style (php-cs-fixer)
Jan 31, 2023
ba8cfdd
Merge branch 'master' into cache-prefix
lukinovec Jan 31, 2023
9b36bf2
Downgrade Laravel
lukinovec Jan 31, 2023
bd6332f
Upgrade Laravel
lukinovec Feb 1, 2023
cc9928b
Hint Repository implementation instead of contract
lukinovec Feb 1, 2023
9517031
Fix types
lukinovec Feb 1, 2023
9ed7308
Fix code style (php-cs-fixer)
Feb 1, 2023
c3e3a33
Fix test
lukinovec Feb 1, 2023
93aaba3
Merge branch 'master' of github.com:archtechx/tenancy into cache-prefix
lukinovec Feb 2, 2023
e11a71e
Use Laravel fork in ci.yml
lukinovec Feb 2, 2023
249fc54
Merge branch 'master' into cache-prefix
lukinovec Feb 21, 2023
f0407e7
Merge branch 'master' into cache-prefix
stancl Feb 21, 2023
a48e447
use dev-master before our changes are released in L10
stancl Feb 21, 2023
587396b
remove laravel fork from repositories
stancl Feb 21, 2023
c254f62
use 10.x-dev instead of master
stancl Feb 21, 2023
b883d52
Merge branch 'master' into cache-prefix
stancl Feb 21, 2023
d655d3a
remove L9 support
stancl Feb 21, 2023
20f9b7f
merge
stancl Feb 21, 2023
4acf7e2
10.x-dev (fix conflict resolution)
stancl Feb 21, 2023
5f3079d
use the laravel version from the ci matrix for the phpstan job as well
stancl Feb 21, 2023
76dae7b
Revert "use the laravel version from the ci matrix for the phpstan jo…
stancl Feb 21, 2023
9e8af40
Test that non-default stores get prefixed too
lukinovec Feb 21, 2023
a152d31
Use new Laravel release, remove L9 support
lukinovec Feb 23, 2023
cf799d3
Complete L9 support removal
lukinovec Feb 23, 2023
a7e4956
Specify 10.1.1 as the minimal Laravel version in ci.yml
lukinovec Feb 23, 2023
4638392
Use 10.x-dev
lukinovec Feb 23, 2023
b715571
Prefix all cache stores specified in `$tenantCacheStores`
lukinovec Feb 24, 2023
5a20a0e
Update Laravel
lukinovec Mar 14, 2023
660b772
Use tmpfs in docker-compose
lukinovec Mar 14, 2023
8348389
Add customizing cache store prefixes
lukinovec Mar 14, 2023
7b91e3f
Test cache prefixing customization
lukinovec Mar 14, 2023
9388459
Fix code style (php-cs-fixer)
Mar 14, 2023
a1dc8e9
Merge branch 'master' of github.com:archtechx/tenancy into cache-prefix
lukinovec Mar 20, 2023
e9aa5be
Update ci.yml
stancl Mar 20, 2023
c45f900
Delete tmpfs from docker-compose.yml (there were no benefits)
lukinovec Mar 30, 2023
8ea3cc9
Use default prefix generator inline, delete the 'default' key logic
lukinovec Mar 31, 2023
1f1c7cc
Fix original prefix logic
lukinovec Mar 31, 2023
7bbd535
Update tests
lukinovec Mar 31, 2023
840cd83
Delete CacheTenancyBootstrapper
lukinovec Mar 31, 2023
6666993
Reset static properties in afterEach
lukinovec Mar 31, 2023
5849afa
Use `$this->config` instead of `config()`
lukinovec Apr 7, 2023
7ebfc37
Disable cache tagging by default, add CacheTagBootstrapper
lukinovec Apr 11, 2023
52dabed
Fix code style (php-cs-fixer)
Apr 11, 2023
70051e7
Rename bootstrapper
lukinovec Apr 11, 2023
2d0ee2c
Improve CacheManager
lukinovec Apr 12, 2023
940fb17
Move logic from separate method to __call
lukinovec Apr 12, 2023
39e62c7
Make original prefixes customizable
lukinovec Apr 12, 2023
e894c78
Add info in comment
lukinovec Apr 12, 2023
9d9e2b3
Add defaultPrefix property
lukinovec Apr 12, 2023
35b0dea
Use `$this->app` instead of `app()`
lukinovec Apr 12, 2023
4ab692b
Rename bootstrapper
lukinovec Apr 13, 2023
c9ba00c
Fix code style (php-cs-fixer)
Apr 13, 2023
8ac4d87
Use a single original prefix
lukinovec Apr 13, 2023
740d4e7
Update prefix generator logic + tests
lukinovec Apr 13, 2023
6513029
Correct `$addTags` reset in a test
lukinovec Apr 13, 2023
9e15110
Update cache tests so that both prefixing and tagging is covered
lukinovec Apr 13, 2023
dd57d9b
Simplify cache tests
lukinovec Apr 13, 2023
3481e24
Delete afterEach
lukinovec Apr 13, 2023
0c1912b
Small testing improvements
lukinovec Apr 14, 2023
13b85a2
Set `cache.default` in beforeEach
lukinovec Apr 17, 2023
52d9643
Update cache prefixing and tests
lukinovec Apr 17, 2023
859b2c5
Add assertion
lukinovec Apr 17, 2023
d84878b
Refactor assertion
lukinovec Apr 17, 2023
ce43384
Refactor assertions
lukinovec Apr 17, 2023
bd41ae4
Delete TTL from cache put calls
lukinovec Apr 17, 2023
90684a7
Add re-initialization cache assertion
lukinovec Apr 17, 2023
9925e49
Assert that cache is null from the beginning
lukinovec Apr 17, 2023
4c9cb7c
Merge the tenantCacheStores tests
lukinovec Apr 17, 2023
b4f4fd1
Fix formatting
lukinovec Apr 17, 2023
2309b83
Improve test name
lukinovec Apr 17, 2023
ca907e6
Improve tests
lukinovec Apr 17, 2023
ab92f85
Add cache manager config key
lukinovec Apr 17, 2023
f12f119
Fix code style (php-cs-fixer)
Apr 17, 2023
835b169
Update defaulting test
lukinovec Apr 18, 2023
effb6a8
Add todo
lukinovec Apr 18, 2023
e4a4940
Update comments
lukinovec Apr 18, 2023
46f94d1
Extract duplicate assertions into a closure
lukinovec Apr 18, 2023
c0e2926
Update comment
lukinovec Apr 18, 2023
f7c8f22
Add assertions + comment
lukinovec Apr 18, 2023
4b862e2
Delete redundant config put calls
lukinovec Apr 18, 2023
b5abe6e
Use `tenancy.cache.manager` config instead of `Stancl\Tenancy\CacheMa…
lukinovec Apr 18, 2023
cb07a47
Change setting to assertion, add comment
lukinovec Apr 18, 2023
4d38718
Inline variable & config key assignment
lukinovec Apr 18, 2023
1d52096
Delete `cache.default` assertion
lukinovec Apr 19, 2023
2171ca9
Override cache manager only in CacheTagsBootstrapper
lukinovec Apr 19, 2023
cc45f80
Fix code style (php-cs-fixer)
Apr 19, 2023
52d10d3
Prefix both drivers by default, add assertions for the second driver …
lukinovec Apr 19, 2023
53b2181
Clean up global state (static properties) in before/afterEach
lukinovec Apr 19, 2023
0920c6a
Add docblock to tags bootstrapper
lukinovec Apr 19, 2023
62b596f
Delete extra dependency
lukinovec Apr 19, 2023
58e008a
Add `illuminate/support` dependency back
lukinovec Apr 20, 2023
8f5a4e4
Use `$addTags` approach again
lukinovec Apr 20, 2023
ea805fa
Fix code style (php-cs-fixer)
Apr 20, 2023
55cc89e
Revert "Fix code style (php-cs-fixer)"
lukinovec Apr 20, 2023
8178c91
Revert "Use `$addTags` approach again"
lukinovec Apr 20, 2023
30567d9
Add commented CacheTagsBootstrapper with info to the bootstrappers co…
lukinovec Apr 20, 2023
5a158b9
Delete legacy bootstrapper from the bootstrappers config, add info to…
lukinovec Apr 20, 2023
69d4c32
Delete "?" from `tenant()?->getTenantKey()
lukinovec Apr 24, 2023
4b4d808
call generatePrefix() on $bootstrapper
stancl Apr 24, 2023
07c1d77
misc improvements
stancl Apr 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ jobs:
strategy:
matrix:
include:
- laravel: "^9.0"
php: "8.0"
- laravel: "^10.0"
php: "8.2"

Expand Down
6 changes: 4 additions & 2 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Stancl\Tenancy\CacheManager;
use Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Resolvers;

Expand Down Expand Up @@ -98,10 +99,10 @@
*/
'bootstrappers' => [
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
stancl marked this conversation as resolved.
Show resolved Hide resolved
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
Expand Down Expand Up @@ -178,7 +179,7 @@
],

/**
* Cache tenancy config. Used by CacheTenancyBootstrapper.
* Cache tenancy config. Used by the custom CacheManager and the PrefixCacheTenancyBootstrapper.
*
* This works for all Cache facade calls, cache() helper
* calls and direct calls to injected cache stores.
Expand All @@ -190,6 +191,7 @@
*/
'cache' => [
'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call.
'prefix_base' => 'tenant_', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
],

/**
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^8.2",
"ext-json": "*",
"illuminate/support": "^9.38|^10.0",
"illuminate/support": "^10.1",
"facade/ignition-contracts": "^1.0.2",
"spatie/ignition": "^1.4",
"ramsey/uuid": "^4.7.3",
Expand All @@ -26,8 +26,8 @@
"spatie/invade": "^1.1"
},
"require-dev": {
"laravel/framework": "^9.38|^10.0",
"orchestra/testbench": "^7.0|^8.0",
"laravel/framework": "^10.1",
"orchestra/testbench": "^8.0",
"league/flysystem-aws-s3-v3": "^3.12.2",
"doctrine/dbal": "^3.6.0",
"spatie/valuestore": "^1.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\CacheManager as TenantCacheManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

class CacheTenancyBootstrapper implements TenancyBootstrapper
/**
* todo name.
*
* Separate tenant cache using tagging.
* This is the legacy approach. Some things, like dependency injection, won't work properly with this bootstrapper.
* PrefixCacheTenancyBootstrapper is the recommended bootstrapper for cache separation.
*/
class CacheTagsBootstrapper implements TenancyBootstrapper
{
protected ?CacheManager $originalCache = null;
public static string $cacheManagerWithTags = \Stancl\Tenancy\CacheManager::class;

public function __construct(
protected Application $app
Expand All @@ -24,9 +31,9 @@ public function bootstrap(Tenant $tenant): void
{
$this->resetFacadeCache();

$this->originalCache = $this->originalCache ?? $this->app['cache'];
$this->originalCache ??= $this->app['cache'];
$this->app->extend('cache', function () {
return new TenantCacheManager($this->app);
return new static::$cacheManagerWithTags($this->app);
});
}

Expand Down
84 changes: 84 additions & 0 deletions src/Bootstrappers/PrefixCacheTenancyBootstrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Bootstrappers;

use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{
protected string|null $originalPrefix = null;
public static array $tenantCacheStores = []; // E.g. ['redis']
public static Closure|null $prefixGenerator = null;

public function __construct(
protected ConfigRepository $config,
protected CacheManager $cacheManager,
) {
}

public function bootstrap(Tenant $tenant): void
{
$this->originalPrefix = $this->config->get('cache.prefix');

$prefix = $this->generatePrefix($tenant);

foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $prefix);

// Now that the store uses the passed prefix
// Set the configured prefix back to the default one
$this->config->set('cache.prefix', $this->originalPrefix);
}
}

public function revert(): void
{
foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $this->originalPrefix);
}
}

protected function setCachePrefix(string $driver, string|null $prefix): void
{
$this->config->set('cache.prefix', $prefix);

// Refresh driver's store to make the driver use the current prefix
$this->refreshStore($driver);

// It is needed when a call to the facade has been made before bootstrapping tenancy
// The facade has its own cache, separate from the container
Cache::clearResolvedInstances();
}

public function generatePrefix(Tenant $tenant): string
{
$defaultPrefix = $this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();

return static::$prefixGenerator ? (static::$prefixGenerator)($tenant) : $defaultPrefix;
}

public static function generatePrefixUsing(Closure $prefixGenerator): void
{
static::$prefixGenerator = $prefixGenerator;
}

/**
* Refresh cache driver's store.
*/
protected function refreshStore(string $driver): void
{
$newStore = $this->cacheManager->resolve($driver)->getStore();
/** @var Repository $repository */
$repository = $this->cacheManager->driver($driver);

$repository->setStore($newStore);
}
}
2 changes: 1 addition & 1 deletion src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CacheManager extends BaseCacheManager
*/
public function __call($method, $parameters)
{
$tags = [config('tenancy.cache.tag_base') . tenant()->getTenantKey()];
$tags = [config('tenancy.cache.tag_base') . tenant()?->getTenantKey()];
lukinovec marked this conversation as resolved.
Show resolved Hide resolved

if ($method === 'tags') {
$count = count($parameters);
Expand Down
30 changes: 23 additions & 7 deletions tests/BootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;

beforeEach(function () {
$this->mockConsoleOutput = false;

config(['cache.default' => $cacheDriver = 'redis']);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
// Reset static properties of classes used in this test file to their default values
BroadcastTenancyBootstrapper::$credentialsMap = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
UrlTenancyBootstrapper::$rootUrlOverride = null;

Event::listen(
TenantCreated::class,
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
Expand All @@ -51,6 +59,14 @@
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});

afterEach(function () {
// Reset static properties of classes used in this test file to their default values
UrlTenancyBootstrapper::$rootUrlOverride = null;
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
BroadcastTenancyBootstrapper::$credentialsMap = [];
});

test('database data is separated', function () {
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
Expand Down Expand Up @@ -82,12 +98,9 @@
expect(DB::table('users')->first()->name)->toBe('Foo');
});

test('cache data is separated', function () {
test('cache data is separated', function (string $bootstrapper) {
config([
'tenancy.bootstrappers' => [
CacheTenancyBootstrapper::class,
],
'cache.default' => 'redis',
'tenancy.bootstrappers' => [$bootstrapper],
]);

$tenant1 = Tenant::create();
Expand Down Expand Up @@ -121,7 +134,10 @@

// Asset central is still the same
expect(Cache::get('foo'))->toBe('central');
});
})->with([
CacheTagsBootstrapper::class,
PrefixCacheTenancyBootstrapper::class,
]);

test('redis data is separated', function () {
config(['tenancy.bootstrappers' => [
Expand Down
7 changes: 6 additions & 1 deletion tests/BroadcastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;

beforeEach(function() {
beforeEach(function () {
withTenantDatabases();
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});

afterEach(function () {
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
});

test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
config(['broadcasting.default' => 'null']);
TenancyBroadcastManager::$tenantBroadcasters[] = 'null';
Expand Down
12 changes: 5 additions & 7 deletions tests/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

declare(strict_types=1);

use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;

beforeEach(function () {
config(['tenancy.bootstrappers' => [
CacheTenancyBootstrapper::class,
]]);
config(['tenancy.bootstrappers' => [CacheTagsBootstrapper::class]]);

Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
});
Expand Down Expand Up @@ -60,7 +58,7 @@
$tenant2 = Tenant::create();
tenancy()->initialize($tenant2);

pest()->assertNotSame('bar', cache()->get('foo'));
expect(cache('foo'))->not()->toBe('bar');

cache()->put('foo', 'xyz', 1);
expect(cache()->get('foo'))->toBe('xyz');
Expand All @@ -76,7 +74,7 @@
$tenant2 = Tenant::create();
tenancy()->initialize($tenant2);

pest()->assertNotSame('bar', cache('foo'));
expect(cache('foo'))->not()->toBe('bar');

cache(['foo' => 'xyz'], 1);
expect(cache('foo'))->toBe('xyz');
Expand Down
9 changes: 6 additions & 3 deletions tests/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Stancl\Tenancy\Resolvers\DomainTenantResolver;

beforeEach(function () {
InitializeTenancyByDomain::$onFail = null;

Route::group([
'middleware' => InitializeTenancyByDomain::class,
], function () {
Expand All @@ -23,6 +25,10 @@
config(['tenancy.models.tenant' => DomainTenant::class]);
});

afterEach(function () {
InitializeTenancyByDomain::$onFail = null;
});

test('tenant can be identified using hostname', function () {
$tenant = DomainTenant::create();

Expand Down Expand Up @@ -89,9 +95,6 @@
});

test('throw correct exception when onFail is null and universal routes are enabled', function () {
// un-define onFail logic
InitializeTenancyByDomain::$onFail = null;

// Enable UniversalRoute feature
Route::middlewareGroup('universal', []);

Expand Down
24 changes: 24 additions & 0 deletions tests/Etc/CacheService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Tests\Etc;

use Illuminate\Cache\Repository;

class CacheService
{
public function __construct(
protected Repository $cache
) {
}

public function handle(): void
{
if (tenancy()->initialized) {
$this->cache->put('key', tenant()->getTenantKey());
} else {
$this->cache->put('key', 'central-value');
}
}
}
Loading