Skip to content

Commit 4020a2d

Browse files
committed
Add and improve unit tests
1 parent bc3293e commit 4020a2d

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

src/Builder.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,31 @@ protected function eagerLoadRelation(array $models, $name, Closure $constraints)
1515
$relation->addEagerConstraints($models);
1616
$constraints($relation);
1717

18-
return $relation->match(
19-
$relation->initRelation($models, $name),
20-
$this->cacheResults($relation, $models),
21-
$name
22-
);
18+
return $this->cacheResults($relation, $models, $name);
2319
}
2420

25-
protected function cacheResults(Relation $relation, array $models) : Collection
21+
protected function cacheResults(Relation $relation, array $models, string $name) : array
2622
{
2723
$parentIds = implode('_', collect($models)->pluck('id')->toArray());
2824
$parentName = str_slug(get_class($relation->getParent()));
2925
$childName = str_slug(get_class($relation->getRelated()));
3026
$cache = cache();
3127

3228
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
33-
$cache->tags([$parentName, $childName]);
29+
$cache = $cache->tags([$parentName, $childName]);
3430
}
3531

36-
return cache()->tags([$parentName, $childName])
37-
->rememberForever("{$parentName}_{$parentIds}-{$childName}s", function () use ($relation) {
38-
return $relation->getEager();
39-
});
32+
$cachedResults = $cache->rememberForever(
33+
"{$parentName}_{$parentIds}-{$childName}s",
34+
function () use ($relation, $models, $name) {
35+
return $relation->match(
36+
$relation->initRelation($models, $name),
37+
$relation->getEager(),
38+
$name
39+
);
40+
}
41+
);
42+
43+
return $cachedResults;
4044
}
4145
}

src/CachedModel.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function cache(array $tags = [])
4343
{
4444
$cache = cache();
4545

46-
if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
46+
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
4747
array_push($tags, str_slug(get_called_class()));
48-
$cache = cache()->tags($tags);
48+
$cache = $cache->tags($tags);
4949
}
5050

5151
return $cache;

tests/Unit/CacheTest.php

+42-10
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testCacheIsEmptyBeforeLoadingModels()
4949

5050
public function testCacheIsNotEmptyAfterLoadingModels()
5151
{
52-
(new Author)->with('books')->get()->first();
52+
(new Author)->with('books')->get();
5353

5454
$results = cache()->tags([
5555
'genealabslaravelmodelcachingtestsfixturesauthor',
@@ -61,6 +61,21 @@ public function testCacheIsNotEmptyAfterLoadingModels()
6161
}
6262

6363
public function testCreatingModelClearsCache()
64+
{
65+
$author = (new Author)->with('books')->get();
66+
67+
factory(Author::class)->create();
68+
69+
$results = cache()->tags([
70+
'genealabslaravelmodelcachingtestsfixturesauthor',
71+
'genealabslaravelmodelcachingtestsfixturesbook'
72+
])
73+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
74+
75+
$this->assertNull($results);
76+
}
77+
78+
public function testUpdatingModelClearsCache()
6479
{
6580
$author = (new Author)->with('books')->get()->first();
6681
$author->name = "John Jinglheimer";
@@ -94,54 +109,71 @@ public function testHasManyRelationshipIsCached()
94109
$authors = (new Author)->with('books')->get();
95110
$authorIds = implode('_', $authors->pluck('id')->toArray());
96111

97-
$results = cache()->tags([
112+
$results = collect(cache()->tags([
98113
'genealabslaravelmodelcachingtestsfixturesauthor',
99114
'genealabslaravelmodelcachingtestsfixturesbook'
100115
])
101-
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks");
116+
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks"));
102117

103118
$this->assertNotNull($results);
119+
$this->assertEmpty($authors->diffAssoc($results));
120+
$this->assertNotEmpty($authors);
121+
$this->assertNotEmpty($results);
122+
$this->assertEquals($authors->count(), $results->count());
104123
}
105124

106125
public function testBelongsToRelationshipIsCached()
107126
{
108-
$books = (new Book)->with('author')->get()->first();
127+
$books = (new Book)->with('author')->get();
109128
$bookIds = implode('_', $books->pluck('id')->toArray());
110129

111-
$results = cache()->tags([
130+
$results = collect(cache()->tags([
112131
'genealabslaravelmodelcachingtestsfixturesbook',
113132
'genealabslaravelmodelcachingtestsfixturesauthor'
114133
])
115-
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors");
134+
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors"));
116135

117136
$this->assertNotNull($results);
137+
$this->assertEmpty($books->diffAssoc($results));
138+
$this->assertNotEmpty($books);
139+
$this->assertNotEmpty($results);
140+
$this->assertEquals($books->count(), $results->count());
118141
}
119142

120143
public function testBelongsToManyRelationshipIsCached()
121144
{
122145
$books = (new Book)->with('stores')->get();
123146
$bookIds = implode('_', $books->pluck('id')->toArray());
124147

125-
$results = cache()->tags([
148+
$results = collect(cache()->tags([
126149
'genealabslaravelmodelcachingtestsfixturesbook',
127150
'genealabslaravelmodelcachingtestsfixturesstore'
128151
])
129-
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores");
152+
->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores"));
130153

131154
$this->assertNotNull($results);
155+
$this->assertEmpty($books->diffAssoc($results));
156+
$this->assertNotEmpty($books);
157+
$this->assertNotEmpty($results);
158+
$this->assertEquals($books->count(), $results->count());
132159
}
133160

134161
public function testHasOneRelationshipIsCached()
135162
{
136163
$authors = (new Author)->with('profile')->get();
137164
$authorIds = implode('_', $authors->pluck('id')->toArray());
138165

139-
$results = cache()->tags([
166+
$results = collect(cache()
167+
->tags([
140168
'genealabslaravelmodelcachingtestsfixturesauthor',
141169
'genealabslaravelmodelcachingtestsfixturesprofile'
142170
])
143-
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles");
171+
->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles"));
144172

145173
$this->assertNotNull($results);
174+
$this->assertEmpty($authors->diffAssoc($results));
175+
$this->assertNotEmpty($authors);
176+
$this->assertNotEmpty($results);
177+
$this->assertEquals($authors->count(), $results->count());
146178
}
147179
}

0 commit comments

Comments
 (0)