From fceddb8c4d5a0fd1567b51f274df751fdc7d7735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 7 Feb 2019 21:44:47 +0100 Subject: [PATCH] Add more tests --- README.md | 4 ++- src/CacheManager.php | 4 +++ tests/BootstrapsTenancyTest.php | 50 +++++++++++++++++++++++++++++++++ tests/CacheManagerTest.php | 24 ++++++++++++++++ tests/TestCase.php | 8 ++++-- 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/BootstrapsTenancyTest.php create mode 100644 tests/CacheManagerTest.php diff --git a/README.md b/README.md index 5202122c..c6d414d3 100644 --- a/README.md +++ b/README.md @@ -326,7 +326,7 @@ Assuming the following tenancy config: ], ``` -The `local` filesystem driver will be suffixed with a directory containing `tenant` and the tenant UUID. +The `local` filesystem driver will be suffixed with a directory named `tenant` + the tenant UUID. ```php >>> Storage::disk('local')->getAdapter()->getPathPrefix() @@ -431,3 +431,5 @@ However, you still need to reload nginx configuration to apply the changes to co ## Testing If you run the tests of this package, please make sure you don't store anything in Redis @ 127.0.0.1:6379 db#14. The contents of this database are flushed everytime the tests are run. + +Some tests are run only if the `CI`, `TRAVIS` and `CONTINUOUS_INTEGRATION` environment variables are set to `true`. This is to avoid things like bloating your MySQL instance with test databases. diff --git a/src/CacheManager.php b/src/CacheManager.php index 08e26f98..07b10f28 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -11,6 +11,10 @@ class CacheManager extends BaseCacheManager $tags = [config('tenancy.cache.prefix_base') . tenant('uuid')]; if ($method === "tags") { + if (count($parameters == 1) && is_array($parameters[0])) { + $parameters = [$parameters]; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items + } + return $this->store()->tags(array_merge($tags, ...$parameters)); } diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php new file mode 100644 index 00000000..8beaf506 --- /dev/null +++ b/tests/BootstrapsTenancyTest.php @@ -0,0 +1,50 @@ +connection()->getName(); + tenancy()->init('localhost'); + $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); + + $this->assertNotEquals($old_connection_name, $new_connection_name); + $this->assertEquals('tenant', $new_connection_name); + } + + /** @test */ + public function redis_is_prefixed() + { + tenancy()->init('localhost'); + foreach (config('tenancy.redis.prefixed_connections', ['default']) as $connection) { + $prefix = config('tenancy.redis.prefix_base') . tenant('uuid'); + $client = Redis::connection($connection)->client(); + $this->assertEquals($prefix, $client->getOption($client::OPT_PREFIX)); + } + } + + /** @test */ + public function filesystem_is_suffixed() + { + $old_storage_path = storage_path(); + tenancy()->init(); + $new_storage_path = storage_path(); + + $this->assertEquals($old_storage_path . "/" . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path); + } + + /** @test */ + public function cache_is_tagged() + { + $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + // todo check that tags are set + // doesn't seem to be possible right now? can't find a way to get TaggedCache's tags + } +} diff --git a/tests/CacheManagerTest.php b/tests/CacheManagerTest.php new file mode 100644 index 00000000..3ec30cbe --- /dev/null +++ b/tests/CacheManagerTest.php @@ -0,0 +1,24 @@ +markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + } + + /** @test */ + public function tags_are_merged_when_array_is_passed() + { + $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + } + + /** @test */ + public function tags_are_merged_when_string_is_passed() + { + $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 4cc9bdb5..a5bd1aa4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Redis; class TestCase extends \Orchestra\Testbench\TestCase { + public $initTenancy = true; + /** * Setup the test environment * @@ -14,12 +16,14 @@ class TestCase extends \Orchestra\Testbench\TestCase protected function setUp() { parent::setUp(); - + Redis::connection('tenancy')->flushdb(); tenant()->create('localhost'); - tenancy()->init('localhost'); + if ($this->initTenancy) { + tenancy()->init('localhost'); + } } /**