From 5ce0d946eee2ec33eb9c5202d66648be38a34c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 2 Feb 2019 12:08:08 +0100 Subject: [PATCH] Add ServerManagerTest, fix a lot of bugs --- src/ServerManager.php | 2 +- src/TenancyServiceProvider.php | 1 + src/TenantManager.php | 4 +-- src/Traits/BootstrapsTenancy.php | 6 +++-- tests/ServerManagerTest.php | 42 ++++++++++++++++++++++++++++++++ tests/TestCase.php | 32 +++++++++++++++++++++++- 6 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/ServerManagerTest.php diff --git a/src/ServerManager.php b/src/ServerManager.php index e74cc191..251d36c2 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -18,7 +18,7 @@ class ServerManager 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() diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index d7654c6a..f613184b 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Stancl\Tenancy\Commands\TenantList; use Stancl\Tenancy\Interfaces\StorageDriver; +use Stancl\Tenancy\Interfaces\ServerConfigManager; use Stancl\Tenancy\StorageDrivers\RedisStorageDriver; class TenancyServiceProvider extends ServiceProvider diff --git a/src/TenantManager.php b/src/TenantManager.php index c239a230..57c66c04 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -2,9 +2,9 @@ namespace Stancl\Tenancy; -use Stancl\Tenancy\BootstrapsTenancy; use Illuminate\Support\Facades\Redis; use Stancl\Tenancy\Interfaces\StorageDriver; +use Stancl\Tenancy\Traits\BootstrapsTenancy; class TenantManager { @@ -77,7 +77,7 @@ class TenantManager 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)); return $tenant; diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index 1775fc15..a791854a 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -1,6 +1,9 @@ tenant['uuid']; $client = Redis::connection($connection)->client(); diff --git a/tests/ServerManagerTest.php b/tests/ServerManagerTest.php new file mode 100644 index 00000000..269fc9ba --- /dev/null +++ b/tests/ServerManagerTest.php @@ -0,0 +1,42 @@ +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()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index a33d3ad7..e411b3a1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,8 @@ namespace Stancl\Tenancy\Tests; +use Illuminate\Support\Facades\Redis; + class TestCase extends \Orchestra\Testbench\TestCase { /** @@ -13,7 +15,35 @@ class TestCase extends \Orchestra\Testbench\TestCase { 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)