Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CachedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function newEloquentBuilder($query)
public static function boot()
{
parent::boot();

$class = get_called_class();
$instance = new $class;

Expand Down
17 changes: 17 additions & 0 deletions src/Console/Commands/Flush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;

use Illuminate\Console\Command;

class Flush extends Command
{
protected $signature = 'modelCache:flush {--model=}';
protected $description = 'Flush cache for a given model.';

public function handle()
{
$option = $this->option('model');
$model = new $option;
$model->flushCache();
$this->info("Cache for model '{$option}' flushed.");
}
}
2 changes: 2 additions & 0 deletions src/Providers/Service.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace GeneaLabs\LaravelModelCaching\Providers;

use GeneaLabs\LaravelModelCaching\Console\Commands\Flush;
use Illuminate\Support\ServiceProvider;

class Service extends ServiceProvider
Expand All @@ -10,5 +11,6 @@ public function boot()
{
$configPath = __DIR__ . '/../../config/laravel-model-caching.php';
$this->mergeConfigFrom($configPath, 'laravel-model-caching');
$this->commands(Flush::class);
}
}
85 changes: 85 additions & 0 deletions tests/Unit/Console/Commands/FlushTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit\Console\Commands;

use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class FlushTest extends TestCase
{
use RefreshDatabase;

public function setUp()
{
parent::setUp();

cache()->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);
$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()
{
$authors = (new Author)->with('books')->get();
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books';
$tags = [
'genealabslaravelmodelcachingtestsfixturesauthor',
'genealabslaravelmodelcachingtestsfixturesbook',
];

$cachedResults = cache()
->tags($tags)
->get($key);
$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);
}
}
1 change: 1 addition & 0 deletions tests/Unit/DisabledCachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function testCursorModelResultsIsNotCached()
public function testFindModelResultsIsNotCached()
{
$author = (new Author)
->with('books')
->disableCache()
->find(1);
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_1';
Expand Down