From 12aace37966247f969aa6aac73d286bf12aa4db3 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 30 Dec 2017 15:50:53 -0800 Subject: [PATCH 1/3] Add functionality to flush model-specific cache Fixes #45 --- src/CachedModel.php | 1 + src/Console/Commands/Flush.php | 16 +++++ src/Providers/Service.php | 2 + tests/Unit/Console/Commands/FlushTest.php | 80 +++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 src/Console/Commands/Flush.php create mode 100644 tests/Unit/Console/Commands/FlushTest.php diff --git a/src/CachedModel.php b/src/CachedModel.php index 790c45b..a2620f6 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -29,6 +29,7 @@ public function newEloquentBuilder($query) public static function boot() { parent::boot(); + $class = get_called_class(); $instance = new $class; diff --git a/src/Console/Commands/Flush.php b/src/Console/Commands/Flush.php new file mode 100644 index 0000000..1a52623 --- /dev/null +++ b/src/Console/Commands/Flush.php @@ -0,0 +1,16 @@ +option('model'); + $model = new $option; + $model->flushCache(); + } +} diff --git a/src/Providers/Service.php b/src/Providers/Service.php index 64afdbb..41c3e0f 100644 --- a/src/Providers/Service.php +++ b/src/Providers/Service.php @@ -1,5 +1,6 @@ mergeConfigFrom($configPath, 'laravel-model-caching'); + $this->commands(Flush::class); } } diff --git a/tests/Unit/Console/Commands/FlushTest.php b/tests/Unit/Console/Commands/FlushTest.php new file mode 100644 index 0000000..d26dc8b --- /dev/null +++ b/tests/Unit/Console/Commands/FlushTest.php @@ -0,0 +1,80 @@ +flush(); + $publishers = factory(Publisher::class, 10)->create(); + factory(Author::class, 10)->create() + ->each(function ($author) use ($publishers) { + factory(Book::class, random_int(2, 10))->make() + ->each(function ($book) use ($author, $publishers) { + $book->author()->associate($author); + $book->publisher()->associate($publishers[rand(0, 9)]); + $book->save(); + }); + factory(Profile::class)->make([ + 'author_id' => $author->id, + ]); + }); + + $bookIds = (new Book)->all()->pluck('id'); + factory(Store::class, 10)->create() + ->each(function ($store) use ($bookIds) { + $store->books()->sync(rand($bookIds->min(), $bookIds->max())); + }); + cache()->flush(); + } + + public function testGivenModelIsFlushed() + { + $authors = (new Author)->all(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + $this->artisan('modelCache:flush', ['--model' => Author::class]); + $flushedResults = cache() + ->tags($tags) + ->get($key); + + $this->assertEquals($authors, $cachedResults); + $this->assertEmpty($flushedResults); + } + + public function testGivenModelWithRelationshipIsFlushed() + { + $authors = (new Author)->with('books')->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesauthor', + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + $this->artisan('modelCache:flush', ['--model' => Author::class]); + $flushedResults = cache() + ->tags($tags) + ->get($key); + + $this->assertEquals($authors, $cachedResults); + $this->assertEmpty($flushedResults); + } +} From c38d5f67c4ae3217c94cc47aa6667b7e343c1fa0 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 30 Dec 2017 15:51:30 -0800 Subject: [PATCH 2/3] Update test to cover all code Fixes #44 --- tests/Unit/DisabledCachedBuilderTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/DisabledCachedBuilderTest.php b/tests/Unit/DisabledCachedBuilderTest.php index 249a994..f6eccd8 100644 --- a/tests/Unit/DisabledCachedBuilderTest.php +++ b/tests/Unit/DisabledCachedBuilderTest.php @@ -169,6 +169,7 @@ public function testCursorModelResultsIsNotCached() public function testFindModelResultsIsNotCached() { $author = (new Author) + ->with('books') ->disableCache() ->find(1); $key = 'genealabslaravelmodelcachingtestsfixturesauthor_1'; From 8e2573e049bae0dead49f0dc52635cd898503c32 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 30 Dec 2017 16:06:12 -0800 Subject: [PATCH 3/3] Update flush test to check exit code of artisan command --- src/Console/Commands/Flush.php | 1 + tests/Unit/Console/Commands/FlushTest.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/Flush.php b/src/Console/Commands/Flush.php index 1a52623..cd80818 100644 --- a/src/Console/Commands/Flush.php +++ b/src/Console/Commands/Flush.php @@ -12,5 +12,6 @@ public function handle() $option = $this->option('model'); $model = new $option; $model->flushCache(); + $this->info("Cache for model '{$option}' flushed."); } } diff --git a/tests/Unit/Console/Commands/FlushTest.php b/tests/Unit/Console/Commands/FlushTest.php index d26dc8b..3a03912 100644 --- a/tests/Unit/Console/Commands/FlushTest.php +++ b/tests/Unit/Console/Commands/FlushTest.php @@ -1,4 +1,4 @@ -tags($tags) ->get($key); - $this->artisan('modelCache:flush', ['--model' => Author::class]); + $result = $this->artisan('modelCache:flush', ['--model' => Author::class]); $flushedResults = cache() ->tags($tags) ->get($key); $this->assertEquals($authors, $cachedResults); $this->assertEmpty($flushedResults); + $this->assertEquals($result, 0); } public function testGivenModelWithRelationshipIsFlushed() @@ -69,12 +70,16 @@ public function testGivenModelWithRelationshipIsFlushed() $cachedResults = cache() ->tags($tags) ->get($key); - $this->artisan('modelCache:flush', ['--model' => Author::class]); + $result = $this->artisan( + 'modelCache:flush', + ['--model' => Author::class] + ); $flushedResults = cache() ->tags($tags) ->get($key); $this->assertEquals($authors, $cachedResults); $this->assertEmpty($flushedResults); + $this->assertEquals($result, 0); } }