1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 22:04:03 +00:00

Add ServerManagerTest, fix a lot of bugs

This commit is contained in:
Samuel Štancl 2019-02-02 12:08:08 +01:00
parent 35eab00742
commit 5ce0d946ee
6 changed files with 81 additions and 6 deletions

View file

@ -18,7 +18,7 @@ class ServerManager
return config('tenancy.server.file.path'); return config('tenancy.server.file.path');
} }
return config('tenancy.server.file.path.prefix') . $this->tenantManager('uuid') . config('tenancy.server.file.path.suffix'); return config('tenancy.server.file.path.prefix') . $this->tenantManager->tenant['uuid'] . config('tenancy.server.file.path.suffix');
} }
public function create() public function create()

View file

@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Commands\TenantList; use Stancl\Tenancy\Commands\TenantList;
use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Interfaces\ServerConfigManager;
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver; use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider

View file

@ -2,9 +2,9 @@
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Stancl\Tenancy\BootstrapsTenancy;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Traits\BootstrapsTenancy;
class TenantManager class TenantManager
{ {
@ -77,7 +77,7 @@ class TenantManager
throw new \Exception("Domain $domain is already occupied by tenant $id."); throw new \Exception("Domain $domain is already occupied by tenant $id.");
} }
$tenant = $this->storage->createTenant($domain, \Uuid::generate(1, $domain)); $tenant = $this->storage->createTenant($domain, \Webpatser\Uuid\Uuid::generate(1, $domain));
$this->database->create($this->getDatabaseName($tenant)); $this->database->create($this->getDatabaseName($tenant));
return $tenant; return $tenant;

View file

@ -1,6 +1,9 @@
<?php <?php
namespace Stancl\Tenancy; namespace Stancl\Tenancy\Traits;
use Stancl\Tenancy\CacheManager;
use Illuminate\Support\Facades\Redis;
trait BootstrapsTenancy trait BootstrapsTenancy
{ {
@ -21,7 +24,6 @@ trait BootstrapsTenancy
public function setPhpRedisPrefix($connections = ['default']) public function setPhpRedisPrefix($connections = ['default'])
{ {
return;
foreach ($connections as $connection) { foreach ($connections as $connection) {
$prefix = config('tenancy.redis.prefix_base') . $this->tenant['uuid']; $prefix = config('tenancy.redis.prefix_base') . $this->tenant['uuid'];
$client = Redis::connection($connection)->client(); $client = Redis::connection($connection)->client();

View file

@ -0,0 +1,42 @@
<?php
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\ServerManager;
class ServerManagerTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->serverManager = app(ServerManager::class);
}
/** @test */
public function getConfigFilePath_works_when_single_file_mode_is_used()
{
config([
'tenancy.server.file.single' => true,
'tenancy.server.file.path' => '/foo/bar',
]);
$this->assertSame('/foo/bar', $this->serverManager->getConfigFilePath());
}
/** @test */
public function getConfigFilePath_works_when_multiple_files_mode_is_used()
{
config([
'tenancy.server.file.single' => false,
'tenancy.server.file.path' => [
'prefix' => '/etc/foo',
'suffix' => 'bar'
],
]);
$uuid = tenant('uuid');
$this->assertSame("/etc/foo{$uuid}bar", $this->serverManager->getConfigFilePath());
}
}

View file

@ -2,6 +2,8 @@
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Redis;
class TestCase extends \Orchestra\Testbench\TestCase class TestCase extends \Orchestra\Testbench\TestCase
{ {
/** /**
@ -13,7 +15,35 @@ class TestCase extends \Orchestra\Testbench\TestCase
{ {
parent::setUp(); parent::setUp();
// Redis::connection('tenancy')->flushdb();
tenant()->create('phpunit.localhost');
tenancy()->init('phpunit.localhost');
}
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.redis.client', 'phpredis');
$app['config']->set('database.redis.tenancy', [
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
'port' => env('TENANCY_TEST_REDIS_PORT', 6379),
// 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),
]);
$app['config']->set('tenancy.database', [
'based_on' => 'sqlite',
'prefix' => 'tenant',
'suffix' => '.sqlite',
]);
} }
protected function getPackageProviders($app) protected function getPackageProviders($app)