Skip to content

Commit ef52b7f

Browse files
committed
fix model flush command
1 parent 31c0b12 commit ef52b7f

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

src/Console/Commands/Flush.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
22

33
use Illuminate\Console\Command;
4+
use Illuminate\Support\Collection;
45

56
class Flush extends Command
67
{
@@ -10,7 +11,7 @@ class Flush extends Command
1011
public function handle()
1112
{
1213
$option = $this->option('model');
13-
14+
1415
if (! $option) {
1516
return $this->flushEntireCache();
1617
}
@@ -32,7 +33,7 @@ protected function flushEntireCache() : int
3233
protected function flushModelCache(string $option) : int
3334
{
3435
$model = new $option;
35-
$usesCachableTrait = collect(class_uses($model))
36+
$usesCachableTrait = $this->getAllTraitsUsedByClass($option)
3637
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
3738

3839
if (! $usesCachableTrait) {
@@ -47,4 +48,21 @@ protected function flushModelCache(string $option) : int
4748

4849
return 0;
4950
}
51+
52+
protected function getAllTraitsUsedByClass(string $classname, bool $autoload = true) : Collection
53+
{
54+
$traits = collect();
55+
56+
if (class_exists($classname, $autoload)) {
57+
$traits = collect(class_uses($classname, $autoload));
58+
}
59+
60+
$parentClass = get_parent_class($classname);
61+
62+
if ($parentClass) {
63+
$traits = $traits->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
64+
}
65+
66+
return $traits;
67+
}
5068
}

tests/Fixtures/PrefixedAuthor.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
22

3-
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
43
use Illuminate\Database\Eloquent\Builder;
54
use Illuminate\Database\Eloquent\Relations\HasMany;
65
use Illuminate\Database\Eloquent\Relations\HasOne;
76

87
class PrefixedAuthor extends BaseModel
98
{
10-
use Cachable;
11-
129
protected $fillable = [
1310
'name',
1411
'email',

tests/Integration/Console/Commands/FlushTest.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,14 @@
66
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
77
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
88
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\PrefixedAuthor;
910
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
1011
use Illuminate\Foundation\Testing\RefreshDatabase;
1112

1213
class FlushTest extends IntegrationTestCase
1314
{
1415
use RefreshDatabase;
1516

16-
public function setUp()
17-
{
18-
parent::setUp();
19-
20-
$this->cache->flush();
21-
$publishers = factory(Publisher::class, 10)->create();
22-
factory(Author::class, 10)->create()
23-
->each(function ($author) use ($publishers) {
24-
factory(Book::class, random_int(2, 10))->make()
25-
->each(function ($book) use ($author, $publishers) {
26-
$book->author()->associate($author);
27-
$book->publisher()->associate($publishers[rand(0, 9)]);
28-
$book->save();
29-
});
30-
factory(Profile::class)->make([
31-
'author_id' => $author->id,
32-
]);
33-
});
34-
35-
$bookIds = (new Book)->all()->pluck('id');
36-
factory(Store::class, 10)->create()
37-
->each(function ($store) use ($bookIds) {
38-
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
39-
});
40-
$this->cache->flush();
41-
}
42-
4317
public function testGivenModelIsFlushed()
4418
{
4519
$authors = (new Author)->all();
@@ -59,6 +33,29 @@ public function testGivenModelIsFlushed()
5933
$this->assertEquals($result, 0);
6034
}
6135

36+
public function testExtendedModelIsFlushed()
37+
{
38+
$authors = (new PrefixedAuthor)
39+
->get();
40+
41+
$key = sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor');
42+
$tags = ['genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'];
43+
44+
$cachedResults = $this
45+
->cache
46+
->tags($tags)
47+
->get($key)['value'];
48+
$result = $this->artisan('modelCache:flush', ['--model' => PrefixedAuthor::class]);
49+
$flushedResults = $this
50+
->cache
51+
->tags($tags)
52+
->get($key)['value'];
53+
54+
$this->assertEquals($authors, $cachedResults);
55+
$this->assertEmpty($flushedResults);
56+
$this->assertEquals($result, 0);
57+
}
58+
6259
public function testGivenModelWithRelationshipIsFlushed()
6360
{
6461
$authors = (new Author)->with('books')->get();

0 commit comments

Comments
 (0)