Skip to content

Commit 6b401d7

Browse files
committed
Bring all tests to green
1 parent f0f1149 commit 6b401d7

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

src/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ protected function eagerLoadRelation(array $models, $name, Closure $constraints)
1515
$parentIds = implode('_', collect($models)->pluck('id')->toArray());
1616
$parentName = str_slug(get_class($relation->getParent()));
1717
$childName = str_slug(get_class($relation->getRelated()));
18-
$cache = cache();
18+
// $cache = cache();
19+
//
20+
// if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
21+
// $cache->tags([$parentName, $childName]);
22+
// }
1923

20-
if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
21-
$cache->tags([$parentName, $childName]);
22-
}
23-
24-
$results = $cache
24+
$results = cache()->tags([$parentName, $childName])
2525
->rememberForever("{$parentName}_{$parentIds}-{$childName}s", function () use ($relation) {
2626
return $relation->getEager();
2727
});

src/CachedModel.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Database\Eloquent\Relations\Relation;
88
use LogicException;
99

10+
use Illuminate\Cache\TaggedCache;
11+
1012
abstract class CachedModel extends Model
1113
{
1214
protected function getRelationshipFromMethod($method)
@@ -59,15 +61,21 @@ public function cache(array $tags = [])
5961

6062
if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
6163
array_push($tags, str_slug(get_called_class()));
62-
$cache = $cache->tags($tags);
64+
$cache = cache()->tags($tags);
6365
}
6466

6567
return $cache;
6668
}
6769

68-
public static function flushCache()
70+
public static function flushCache(array $tags = [])
6971
{
70-
cache()->tags([str_slug(get_called_class())])
71-
->flush();
72+
$cache = cache();
73+
74+
if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
75+
array_push($tags, str_slug(get_called_class()));
76+
$cache = cache()->tags($tags);
77+
}
78+
79+
$cache->flush();
7280
}
7381
}

tests/Unit/CacheTest.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function setUp()
1313
{
1414
parent::setUp();
1515

16+
cache()->flush();
17+
1618
factory(Author::class, 10)->create()
1719
->each(function($author) {
1820
factory(Book::class, random_int(2, 10))->make()
@@ -25,22 +27,54 @@ public function setUp()
2527

2628
public function testCacheIsEmptyBeforeLoadingModels()
2729
{
28-
$this->assertNull(cache()->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'));
30+
$results = cache()->tags([
31+
'genealabslaravelmodelcachingtestsfixturesauthor',
32+
'genealabslaravelmodelcachingtestsfixturesbook'
33+
])
34+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
35+
36+
$this->assertNull($results);
2937
}
3038

3139
public function testCacheIsNotEmptyAfterLoadingModels()
3240
{
33-
(new Author)->with('books')->get();
41+
(new Author)->with('books')->get()->first();
42+
43+
$results = cache()->tags([
44+
'genealabslaravelmodelcachingtestsfixturesauthor',
45+
'genealabslaravelmodelcachingtestsfixturesbook'
46+
])
47+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
3448

35-
$this->assertNotNull(cache()->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'));
49+
$this->assertNotNull($results);
3650
}
3751

38-
public function testChangingModelClearsCache()
52+
public function testCreatingModelClearsCache()
3953
{
40-
$author = (new Author)->with('books')->first();
54+
$author = (new Author)->with('books')->get()->first();
4155
$author->name = "John Jinglheimer";
4256
$author->save();
4357

44-
$this->assertNull(cache()->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'));
58+
$results = cache()->tags([
59+
'genealabslaravelmodelcachingtestsfixturesauthor',
60+
'genealabslaravelmodelcachingtestsfixturesbook'
61+
])
62+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
63+
64+
$this->assertNull($results);
65+
}
66+
67+
public function testDeletingModelClearsCache()
68+
{
69+
$author = (new Author)->with('books')->get()->first();
70+
$author->delete();
71+
72+
$results = cache()->tags([
73+
'genealabslaravelmodelcachingtestsfixturesauthor',
74+
'genealabslaravelmodelcachingtestsfixturesbook'
75+
])
76+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
77+
78+
$this->assertNull($results);
4579
}
4680
}

0 commit comments

Comments
 (0)