Skip to content

Commit

Permalink
Added exception catches for the file driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Aug 9, 2020
1 parent 932b74c commit 2b1e7db
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
database.sqlite
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing" />
</php>
</phpunit>
15 changes: 10 additions & 5 deletions src/Traits/QueryCacheModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rennokki\QueryCache\Traits;

use BadMethodCallException;
use DateTime;

trait QueryCacheModule
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
16 changes: 8 additions & 8 deletions tests/FlushCacheOnUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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);

Expand All @@ -24,7 +24,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);
}
Expand All @@ -33,7 +33,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);

Expand All @@ -46,7 +46,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);
}
Expand All @@ -55,7 +55,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);

Expand All @@ -66,7 +66,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);
}
Expand All @@ -75,7 +75,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);

Expand All @@ -86,7 +86,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);
}
Expand Down
34 changes: 22 additions & 12 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -62,22 +67,27 @@ 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()
{
$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([
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
42 changes: 31 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,10 +26,7 @@ public function setUp(): void
}

/**
* Get the package providers.
*
* @param mixed $app
* @return array
* {@inheritdoc}
*/
protected function getPackageProviders($app)
{
Expand All @@ -40,10 +36,7 @@ protected function getPackageProviders($app)
}

/**
* Set up the environment.
*
* @param mixed $app
* @return void
* {@inheritdoc}
*/
public function getEnvironmentSetUp($app)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 2b1e7db

Please sign in to comment.