mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
Fix Redis scan for predis, make phpunit use multiple configs (e.g. different Redis drivers)
This commit is contained in:
parent
fe6ce82045
commit
0dc8c80a02
6 changed files with 37 additions and 13 deletions
|
|
@ -82,17 +82,21 @@ class RedisStorageDriver implements StorageDriver
|
|||
}, $uuids);
|
||||
|
||||
if (! $hashes) {
|
||||
// Apparently, the PREFIX is applied to all functions except scan().
|
||||
// Therefore, if the `tenancy` Redis connection has a prefix set
|
||||
// (and PhpRedis is used), prepend the prefix to the search.
|
||||
$redis_prefix = '';
|
||||
// Prefix is applied to all functions except scan().
|
||||
// This code applies the correct prefix manually.
|
||||
$redis_prefix = config('database.redis.options.prefix');
|
||||
|
||||
if (config('database.redis.client') === 'phpredis') {
|
||||
$redis_prefix = $this->redis->getOption($this->redis->client()::OPT_PREFIX);
|
||||
$redis_prefix = $this->redis->getOption($this->redis->client()::OPT_PREFIX) ?? $redis_prefix;
|
||||
$all_keys = $this->redis->scan(null, $redis_prefix . 'tenants:*');
|
||||
} else {
|
||||
$all_keys = $this->redis->scan(null, 'MATCH', $redis_prefix . 'tenants:*')[1];
|
||||
}
|
||||
$hashes = array_map(function ($hash) use ($redis_prefix) {
|
||||
// Left strip $redis_prefix from $hash
|
||||
return substr($hash, strlen($redis_prefix));
|
||||
}, $this->redis->scan(null, $redis_prefix.'tenants:*'));
|
||||
|
||||
$hashes = array_map(function ($key) use ($redis_prefix) {
|
||||
// Left strip $redis_prefix from $key
|
||||
return substr($key, strlen($redis_prefix));
|
||||
}, $all_keys);
|
||||
}
|
||||
|
||||
return array_map(function ($tenant) {
|
||||
|
|
|
|||
1
test
1
test
|
|
@ -22,4 +22,5 @@ for variant in variants:
|
|||
system('docker-compose exec test vendor/bin/phpunit --configuration "%s" --coverage-php %s %s'
|
||||
% (filename_base + '.xml', 'coverage/' + filename_base + '.cov', ' '.join(other)))
|
||||
|
||||
# todo delete folder contents first?
|
||||
system("docker-compose exec test vendor/bin/phpcov merge --clover clover.xml coverage/")
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ class BootstrapsTenancyTest extends TestCase
|
|||
/** @test */
|
||||
public function redis_is_prefixed()
|
||||
{
|
||||
if (! config('tenancy.redis.tenancy')) {
|
||||
$this->markTestSkipped('Redis tenancy disabled.');
|
||||
}
|
||||
|
||||
$this->initTenancy();
|
||||
foreach (config('tenancy.redis.prefixed_connections', ['default']) as $connection) {
|
||||
$prefix = config('tenancy.redis.prefix_base') . tenant('uuid');
|
||||
|
|
@ -35,6 +39,7 @@ class BootstrapsTenancyTest extends TestCase
|
|||
/** @test */
|
||||
public function predis_is_supported()
|
||||
{
|
||||
// No setDriver() before that version.
|
||||
if (app()->version() < 'v5.8.27') {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,10 @@ class DataSeparationTest extends TestCase
|
|||
/** @test */
|
||||
public function redis_is_separated()
|
||||
{
|
||||
if (! config('tenancy.redis.tenancy')) {
|
||||
$this->markTestSkipped('Redis tenancy disabled.');
|
||||
}
|
||||
|
||||
tenancy()->create('tenant1.localhost');
|
||||
tenancy()->create('tenant2.localhost');
|
||||
|
||||
|
|
|
|||
|
|
@ -186,4 +186,12 @@ class TenantManagerTest extends TestCase
|
|||
$tenant = tenant()->create('foo.localhost');
|
||||
$this->assertSame([$tenant], tenancy()->all()->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all_returns_a_list_of_all_tenants()
|
||||
{
|
||||
$tenant1 = tenant()->create('foo.localhost');
|
||||
$tenant2 = tenant()->create('bar.localhost');
|
||||
$this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenant()->all()->toArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'database.redis.client' => 'phpredis',
|
||||
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.options.prefix' => 'foo',
|
||||
'database.redis.tenancy' => [
|
||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
||||
|
|
@ -70,6 +71,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
// Use the #14 Redis database unless specified otherwise.
|
||||
// Make sure you don't store anything in this db!
|
||||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
||||
'prefix' => 'abc', // todo unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation
|
||||
],
|
||||
'tenancy.database' => [
|
||||
'based_on' => 'sqlite',
|
||||
|
|
@ -92,14 +94,14 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
switch ((string) env('STANCL_TENANCY_TEST_VARIANT', '1')) {
|
||||
case '2':
|
||||
$app['config']->set([
|
||||
'tenancy.redis.tenancy' => true,
|
||||
'database.redis.client' => 'phpredis',
|
||||
'tenancy.redis.tenancy' => false,
|
||||
'database.redis.client' => 'predis',
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
$app['config']->set([
|
||||
'tenancy.redis.tenancy' => false,
|
||||
'database.redis.client' => 'predis',
|
||||
'tenancy.redis.tenancy' => true,
|
||||
'database.redis.client' => 'phpredis',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue