diff --git a/src/CacheManager.php b/src/CacheManager.php index 98ff4415..daaa8d58 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -9,13 +9,16 @@ class CacheManager extends BaseCacheManager public function __call($method, $parameters) { $tags = [config('tenancy.cache.tag_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 + if (count($parameters) !== 1) { + throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed."); } - return $this->store()->tags(array_merge($tags, ...$parameters)); + $names = $parameters[0]; + $names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items + + return $this->store()->tags(array_merge($tags, $names)); } return $this->store()->tags($tags)->$method(...$parameters); diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index 2d9722dd..6fdab20c 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -65,8 +65,10 @@ class BootstrapsTenancyTest extends TestCase /** @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 + $this->assertSame(['foo'], cache()->tags('foo')->getTags()->getNames()); + $this->initTenancy(); + + $expected = [config('tenancy.cache.tag_base') . tenant('uuid'), 'foo', 'bar']; + $this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames()); } } diff --git a/tests/CacheManagerTest.php b/tests/CacheManagerTest.php index 3ec30cbe..b055f6cd 100644 --- a/tests/CacheManagerTest.php +++ b/tests/CacheManagerTest.php @@ -7,18 +7,34 @@ class CacheManagerTest extends TestCase /** @test */ public function default_tag_is_automatically_applied() { - $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + $this->assertArraySubset([config('tenancy.cache.tag_base') . tenant('uuid')], cache()->tags('foo')->getTags()->getNames()); } /** @test */ public function tags_are_merged_when_array_is_passed() { - $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + $expected = [config('tenancy.cache.tag_base') . tenant('uuid'), 'foo', 'bar']; + $this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames()); } /** @test */ public function tags_are_merged_when_string_is_passed() { - $this->markTestIncomplete('see BootstrapsTenancyTest@cache_is_tagged'); + $expected = [config('tenancy.cache.tag_base') . tenant('uuid'), 'foo']; + $this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames()); + } + + /** @test */ + public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method() + { + $this->expectException(\Exception::class); + cache()->tags(); + } + + /** @test */ + public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method() + { + $this->expectException(\Exception::class); + cache()->tags(1, 2); } }