mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +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);
|
}, $uuids);
|
||||||
|
|
||||||
if (! $hashes) {
|
if (! $hashes) {
|
||||||
// Apparently, the PREFIX is applied to all functions except scan().
|
// Prefix is applied to all functions except scan().
|
||||||
// Therefore, if the `tenancy` Redis connection has a prefix set
|
// This code applies the correct prefix manually.
|
||||||
// (and PhpRedis is used), prepend the prefix to the search.
|
$redis_prefix = config('database.redis.options.prefix');
|
||||||
$redis_prefix = '';
|
|
||||||
if (config('database.redis.client') === 'phpredis') {
|
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
|
$hashes = array_map(function ($key) use ($redis_prefix) {
|
||||||
return substr($hash, strlen($redis_prefix));
|
// Left strip $redis_prefix from $key
|
||||||
}, $this->redis->scan(null, $redis_prefix.'tenants:*'));
|
return substr($key, strlen($redis_prefix));
|
||||||
|
}, $all_keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_map(function ($tenant) {
|
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'
|
system('docker-compose exec test vendor/bin/phpunit --configuration "%s" --coverage-php %s %s'
|
||||||
% (filename_base + '.xml', 'coverage/' + filename_base + '.cov', ' '.join(other)))
|
% (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/")
|
system("docker-compose exec test vendor/bin/phpcov merge --clover clover.xml coverage/")
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ class BootstrapsTenancyTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function redis_is_prefixed()
|
public function redis_is_prefixed()
|
||||||
{
|
{
|
||||||
|
if (! config('tenancy.redis.tenancy')) {
|
||||||
|
$this->markTestSkipped('Redis tenancy disabled.');
|
||||||
|
}
|
||||||
|
|
||||||
$this->initTenancy();
|
$this->initTenancy();
|
||||||
foreach (config('tenancy.redis.prefixed_connections', ['default']) as $connection) {
|
foreach (config('tenancy.redis.prefixed_connections', ['default']) as $connection) {
|
||||||
$prefix = config('tenancy.redis.prefix_base') . tenant('uuid');
|
$prefix = config('tenancy.redis.prefix_base') . tenant('uuid');
|
||||||
|
|
@ -35,6 +39,7 @@ class BootstrapsTenancyTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function predis_is_supported()
|
public function predis_is_supported()
|
||||||
{
|
{
|
||||||
|
// No setDriver() before that version.
|
||||||
if (app()->version() < 'v5.8.27') {
|
if (app()->version() < 'v5.8.27') {
|
||||||
$this->markTestSkipped();
|
$this->markTestSkipped();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,10 @@ class DataSeparationTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function redis_is_separated()
|
public function redis_is_separated()
|
||||||
{
|
{
|
||||||
|
if (! config('tenancy.redis.tenancy')) {
|
||||||
|
$this->markTestSkipped('Redis tenancy disabled.');
|
||||||
|
}
|
||||||
|
|
||||||
tenancy()->create('tenant1.localhost');
|
tenancy()->create('tenant1.localhost');
|
||||||
tenancy()->create('tenant2.localhost');
|
tenancy()->create('tenant2.localhost');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,4 +186,12 @@ class TenantManagerTest extends TestCase
|
||||||
$tenant = tenant()->create('foo.localhost');
|
$tenant = tenant()->create('foo.localhost');
|
||||||
$this->assertSame([$tenant], tenancy()->all()->toArray());
|
$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.client' => 'phpredis',
|
||||||
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
'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.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||||
|
'database.redis.options.prefix' => 'foo',
|
||||||
'database.redis.tenancy' => [
|
'database.redis.tenancy' => [
|
||||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
'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.
|
// Use the #14 Redis database unless specified otherwise.
|
||||||
// Make sure you don't store anything in this db!
|
// Make sure you don't store anything in this db!
|
||||||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
'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' => [
|
'tenancy.database' => [
|
||||||
'based_on' => 'sqlite',
|
'based_on' => 'sqlite',
|
||||||
|
|
@ -92,14 +94,14 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
switch ((string) env('STANCL_TENANCY_TEST_VARIANT', '1')) {
|
switch ((string) env('STANCL_TENANCY_TEST_VARIANT', '1')) {
|
||||||
case '2':
|
case '2':
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
'tenancy.redis.tenancy' => true,
|
'tenancy.redis.tenancy' => false,
|
||||||
'database.redis.client' => 'phpredis',
|
'database.redis.client' => 'predis',
|
||||||
]);
|
]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
'tenancy.redis.tenancy' => false,
|
'tenancy.redis.tenancy' => true,
|
||||||
'database.redis.client' => 'predis',
|
'database.redis.client' => 'phpredis',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue