diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0e0dfc5..105f40e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -29,7 +29,9 @@ jobs:
composer update --no-interaction --prefer-stable
- name: Run tests
run: |
- phpunit --coverage-text --coverage-clover=coverage.xml
+ CACHE_DRIVER=array phpunit --coverage-text --coverage-clover=coverage_array.xml
+ CACHE_DRIVER=file phpunit --coverage-text --coverage-clover=coverage_file.xml
- uses: codecov/codecov-action@v1
with:
fail_ci_if_error: false
+ file: '*.xml'
diff --git a/.gitignore b/.gitignore
index 2c1fc0c..06c9aac 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
/vendor
composer.phar
composer.lock
-.DS_Store
\ No newline at end of file
+.DS_Store
+database.sqlite
diff --git a/phpunit.xml b/phpunit.xml
index 0845dc0..1e585b5 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -19,4 +19,7 @@
src/
+
+
+
diff --git a/src/Traits/QueryCacheModule.php b/src/Traits/QueryCacheModule.php
index 06b3d6d..738e8f2 100644
--- a/src/Traits/QueryCacheModule.php
+++ b/src/Traits/QueryCacheModule.php
@@ -2,6 +2,7 @@
namespace Rennokki\QueryCache\Traits;
+use BadMethodCallException;
use DateTime;
trait QueryCacheModule
@@ -192,11 +193,11 @@ public function flushQueryCacheWithTag(string $tag): bool
{
$cache = $this->getCacheDriver();
- if (! method_exists($cache, 'tags')) {
- return false;
+ try {
+ return $cache->tags($tag)->flush();
+ } catch (BadMethodCallException $e) {
+ return $cache->flush();
}
-
- return $cache->tags($tag)->flush();
}
/**
@@ -334,7 +335,11 @@ public function getCache()
$this->getCacheBaseTags() ?: []
);
- return $tags ? $cache->tags($tags) : $cache;
+ try {
+ return $tags ? $cache->tags($tags) : $cache;
+ } catch (BadMethodCallException $e) {
+ return $cache;
+ }
}
/**
diff --git a/tests/FlushCacheOnUpdateTest.php b/tests/FlushCacheOnUpdateTest.php
index 7d5988b..9cf0335 100644
--- a/tests/FlushCacheOnUpdateTest.php
+++ b/tests/FlushCacheOnUpdateTest.php
@@ -2,7 +2,6 @@
namespace Rennokki\QueryCache\Test;
-use Cache;
use Rennokki\QueryCache\Test\Models\Page;
class FlushCacheOnUpdateTest extends TestCase
@@ -11,7 +10,7 @@ public function test_flush_cache_on_create()
{
$page = factory(Page::class)->create();
$storedPage = Page::cacheFor(now()->addHours(1))->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
@@ -24,7 +23,7 @@ public function test_flush_cache_on_create()
'name' => '9GAG',
]);
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
@@ -33,7 +32,7 @@ public function test_flush_cache_on_update()
{
$page = factory(Page::class)->create();
$storedPage = Page::cacheFor(now()->addHours(1))->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
@@ -46,7 +45,7 @@ public function test_flush_cache_on_update()
'name' => '9GAG',
]);
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
@@ -55,7 +54,7 @@ public function test_flush_cache_on_delete()
{
$page = factory(Page::class)->create();
$storedPage = Page::cacheFor(now()->addHours(1))->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
@@ -66,7 +65,7 @@ public function test_flush_cache_on_delete()
$page->delete();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
@@ -75,7 +74,7 @@ public function test_flush_cache_on_force_deletion()
{
$page = factory(Page::class)->create();
$storedPage = Page::cacheFor(now()->addHours(1))->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
@@ -86,7 +85,7 @@ public function test_flush_cache_on_force_deletion()
$page->forceDelete();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "pages" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php
index ce59477..75ee440 100644
--- a/tests/MethodsTest.php
+++ b/tests/MethodsTest.php
@@ -36,10 +36,15 @@ public function test_cache_tags()
$post = factory(Post::class)->create();
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
- $cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
- $this->assertNull($cache);
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+
+ // The caches that do not support tagging should
+ // cache the query either way.
+ $this->driverSupportsTags()
+ ? $this->assertNull($cache)
+ : $this->assertNotNull($cache);
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
}
@@ -48,12 +53,12 @@ public function test_cache_flush_with_the_right_tag()
$post = factory(Post::class)->create();
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
Post::flushQueryCache(['test']);
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
@@ -62,14 +67,19 @@ public function test_cache_flush_without_the_right_tag()
$post = factory(Post::class)->create();
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
Post::flushQueryCache(['test2']);
Post::flushQueryCacheWithTag('test2');
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
- $this->assertNotNull($cache);
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
+
+ // The caches that do not support tagging should
+ // flush the cache either way since tags are not supported.
+ $this->driverSupportsTags()
+ ? $this->assertNotNull($cache)
+ : $this->assertNull($cache);
}
public function test_cache_flush_with_more_tags()
@@ -77,7 +87,7 @@ public function test_cache_flush_with_more_tags()
$post = factory(Post::class)->create();
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
Post::flushQueryCache([
@@ -86,7 +96,7 @@ public function test_cache_flush_with_more_tags()
'test3',
]);
- $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNull($cache);
}
@@ -95,12 +105,12 @@ public function test_cache_flush_with_default_tags_attached()
$book = factory(Book::class)->create();
$storedBook = Book::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
- $cache = Cache::tags(['test', Book::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "books" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "books" limit 1a:0:{}', ['test', Book::getCacheBaseTags()[0]]);
$this->assertNotNull($cache);
Book::flushQueryCache();
- $cache = Cache::tags(['test', Book::getCacheBaseTags()[0]])->get('leqc:sqlitegetselect * from "books" limit 1a:0:{}');
+ $cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "books" limit 1a:0:{}', ['test', Book::getCacheBaseTags()[0]]);
$this->assertNull($cache);
}
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 97981fc..8ae36ae 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -2,14 +2,13 @@
namespace Rennokki\QueryCache\Test;
+use Cache;
use Orchestra\Testbench\TestCase as Orchestra;
abstract class TestCase extends Orchestra
{
/**
- * Set up the tests.
- *
- * @return void
+ * {@inheritdoc}
*/
public function setUp(): void
{
@@ -27,10 +26,7 @@ public function setUp(): void
}
/**
- * Get the package providers.
- *
- * @param mixed $app
- * @return array
+ * {@inheritdoc}
*/
protected function getPackageProviders($app)
{
@@ -40,10 +36,7 @@ protected function getPackageProviders($app)
}
/**
- * Set up the environment.
- *
- * @param mixed $app
- * @return void
+ * {@inheritdoc}
*/
public function getEnvironmentSetUp($app)
{
@@ -53,6 +46,9 @@ public function getEnvironmentSetUp($app)
'database' => __DIR__.'/database.sqlite',
'prefix' => '',
]);
+ $app['config']->set(
+ 'cache.driver', getenv('CACHE_DRIVER') ?: env('CACHE_DRIVER', 'array')
+ );
$app['config']->set('auth.providers.users.model', User::class);
$app['config']->set('auth.providers.posts.model', Post::class);
$app['config']->set('auth.providers.kids.model', Kid::class);
@@ -80,4 +76,28 @@ protected function clearCache()
{
$this->artisan('cache:clear');
}
+
+ /**
+ * Get the cache with tags, if the driver supports it.
+ *
+ * @param string $key
+ * @param array|null $tags
+ * @return mixed
+ */
+ protected function getCacheWithTags(string $key, $tags = null)
+ {
+ return $this->driverSupportsTags()
+ ? Cache::tags($tags)->get($key)
+ : Cache::get($key);
+ }
+
+ /**
+ * Check if the current driver supports tags.
+ *
+ * @return bool
+ */
+ protected function driverSupportsTags(): bool
+ {
+ return ! in_array(config('cache.driver'), ['file', 'database']);
+ }
}