Skip to content

Commit cce4386

Browse files
authored
Merge pull request #46 from GeneaLabs/laravel-5.5
Laravel 5.5
2 parents a370197 + 8e2573e commit cce4386

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

src/CachedModel.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function newEloquentBuilder($query)
2929
public static function boot()
3030
{
3131
parent::boot();
32+
3233
$class = get_called_class();
3334
$instance = new $class;
3435

src/Console/Commands/Flush.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
2+
3+
use Illuminate\Console\Command;
4+
5+
class Flush extends Command
6+
{
7+
protected $signature = 'modelCache:flush {--model=}';
8+
protected $description = 'Flush cache for a given model.';
9+
10+
public function handle()
11+
{
12+
$option = $this->option('model');
13+
$model = new $option;
14+
$model->flushCache();
15+
$this->info("Cache for model '{$option}' flushed.");
16+
}
17+
}

src/Providers/Service.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Providers;
22

3+
use GeneaLabs\LaravelModelCaching\Console\Commands\Flush;
34
use Illuminate\Support\ServiceProvider;
45

56
class Service extends ServiceProvider
@@ -10,5 +11,6 @@ public function boot()
1011
{
1112
$configPath = __DIR__ . '/../../config/laravel-model-caching.php';
1213
$this->mergeConfigFrom($configPath, 'laravel-model-caching');
14+
$this->commands(Flush::class);
1315
}
1416
}
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit\Console\Commands;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
11+
class FlushTest extends TestCase
12+
{
13+
use RefreshDatabase;
14+
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
cache()->flush();
20+
$publishers = factory(Publisher::class, 10)->create();
21+
factory(Author::class, 10)->create()
22+
->each(function ($author) use ($publishers) {
23+
factory(Book::class, random_int(2, 10))->make()
24+
->each(function ($book) use ($author, $publishers) {
25+
$book->author()->associate($author);
26+
$book->publisher()->associate($publishers[rand(0, 9)]);
27+
$book->save();
28+
});
29+
factory(Profile::class)->make([
30+
'author_id' => $author->id,
31+
]);
32+
});
33+
34+
$bookIds = (new Book)->all()->pluck('id');
35+
factory(Store::class, 10)->create()
36+
->each(function ($store) use ($bookIds) {
37+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
38+
});
39+
cache()->flush();
40+
}
41+
42+
public function testGivenModelIsFlushed()
43+
{
44+
$authors = (new Author)->all();
45+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
46+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
47+
48+
$cachedResults = cache()
49+
->tags($tags)
50+
->get($key);
51+
$result = $this->artisan('modelCache:flush', ['--model' => Author::class]);
52+
$flushedResults = cache()
53+
->tags($tags)
54+
->get($key);
55+
56+
$this->assertEquals($authors, $cachedResults);
57+
$this->assertEmpty($flushedResults);
58+
$this->assertEquals($result, 0);
59+
}
60+
61+
public function testGivenModelWithRelationshipIsFlushed()
62+
{
63+
$authors = (new Author)->with('books')->get();
64+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books';
65+
$tags = [
66+
'genealabslaravelmodelcachingtestsfixturesauthor',
67+
'genealabslaravelmodelcachingtestsfixturesbook',
68+
];
69+
70+
$cachedResults = cache()
71+
->tags($tags)
72+
->get($key);
73+
$result = $this->artisan(
74+
'modelCache:flush',
75+
['--model' => Author::class]
76+
);
77+
$flushedResults = cache()
78+
->tags($tags)
79+
->get($key);
80+
81+
$this->assertEquals($authors, $cachedResults);
82+
$this->assertEmpty($flushedResults);
83+
$this->assertEquals($result, 0);
84+
}
85+
}

tests/Unit/DisabledCachedBuilderTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public function testCursorModelResultsIsNotCached()
169169
public function testFindModelResultsIsNotCached()
170170
{
171171
$author = (new Author)
172+
->with('books')
172173
->disableCache()
173174
->find(1);
174175
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_1';

0 commit comments

Comments
 (0)