From 8565cc7accb7c26e0404f90f7ce19a61b18fe315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 17 Sep 2019 19:39:57 +0200 Subject: [PATCH] Get multiple envs (redis/db) to run correctly --- src/Commands/Migrate.php | 2 +- src/Contracts/TenancyBootstrapper.php | 2 +- src/DatabaseManager.php | 2 +- src/Facades/TenantFacade.php | 2 +- src/Features/TelescopeTags.php | 2 +- .../Database/DatabaseStorageDriver.php | 6 +++--- src/StorageDrivers/RedisStorageDriver.php | 12 ++++++------ .../FilesystemTenancyBootstrapper.php | 2 +- .../QueueTenancyBootstrapper.php | 2 +- src/TenancyServiceProvider.php | 4 +++- src/Tenant.php | 2 +- src/TenantManager.php | 4 ++-- tests/BootstrapsTenancyTest.php | 2 +- tests/CommandsTest.php | 4 ++-- tests/QueueTest.php | 2 +- tests/TenantAssetTest.php | 8 ++++---- tests/TenantDatabaseManagerTest.php | 2 -- tests/TenantManagerTest.php | 2 +- tests/TestCase.php | 4 ---- 19 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 9146b9c9..7f68e37b 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -56,7 +56,7 @@ class Migrate extends MigrateCommand // See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection. // Database connections are cached by Illuminate\Database\ConnectionResolver. $this->input->setOption('database', 'tenant'); - tenancy()->initialize($tenant); // todo test that this works with multiple tenants with MySQL + tenancy()->initialize($tenant); // todo2 test that this works with multiple tenants with MySQL // Migrate parent::handle(); diff --git a/src/Contracts/TenancyBootstrapper.php b/src/Contracts/TenancyBootstrapper.php index 0e8c063f..4dfb9536 100644 --- a/src/Contracts/TenancyBootstrapper.php +++ b/src/Contracts/TenancyBootstrapper.php @@ -8,7 +8,7 @@ use Stancl\Tenancy\Tenant; interface TenancyBootstrapper { - public function start(Tenant $tenant); // todo TenantManager instead of Tenant + public function start(Tenant $tenant); // todo2 TenantManager instead of Tenant public function end(); } diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 864152c1..ce8cd99a 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -129,7 +129,7 @@ class DatabaseManager protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager { - // todo this shouldn't have to create a connection + // todo2 this shouldn't have to create a connection $this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName()); $driver = $this->getDriver($tenant->getConnectionName()); diff --git a/src/Facades/TenantFacade.php b/src/Facades/TenantFacade.php index dcde0d92..8e553a8e 100644 --- a/src/Facades/TenantFacade.php +++ b/src/Facades/TenantFacade.php @@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Facades; use Illuminate\Support\Facades\Facade; use Stancl\Tenancy\Tenant as Tenant; -// todo rename to CurrentTenant? +// todo2 rename to CurrentTenant? class TenantFacade extends Facade { protected static function getFacadeAccessor() diff --git a/src/Features/TelescopeTags.php b/src/Features/TelescopeTags.php index 2ac4eed6..19a1131a 100644 --- a/src/Features/TelescopeTags.php +++ b/src/Features/TelescopeTags.php @@ -26,7 +26,7 @@ class TelescopeTags implements Feature if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) { $tags = array_merge($tags, [ 'tenant:' . tenant('id'), - // 'domain:' . tenant('domain'), todo? + // todo2 domain? ]); } diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index 88bdee91..5d3c268c 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -16,7 +16,7 @@ use Stancl\Tenancy\Tenant; class DatabaseStorageDriver implements StorageDriver { - // todo write tests verifying that data is decoded and added to the array + // todo2 write tests verifying that data is decoded and added to the array /** @var Application */ protected $app; @@ -47,7 +47,7 @@ class DatabaseStorageDriver implements StorageDriver public function ensureTenantCanBeCreated(Tenant $tenant): void { - // todo test this + // todo2 test this if (Tenants::find($tenant->id)) { throw new TenantWithThisIdAlreadyExistsException($tenant->id); } @@ -85,7 +85,7 @@ class DatabaseStorageDriver implements StorageDriver public function updateTenant(Tenant $tenant): void { Tenant::find($tenant->id)->putMany($tenant->data); - // todo update domains + // todo1 update domains } public function deleteTenant(Tenant $tenant): void diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index a8192ec3..cbfb8e1a 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -10,10 +10,10 @@ use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Tenant; -// todo transactions instead of pipelines? +// todo2 transactions instead of pipelines? class RedisStorageDriver implements StorageDriver { - // todo json encoding? + // todo2 json encoding? /** @var Application */ protected $app; @@ -49,7 +49,7 @@ class RedisStorageDriver implements StorageDriver public function ensureTenantCanBeCreated(Tenant $tenant): void { - // todo + // todo2 } public function findByDomain(string $domain): Tenant @@ -76,7 +76,7 @@ class RedisStorageDriver implements StorageDriver } $data = array_combine($keys, $values); - $domains = []; // todo + $domains = []; // todo2 return Tenant::fromStorage($data)->withDomains($domains); } @@ -107,7 +107,7 @@ class RedisStorageDriver implements StorageDriver $pipe->hmset("domains:$domain", 'tenant_id', $tenant->id); } - // todo deleted domains + // todo2 deleted domains }); } @@ -124,7 +124,7 @@ class RedisStorageDriver implements StorageDriver public function all(array $ids = []): array { - // todo $this->redis->pipeline() - return? + // todo2 $this->redis->pipeline() $hashes = array_map(function ($hash) { return "tenants:{$hash}"; }, $ids); diff --git a/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php b/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php index 12cc8293..ec9c17e2 100644 --- a/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php @@ -29,7 +29,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper public function start(Tenant $tenant) { - // todo revisit this + // todo2 revisit this $suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . $tenant->id; // storage_path() diff --git a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php index 4006e373..418ed7e5 100644 --- a/src/TenancyBootstrappers/QueueTenancyBootstrapper.php +++ b/src/TenancyBootstrappers/QueueTenancyBootstrapper.php @@ -50,7 +50,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper 'tenant_id' => $id, 'tags' => [ "tenant:$id", - // todo domain? + // todo2 domain ], ]; } diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index eb703c3c..9ecf22b1 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -52,7 +52,9 @@ class TenancyServiceProvider extends ServiceProvider { $this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy'); - $this->app->bind(Contracts\StorageDriver::class, $this->app['config']['tenancy.storage_driver']); + $this->app->bind(Contracts\StorageDriver::class, function ($app) { + return $app->make($app['config']['tenancy.storage_driver']); + }); $this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']); $this->app->singleton(DatabaseManager::class); $this->app->singleton(TenantManager::class); diff --git a/src/Tenant.php b/src/Tenant.php index ac35d4de..d5d4f393 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -240,7 +240,7 @@ class Tenant implements ArrayAccess public function put($key, $value = null): self { if ($key === 'id') { - throw new TenantStorageException("The tenant's id can't be changed."); + throw new TenantStorageException("Tenant ids can't be changed."); } if (is_array($key)) { diff --git a/src/TenantManager.php b/src/TenantManager.php index dad6196d..7c124cf3 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -29,7 +29,7 @@ class TenantManager protected $artisan; /** @var Contracts\StorageDriver */ - protected $storage; + public $storage; /** @var DatabaseManager */ public $database; @@ -229,7 +229,7 @@ class TenantManager protected function bootstrapFeatures(): self { - // todo this doesn't work + // todo1 this doesn't work // foreach ($this->app['config']['tenancy.features'] as $feature) { // $this->app[$feature]->bootstrap($this); // } diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index b8c24d37..310784cf 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Tests; use Illuminate\Support\Facades\Redis; -// todo rename +// todo2 rename class BootstrapsTenancyTest extends TestCase { public $autoInitTenancy = false; diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index ef1702f3..49055b16 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -117,7 +117,7 @@ class CommandsTest extends TestCase ->expectsOutput('xyz'); } - // todo check that multiple tenants can be migrated at once using all database engines + // todo2 check that multiple tenants can be migrated at once using all database engines /** @test */ public function install_command_works() @@ -129,7 +129,7 @@ class CommandsTest extends TestCase \mkdir($dir, 0777, true); } - // todo move this to a file + // todo2 move this to a file \file_put_contents(app_path('Http/Kernel.php'), "markTestIncomplete(); } diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 1c5a4445..967877e4 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -15,14 +15,14 @@ class TenantAssetTest extends TestCase // response()->file() returns BinaryFileResponse whose content is // inaccessible via getContent, so ->assertSee() can't be used - // $this->get(tenant_asset($filename))->assertSuccessful(); // TODO COMMENTED ASSERTIONS - // $this->assertFileExists($path); // TODO COMMENTED ASSERTIONS + // $this->get(tenant_asset($filename))->assertSuccessful(); // TODO2 COMMENTED ASSERTIONS + // $this->assertFileExists($path); // TODO2 COMMENTED ASSERTIONS $f = \fopen($path, 'r'); $content = \fread($f, \filesize($path)); \fclose($f); - // $this->assertSame('bar', $content); // TODO COMMENTED ASSERTIONS - $this->assertTrue(true); // TODO COMMENTED ASSERTIONS + // $this->assertSame('bar', $content); // TODO2 COMMENTED ASSERTIONS + $this->assertTrue(true); // TODO2 COMMENTED ASSERTIONS } } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index c228041e..a40305a5 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -14,8 +14,6 @@ use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager; class TenantDatabaseManagerTest extends TestCase { - // todo use data providers and TenantDatabaseManager::databaseExists() - /** * @test * @dataProvider database_manager_provider diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 35874b65..e4019376 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -205,6 +205,6 @@ class TenantManagerTest extends TestCase /** @test */ public function id_cannot_be_changed() { - // todo + // todo1 } } diff --git a/tests/TestCase.php b/tests/TestCase.php index bca4707a..500d0461 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -105,14 +105,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $app['config']->set([ 'tenancy.storage_driver' => RedisStorageDriver::class, ]); - - // tenancy()->storage = $app->make(RedisStorageDriver::class); // TODO this shouldn't be necessary but is necessary } elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') { $app['config']->set([ 'tenancy.storage_driver' => DatabaseStorageDriver::class, ]); - - // tenancy()->storage = $app->make(DatabaseStorageDriver::class); // TODO this shouldn't be necessary but is necessary } }